您好,我正在使用 C# 制作一个 CLR 存储过程,我正在通过示例进行学习。
以下是我现在正在尝试的
public static void GetProductsByPrice(int price)
{
SqlConnection connection = new SqlConnection("context connection=true");
connection.Open();
string commandText = "SELECT * FROM Products WHERE PRICE < " + price.ToString();
SqlCommand command = new SqlCommand(commandText, connection);
SqlDataReader reader = command.ExecuteReader();
// Create the record and specify the metadata for the columns.
SqlDataRecord record = new SqlDataRecord(
new SqlMetaData("col1", SqlDbType.NVarChar, 100),
new SqlMetaData("col2", SqlDbType.NVarChar, 100));
// Mark the begining of the result-set.
SqlContext.Pipe.SendResultsStart(record);
// Send 10 rows back to the client.
while (reader.Read())
{
// Set values for each column in the row.
record.SetString(0, reader[1].ToString()); //productName
record.SetString(1, reader[2].ToString()); // productDescription
// Send the row back to the client.
SqlContext.Pipe.SendResultsRow(record);
}
// Mark the end of the result-set.
SqlContext.Pipe.SendResultsEnd();
}
但是当我尝试运行它时,出现以下错误
Msg 6549, Level 16, State 1, Procedure GetProductsByPrice, Line 0
A .NET Framework error occurred during execution of user defined routine or aggregate 'GetProductsByPrice':
System.Data.SqlClient.SqlException: The locale identifier (LCID) 16393 is not supported by SQL Server.
System.Data.SqlClient.SqlException:
at Microsoft.SqlServer.Server.SmiEventSink_Default.DispatchMessages(Boolean ignoreNonFatalMessages)
at Microsoft.SqlServer.Server.SqlPipe.SendResultsStart(SqlDataRecord record)
at StoredProcedures.GetProductsByPrice(Int32 price)
User transaction, if any, will be rolled back.
我指的是这个msdn article获取代码。
请帮我解决这个问题。
最佳答案
异常状态:SQL 不支持区域设置标识符 (LCID) 16393
SqlMetaData.LocaleId属性包含列或参数的区域设置 ID,其中默认值是当前线程的当前区域设置。
您的情况中的默认值16393
是英语-印度
语言环境(see table),但您的SQL服务器似乎安装了不同的语言环境英语 - 美国
所以你有三个选择:
- 配置/重新安装 SQL Server 以使用区域设置
英语 - 印度
- 将当前线程的区域设置更改为 SQL Server 支持的本地设置
创建
SqlMetaData
时手动指定区域设置:SqlDataRecord record = new SqlDataRecord( new SqlMetaData("col1", SqlDbType.NVarChar, 1033, SqlCompareOptions.None), new SqlMetaData("col2", SqlDbType.NVarChar, 1033, SqlCompareOptions.None));
其中 1033 是
英语 - 美国
的区域设置 ID
关于c# - CLR 存储过程与 C# 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13438456/