【问题标题】:CLR Stored Procedure with C# throwing errors带有 C# 抛出错误的 CLR 存储过程
【发布时间】:2012-11-06 11:15:33
【问题描述】:

您好,我正在使用 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();
}

但是当我尝试运行它时,我得到了以下错误

消息 6549,级别 16,状态 1,过程 GetProductsByPrice,第 0 行
在执行用户定义的例程或聚合“GetProductsByPrice”期间发生 .NET Framework 错误:
System.Data.SqlClient.SqlException:SQL Server 不支持区域设置标识符 (LCID) 16393。
System.Data.SqlClient.SqlException:
在 Microsoft.SqlServer.Server.SmiEventSink_Default.DispatchMessages(布尔忽略非致命消息)
在 Microsoft.SqlServer.Server.SqlPipe.SendResultsStart(SqlDataRecord 记录)
在 StoredProcedures.GetProductsByPrice(Int32 价格)
用户事务(如果有)将被回滚。

我指的是这个msdn article 的代码。

请帮我解决这个问题。

【问题讨论】:

  • 试试吧:new SqlMetaData("col1", SqlDbType.NVarChar, 100, 1033, SqlCompareOptions.None), new SqlMetaData("col2", SqlDbType.NVarChar, 100, 1033, SqlCompareOptions.None)
  • 旁注:请使用参数,而不是将SQL构造为字符串。养成始终这样做的好习惯。
  • @nemesv 你太棒了!有效 !请发表您的评论作为答案,我会很乐意接受它:) 再次感谢

标签: c# sql-server clr sqlclr


【解决方案1】:

异常状态:The locale identifier (LCID) 16393 is not supported by SQL

SqlMetaData.LocaleId 属性包含列或参数的区域设置 ID,其中 默认值为当前线程的当前区域设置。

在这种情况下,默认值 16393English - India 区域设置 (see table),但您的 SQL 服务器似乎安装了不同的区域设置 English - United States

所以你有三个选择:

  1. 配置/重新安装您的 SQL 服务器以使用区域设置 English - India
  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 的区域设置 ID

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多