【问题标题】:C# call to MS-SQL stored procedure not receiving parametersC# 调用 MS-SQL 存储过程不接收参数
【发布时间】:2022-08-10 14:07:51
【问题描述】:

下图是调用MS=SQL存储过程的代码,可以看到,我尝试在请求中提供参数

using (SqlConnection connection = new SqlConnection(sqlAuth)) {

    // Open database connectionS
    connection.Open();

    // Call upon stored procedure
    SqlCommand cmd = new SqlCommand(\"InsertSystem\", connection);
    cmd.Parameters.Add(\"@ObjectID\", SqlDbType.VarChar).Value = Request.Form[\"objectid\"].ToString();
    cmd.Parameters.Add(\"@SystemID\", SqlDbType.VarChar).Value = Request.Form[\"systemid\"].ToString();
    cmd.Parameters.Add(\"@ItemID\", SqlDbType.Int).Value = Request.Form[\"itemid\"].ToString();
    int records = cmd.ExecuteNonQuery();

    // Close database connection
    connection.Close();

}

message = \"Record Saved<br /><br />\";

从这里返回的错误如下:

System.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=Procedure or function \'InsertSystem\' expects parameter \'@SystemID\', which was not supplied.
  Source=Core .Net SqlClient Data Provider
  StackTrace:
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at ServiceObjectsWebApp.Pages.ObjectInsertModel.OnPost() in C:\\Users\\rmcd\\Desktop\\ServiceObecjts\\ServiceObjectsWebApp\\Pages\\ObjectInsert.cshtml.cs:line 46
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.VoidHandlerMethod.Execute(Object receiver, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.<InvokeHandlerMethodAsync>d__29.MoveNext()

  This exception was originally thrown at this call stack:
    [External Code]
    ServiceObjectsWebApp.Pages.ObjectInsertModel.OnPost() in ObjectInsert.cshtml.cs
    [External Code]
  • 请编辑您的问题并用文本替换图像,并格式化您的错误消息,以便正确显示换行符。
  • Request.Form[\"systemid\"].ToString(); 在调试器的即时窗口中显示什么?您是否单步执行了此代码,或者您只是希望它能够工作?

标签: c#


【解决方案1】:

您正在调用存储过程,因此您需要告诉它:

cmd.CommandType = CommandType.StoredProcedure;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    相关资源
    最近更新 更多