C# guid 和 SQL uniqueidentifier
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1435908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# guid and SQL uniqueidentifier
提问by Daniel
I want to create a GUID and store it in the DB.
我想创建一个 GUID 并将其存储在数据库中。
In C# a guid can be created using Guid.NewGuid(). This creates a 128 bit integer. SQL Server has a uniqueidentifier column which holds a huge hexidecimal number.
在 C# 中,可以使用 Guid.NewGuid() 创建 guid。这将创建一个 128 位整数。SQL Server 有一个 uniqueidentifier 列,其中包含一个巨大的十六进制数。
Is there a good/preferred way to make C# and SQL Server guids play well together? (i.e. create a guid using Guid.New() and then store it in the database using nvarchar or some other field ... or create some hexidecimal number of the form that SQL Server is expecting by some other means)
有没有一种好的/首选的方法可以让 C# 和 SQL Server guid 很好地协同工作?(即使用 Guid.New() 创建一个 guid,然后使用 nvarchar 或其他一些字段将其存储在数据库中......或者通过其他方式创建 SQL Server 期望的形式的一些十六进制数)
采纳答案by Peter Oehlert
SQL is expecting the GUID as a string. The following in C# returns a string Sql is expecting.
SQL 期望 GUID 作为字符串。C# 中的以下内容返回 Sql 期望的字符串。
"'" + Guid.NewGuid().ToString() + "'"
Something like
就像是
INSERT INTO TABLE (GuidID) VALUE ('4b5e95a7-745a-462f-ae53-709a8583700a')
is what it should look like in SQL.
是它在 SQL 中的样子。
回答by Jay Riggs
Store it in the database in a field with a data type of uniqueidentifier.
将其存储在数据库中的字段中,其数据类型为 uniqueidentifier。
回答by DLKJ
Here's a code snippet showing how to insert a GUID using a parameterised query:
下面的代码片段展示了如何使用参数化查询插入 GUID:
using(SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using(SqlTransaction trans = conn.BeginTransaction())
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.Transaction = trans;
cmd.CommandText = @"INSERT INTO [MYTABLE] ([GuidValue]) VALUE @guidValue;";
cmd.Parameters.AddWithValue("@guidValue", Guid.NewGuid());
cmd.ExecuteNonQuery();
trans.Commit();
}
}
回答by canon
You can pass a C# Guid value directly to a SQL Stored Procedure by specifying SqlDbType.UniqueIdentifier
.
您可以通过指定将 C# Guid 值直接传递给 SQL 存储过程SqlDbType.UniqueIdentifier
。
Your method may look like this (provided that your only parameter is the Guid):
您的方法可能如下所示(前提是您的唯一参数是 Guid):
public static void StoreGuid(Guid guid)
{
using (var cnx = new SqlConnection("YourDataBaseConnectionString"))
using (var cmd = new SqlCommand {
Connection = cnx,
CommandType = CommandType.StoredProcedure,
CommandText = "StoreGuid",
Parameters = {
new SqlParameter {
ParameterName = "@guid",
SqlDbType = SqlDbType.UniqueIdentifier, // right here
Value = guid
}
}
})
{
cnx.Open();
cmd.ExecuteNonQuery();
}
}
See also: SQL Server's uniqueidentifier
另请参阅:SQL Server 的 uniqueidentifier
回答by Sunandan Dutt
// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(GentEFONRFFConnection);
myConnection.Open();
SqlCommand myCommand = new SqlCommand("your Procedure Name", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@orgid", SqlDbType.UniqueIdentifier).Value = orgid;
myCommand.Parameters.Add("@statid", SqlDbType.UniqueIdentifier).Value = statid;
myCommand.Parameters.Add("@read", SqlDbType.Bit).Value = read;
myCommand.Parameters.Add("@write", SqlDbType.Bit).Value = write;
// Mark the Command as a SPROC
myCommand.ExecuteNonQuery();
myCommand.Dispose();
myConnection.Close();