gpt4 book ai didi

c# - 通过 C# 查询时 SQL Server 2005 区分大小写

转载 作者:太空狗 更新时间:2023-10-30 00:55:19 24 4
gpt4 key购买 nike

我在安装一个在多个客户站点都运行良好的产品时遇到了问题,我认为这个问题与他们数据库服务器上的排序规则设置有关。我的代码看起来像这样(我更改了表和变量名称,因为代码是专有的):

using (SqlCommand insertCommand = dbConnection.CreateCommand())
{
insertCommand.CommandText = "INSERT INTO [myTable] ([valueOne] ,[valueTwo] ,[CreationDate]) VALUES (@valueTwo ,@valueTwo ,@creationDate);select IDENT_CURRENT('myTable');";
insertCommand.Parameters.AddWithValue("@valueOne", "Value One");
insertCommand.Parameters.AddWithValue("@valueTwo", "Value Two");
insertCommand.Parameters.AddWithValue("@CreationDate", CreationDate);
dbConnection.Open();
object result = insertCommand.ExecuteScalar();
dbConnection.Close();
}

这在大多数站点以及我们的开发和 QA 机器上都有效,但在这个站点上,我们收到一条错误消息,提示“必须声明标量变量“@creationDate”。我看到的主要差异是该站点具有排序规则值设置为 SQL_Latin1_General_CP1_CS_AS,我们的设置都是 SQL_Latin1_General_CP1_CI_AS。我为我们的数据库更改了这个,但服务器仍然设置为区分大小写的变体。这确实解决了我们在使用不同表时遇到的另一个问题(那个是表名,而不是参数名称),但出于某种原因,这仍然是个问题。有没有人知道如何比在我们的代码库中检查和修复所有大小写差异更快地解决这个问题?

该站点使用 SQL Server 2005,我们的代码是用 C# 和 .NET 3.5 编写的。

谢谢-霍利斯

最佳答案

服务器的排序规则控制那些,这是设计使然。

来自 BOL (强调我的):

The collation of an identifier depends on the level at which it is defined. Identifiers of instance-level objects, such as logins and database names, are assigned the default collation of the instance. Identifiers of objects within a database, such as tables, views, and column names, are assigned the default collation of the database. Variables, GOTO labels, temporary stored procedures, and temporary tables can be created when the connection context is associated with one database and then referenced when the context has been switched to another database. Therefore, the identifiers for variables, GOTO labels, and temporary tables are in the default collation of the instance.

关于c# - 通过 C# 查询时 SQL Server 2005 区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10001736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com