【发布时间】: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