我无法执行为nvarchar(128)类型的输入参数提供字符串值的存储过程。有问题的参数是'username'。由于proc的定义是nvarchar(128),请看截图2。下面是我的C#代码,在其中设置参数
if (StoredProcedureExists(auditStoredProc, localConnString))
{
try
{
using (SqlCommand auditCommand = new SqlCommand("sp_executesql", localConnection, tran))
{
auditCommand.CommandType = CommandType.StoredProcedure;
auditCommand.CommandTimeout = sqlCommandTimeoutFallbackValue;
auditCommand.Parameters.AddWithValue("@stmt", auditStoredProc);
var userNameParam = auditCommand.CreateParameter();
userNameParam.ParameterName = "@username";
userNameParam.SqlDbType = SqlDbType.NVarChar;
userNameParam.Value = "JQS";
auditCommand.Parameters.Add(userNameParam);
//my custom method to show what command will be executed
var commnd = CommandAsSql(auditCommand);
auditCommand.ExecuteNonQuery();
}
}
catch (Exception e)
{
}
}
由代码构建的命令执行查询是
use my_db_name_here;
declare @return_value int;
exec [sp_executesql]
@stmt = 'dbo.pr_kr_AuditSetUserName',
@username = 'JQS';
select 'Return Value' = convert(varchar, @return_value);
执行时,我看到的异常是
“ JQS附近的语法不正确”
完整异常消息: e = {“'JQS'附近的语法不正确。”}
“ System.Data.SqlClient.SqlException(0x80131904):'JQS'附近的语法不正确。\ r \ n 在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection, 操作
1 wrapCloseInAction)\r\n
1 wrapCloseInAction)\ r \ n
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, 布尔值调用者HasConnectionLock,布尔值asyncClose)\ r \ n
在System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler, SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj, Boolean&dataReady)\ r \ n
在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior, 字符串resetOptionsString,布尔值isInternal,布尔值forDescribeParameterEncryption, 布尔值shouldCacheForAlwaysEncrypted)\ r \ n
在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior, 布尔returnStream,布尔异步,Int32超时,任务和任务,布尔asyncWrite, 布尔值inRetry,SqlDataReader ds,布尔值describeParameterEncryptionRequest)\ r \ n
在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior,布尔值returnStream,字符串方法,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)\r\n
1完成, 字符串methodName,布尔值sendToPipe,Int32超时,布尔值和usedCache,布尔值asyncWrite, 布尔inRetry)\ r \ n位于System.Data.SqlClient.SqlCommand.ExecuteNonQuery()\ r \ n 在Endpoint.JQS.Common.Processor.ProcessJob(JobQueueMessage消息) 在C:\ dev \ Endpoint.JQS \ Endpoint.JQS.Common \ Processor.cs:行92 \ r \ n ClientConnectionId:39dbe3e4-8b0e-4d23-8b13-2bc379313328 \ r \ n 错误号:102,状态:1,类:15“
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
注意: 错误消息中的Ln#92是调用auditCommand.ExecuteNonQuery();的行。
答案 0 :(得分:0)
我用存储过程名称本身替换了“ sp_execute_sql”。这也消除了在@stmt参数中传递存储的proc名称的需要。工作代码如下。
try
{
using (SqlCommand auditCommand = new SqlCommand(
auditStoredProc,
localConnection,
tran))
{
auditCommand.CommandType = CommandType.StoredProcedure;
auditCommand.CommandTimeout = sqlCommandTimeoutFallbackValue;
var userNameParam = auditCommand.CreateParameter();
userNameParam.ParameterName = "@username";
userNameParam.SqlDbType = SqlDbType.NVarChar;
userNameParam.Value = "JobQueueService";
auditCommand.Parameters.Add(userNameParam);
auditCommand.ExecuteNonQuery();
}
}
catch (Exception e)
{
Logger.Error("Error executing audit storedprocedure" + e.ToString());
}