c# - CLR 存储过程与 C# 抛出错误

标签 c# sql-server clr sqlclr

您好,我正在使用 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服务器似乎安装了不同的语言环境英语 - 美国

所以你有三个选择:

  1. 配置/重新安装 SQL Server 以使用区域设置英语 - 印度
  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 是英语 - 美国的区域设置 ID

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

相关文章:

.net - 从 .NET 生成的 IL 文件有什么用(使用 MSIL assembler-ilasm.exe)?

c# - 文件不包含 "WriteAllTextAsync"的定义?

sql - asp.net mvc,原始 sql : Display data in Views generated using aggregate function

sql-server - 服务器对象在 SQL Server Management Studio 中不可用

c# - 带有 Out 参数的 SQL Server CLR UDF - 这可能吗?

c++ - Directshow C++ CLR 中未解析的 token (0A00001x)

c# - C#-如何按顺时针顺序(tl,tr,br,bl)对4点列表进行排序,以用于opencv getPerspective?

c# - .NET vScrollBar 问题

c# - 你什么时候、为什么要封课?

.net - "SELECT TOP 1 1"VS "IF EXISTS(SELECT 1"