C# guid and SQL uniqueidentifier
SQL is expecting the GUID as a string. The following in C# returns a string Sql is expecting.
"'" + Guid.NewGuid().ToString() + "'"
Something like
INSERT INTO TABLE (GuidID) VALUE ('4b5e95a7-745a-462f-ae53-709a8583700a')
is what it should look like in SQL.
uniqueidentifier Equivalent DataType In C#
It should be System.Guid or Nullable<Guid>
Into what C# data type should I retrieve a SQL Server UNIQUEIDENTIFIER?
Instead of cast try creating Guid
with Guid string or use Guid.Parse
Id = new Guid(dataReader.GetString(0)),
or better to use DataReader.GetGuid()
directly instead of getting string and converting into Guid.
Id = dataReader.GetGuid(0),
SQL Bulk Insert in C# when you have GUID (UniqueIdentifier) Column
I met with a similar issue and solved it with the following code.
DataTable dt = new DataTable();
dt.Columns.Add("IdColumn1", typeof(Guid));
dt.Columns.Add("IdColumn2", typeof(Guid));
foreach (var item in MyCollection)
dt.Rows.Add(item.obj1, item.obj2);
using (SqlConnection con = new SqlConnection(ConnectionString))
{
con.Open();
using (var bulkCopy = new SqlBulkCopy(con))
{
bulkCopy.DestinationTableName = "TableName";
bulkCopy.ColumnMappings.Add("IdColumn1", "IdColumn1");
bulkCopy.ColumnMappings.Add("IdColumn2", "IdColumn2");
bulkCopy.WriteToServer(dt);
}
dt.Dispose();
}
Is there any advantage to storing a GUID as a UNIQUEIDENTIFIER?
The Guid (uniqueidentifier) is 128 bit number. It can be easily indexed like other number types (int/bigint/etc...).
Storing GUID as varchar(50) is bad decision like storing integers in varchar.
How to get Guid from SqlDataReader uniqueidentifier
There is something wrong either with your data or your SQL. The first and third approaches should work - personally I'd use the first form, as it's the clearest in my view.
But look at the stack trace:
...
at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
at System.Data.SqlClient.SqlDataReader.Read()
at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()
Note that it's the Read()
call which is failing, not GetGuid
or the indexer.
My guess is that your property is being fetched several times, and sometimes it works - which is why you were getting a cast exception in your second approach - but for some rows, it fails due to some problem with the data or the SQL. As we've no idea where your data is coming from, we can't help you beyond that diagnosis, but that's where you should look next.
SQL Server unique-identifier equivalent in C#
System.Guid
No conversions needed.
Conversion error (string to uniqueidentifier) when trying to store guid
User parameters and try to specify datatype for uniqueidentier
foreach (entity ent in _entity)
{
con.Open();
string query = @"INSERT INTO tblDemo (ContractType,P2P_Request_SYSID,EntityName,SYSID,
ETag,Name,EntityInstanceId,EntityVersion)
Values (@Type,@P2P_Request_SYSID,@EntityName,@SYSID,@ETag,@Name,
@EntityInstanceId,@EntityVersion)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Type", ent.Type);
cmd.Parameters.Add( "@P2P_Request_SYSID", SqlDbType.UniqueIdentifier, 16 ).Value = ent.P2P_Request_SYSID;
cmd.Parameters.AddWithValue("@EntityName", ent.EntityName);
cmd.Parameters.Add( "@SYSID", SqlDbType.UniqueIdentifier, 16 ).Value = ent.SYSID;
cmd.Parameters.AddWithValue("@ETag", ent.ETag);
cmd.Parameters.AddWithValue("@Name", ent.Name);
cmd.Parameters.Add( "@EntityInstanceId", SqlDbType.UniqueIdentifier, 16 ).Value = ent.EntityInstanceId;
cmd.Parameters.AddWithValue("@EntityVersion", ent.EntityVersion);
cmd.ExecuteNonQuery();
con.Close();
}
Related Topics
C# Split String and Remove Empty String
Static Binding Doesn't Update When Resource Changes
Camera.Main Is Null When Performing Raycast
How to Add a Line Break in C# .Net Documentation
What Replaces Wcf in .Net Core
Replace Unicode Escape Sequences in a String
3D Relative Angle Sum Calculation
How to Implement Gzip Compression in ASP.NET
.Net - Convert Generic Collection to Datatable
Putting HTML Inside HTML.Actionlink(), Plus No Link Text
How to Avoid "Too Many Parameters" Problem in API Design
Why Must We Define Both == and != in C#
Calculate the Display Width of a String in C#
Start New Process, Without Being a Child of the Spawning Process