[英]Issue with SqlParameter and data type
I am just calling a stored procedure which was developed with SQL Server 2005. Here is the signature of my stored proc. 我只是在调用用SQL Server 2005开发的存储过程。这是我的存储过程的签名。
CREATE PROCEDURE [dbo].[GenericSearch]
(
@ModuleName VARCHAR(100),
@ViewName VARCHAR(100),
@WhereClause_XML XML,
@LogicalOperator VARCHAR(3)
)
AS
BEGIN
END
I just call this stored procedure from my front end and pass value like 我只是从前端调用此存储过程,然后传递值,例如
string[] Tables = new string[] { "TPMaster" };
SqlParameter[] cparams = new SqlParameter[4];
SqlParameter param;
param = new SqlParameter("@ModuleName", ModuleName);
param.DbType = DbType.String;
cparams[0] = param;
param = new SqlParameter("@ViewName", DataSource);
param.DbType = DbType.String;
cparams[1] = param;
param = new SqlParameter("@WhereClause_XML", strXml);
param.DbType = DbType.Xml;
cparams[2] = param;
param = new SqlParameter("@LogicalOperator", (rbAnd.Checked ? "AND" : "OR"));
param.DbType = DbType.String;
cparams[3] = param;
ds = Business.Common.GetDataSet("GenericSearch", cparams, Tables);
Here I specify that param.DbType
string for all my varchar
datatype for stored proc and one xml data type. 在这里,我为存储的proc和一种xml数据类型的所有varchar
数据类型指定了param.DbType
字符串。
I am getting an error 我收到一个错误
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar' 过程期望类型为'ntext / nchar / nvarchar'的参数'@statement'
I just do not understand what causes the error. 我只是不明白是什么原因导致了错误。 Please tell me how to fix it. 请告诉我如何解决。
Check the body of the stored procedure, and the internal calls it makes. 检查存储过程的主体及其内部调用。
Probably you're making a call to sp_executesql
and are passing a simple varchar
as the body of the SQL command. 可能您正在调用sp_executesql
并传递一个简单的varchar
作为SQL命令的主体。
Use DbType.AnsiString
for VARCHAR
and DbType.String
for NVARCHAR
. 将DbType.AnsiString
用于VARCHAR
,将DbType.String
用于NVARCHAR
。
Here is a code snippet that I used in a O/R-mapper: 这是我在O / R映射器中使用的代码段:
switch (systemType) {
case 127: // bigint
return DbType.Int64;
case 173: // binary
case 189: // timestamp
case 165: // varbinary, varbinary MAX
case 34: // image
return DbType.Binary;
case 98: // sql_variant
return DbType.Object;
case 104: // bit
return DbType.Boolean;
case 175: // char
return DbType.AnsiStringFixedLength;
case 61: // datetime
case 58: // smalldatetime
return DbType.DateTime;
case 106: // decimal
case 108: // numeric
return DbType.Decimal;
case 62: // float
return DbType.Double;
case 56: // int
return DbType.Int32;
case 60: // money
case 122: // smallmoney
return DbType.Currency;
case 239: // nchar
return DbType.StringFixedLength;
case 99: // ntext
case 231: // nvarchar, nvarchar MAX
return DbType.String;
case 59: // real
return DbType.Single;
case 52: // smallint
return DbType.Int16;
case 35: // text
case 167: // varchar, varchar MAX
return DbType.AnsiString;
case 48: // tinyint
return DbType.Byte;
case 36: // uniqueidentifier
return DbType.Guid;
case 241: // xml
return DbType.Xml;
default:
return (DbType)(-1);
}
Your using sp_executesql, and not passing the correct type of argument. 您使用sp_executesql,并且没有传递正确的参数类型。 @statement is the required argument for sp_executesql and has to be a unicode constant or unicode variable. @statement是sp_executesql的必需参数,并且必须是Unicode常量或Unicode变量。 I'm assuming your building a sql statement based upon the values your accepting into your stored proc. 我假设您根据接受到存储过程中的值构建一个sql语句。
Consider this example: 考虑以下示例:
DECLARE @FieldName VARCHAR(100),@TableName VARCHAR(100),@WhereClause varchar(100)
SET @FieldName = 'ItemID'
SET @TableName = 'Item'
SET @WhereClause = 'ItemID > 200'
DECLARE @yourSql varchar(max)
SET @yourSql = 'SELECT ' + @FieldName + ' FROM ' + @TableName + ' WHERE ' + @WhereClause
EXEC sp_executesql @yourSql
This should throw the same exception your getting. 这应该引发相同的异常。 However, change the declaration of the variable @yourSql from a varchar to an nvarchar like this: 但是,将变量@yourSql的声明从varchar更改为nvarchar,如下所示:
DECLARE @yourSql nvarchar(max)
And you're in the clear. 而且您无事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.