简体   繁体   English

具有C#抛出错误的CLR存储过程

[英]CLR Stored Procedure with C# throwing errors

Hi I am working on making a CLR stored procedure using C#, for which I am learning through examples. 嗨,我正在使用C#制作CLR存储过程,我将通过示例进行学习。

Below is what I am trying now 以下是我现在正在尝试的

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();
}

But when I try to run it I get the below errors 但是当我尝试运行它时,出现以下错误

Msg 6549, Level 16, State 1, Procedure GetProductsByPrice, Line 0 消息6549,级别16,状态1,过程GetProductsByPrice,第0行
A .NET Framework error occurred during execution of user defined routine or aggregate 'GetProductsByPrice': 在执行用户定义的例程或聚合'GetProductsByPrice'期间发生.NET Framework错误:
System.Data.SqlClient.SqlException: The locale identifier (LCID) 16393 is not supported by SQL Server. System.Data.SqlClient.SqlException:SQL Server不支持区域设置标识符(LCID)16393。
System.Data.SqlClient.SqlException: System.Data.SqlClient.SqlException:
at Microsoft.SqlServer.Server.SmiEventSink_Default.DispatchMessages(Boolean ignoreNonFatalMessages) 在Microsoft.SqlServer.Server.SmiEventSink_Default.DispatchMessages(布尔值ignoreNonFatalMessages)
at Microsoft.SqlServer.Server.SqlPipe.SendResultsStart(SqlDataRecord record) 在Microsoft.SqlServer.Server.SqlPipe.SendResultsStart(SqlDataRecord记录)
at StoredProcedures.GetProductsByPrice(Int32 price) 在StoredProcedures.GetProductsByPrice(Int32价格)
User transaction, if any, will be rolled back. 用户事务(如果有)将被回滚。

I am referring to this msdn article for the code. 我指的是此msdn文章中的代码。

Please help me out on this. 请帮助我。

The exception states: The locale identifier (LCID) 16393 is not supported by SQL 异常指出: The locale identifier (LCID) 16393 is not supported by SQL

The SqlMetaData.LocaleId property contains the locale ID of the column or parameter where the default value is the current locale of the current thread. SqlMetaData.LocaleId属性包含列或参数的语言环境ID,其中默认值是当前线程的当前语言环境。

The default value in this your case 16393 which is the English - India locale ( see table ) but it seems your SQL server was installed with a different locale English - United States 在这种情况下,默认值为16393 ,它是English - India语言环境( 请参见表 ),但是您的SQL Server似乎安装了其他语言环境, English - United States

So you have three options: 因此,您有三种选择:

  1. Configure/ReInstall your SQL server to use the locale English - India 配置/重新安装SQL Server以使用语言环境English - India
  2. Change the locale of your current thread to a local which is supported by SQL server 将您当前线程的语言环境更改为SQL Server支持的本地语言环境
  3. Specify the locale manually when creating the SqlMetaData : 创建SqlMetaData时手动指定语言环境:

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

    Where the 1033 is the Locale ID of English - United States 其中1033是English - United States的地区设置ID- English - United States

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有表值参数的C#CLR存储过程 - C# CLR stored procedure with table-valued parameter Linq-to-SQL - 在C#中调用存储过程抛出异常 - Linq-to-SQL - Calling stored procedure throwing exception in C# 在C#CLR存储过程中使用System.Web.HttpUtility.UrlEncode - Using System.Web.HttpUtility.UrlEncode in C# CLR Stored Procedure 如何将可为空的guid声明为带有SQL Server的C#CLR存储过程的可选参数 - How to declare a nullable guid as an optional parameter for a C# CLR stored procedure with SQL Server 我正在通过创建文件夹和文件(如果c#中的clr存储过程中不存在)将数据写入xml文件 - i'm writing data to xml file by creating folder and file if not exists in clr stored procedure in c# C# 代码在控制台函数中工作,但在 SQL CLR 存储过程中不起作用 - C# code works from Console function but does not work in SQL CLR Stored Procedure 使用HTTPClient的c#CLR存储过程调用Web API 2方法 - Call Web API 2 method using c# CLR stored procedure by HTTPClient 如何使用执行存储过程的ExecuteStoreQuery检查C#中的错误? - How to check for errors in C# using ExecuteStoreQuery executing a stored procedure? SQL CLR中的存储过程 - stored procedure in SQL CLR C#中的存储过程 - Stored Procedure in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM