CLR Stored Procedure with C# throwing errors

ε祈祈猫儿з 提交于 2019-12-24 04:13:00

问题


Hi I am working on making a CLR stored procedure using C#, for which I am learning through examples.

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
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.

I am referring to this msdn article for the code.

Please help me out on this.


回答1:


The exception states: 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.

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

So you have three options:

  1. Configure/ReInstall your SQL server to use the locale English - India
  2. Change the locale of your current thread to a local which is supported by SQL server
  3. Specify the locale manually when creating the 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



来源:https://stackoverflow.com/questions/13438456/clr-stored-procedure-with-c-sharp-throwing-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!