gpt4 book ai didi

c# - 带有 C# 抛出错误的 CLR 存储过程

转载 作者:行者123 更新时间:2023-12-02 22:22:42 25 4
gpt4 key购买 nike

您好,我正在使用 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,其中默认值为当前线程的当前区域设置。

在这种情况下,默认值 16393English - India 区域设置 ( see table ),但您的 SQL Server 似乎安装了不同的区域设置 英语-美国

所以你有三个选择:

  1. 配置/重新安装您的 SQL 服务器以使用区域设置英语 - 印度
  2. 将当前线程的语言环境更改为 SQL Server 支持的本地语言环境
  3. 在创建 SqlMetaData 时手动指定语言环境:

     SqlDataRecord record = new SqlDataRecord(
    new SqlMetaData("col1", SqlDbType.NVarChar, 1033, SqlCompareOptions.None),
    new SqlMetaData("col2", SqlDbType.NVarChar, 1033, SqlCompareOptions.None));

    其中 1033 是 English - United States

  4. 的区域设置 ID

关于c# - 带有 C# 抛出错误的 CLR 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13438456/

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