简体   繁体   English

“过程或函数需要未提供的参数。”

[英]“Procedure or function expects parameter which was not supplied.”

I'm trying to execute a stored procedure and print the output, but when I run the below code I'm getting error like "Procedure or function 'SPInsertLocal' expects parameter '@RES', which was not supplied." 我试图执行存储过程并打印输出,但是当我运行下面的代码时,出现诸如“过程或函数'SPInsertLocal'期望未提供参数'@RES'之类的错误”。

private void InsertPdtLocal(string code, string PON,string Qty)
        {
            string str = Properties.Settings.Default.conLocal;
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand("Execute SPInsertLocal @PON,@TCode,@Qty,@Type", con);
            try
            {
                con.Open();
                cmd.CommandTimeout = 150;
                cmd.Parameters.AddWithValue("@PON", PON);
                cmd.Parameters.AddWithValue("@Qty", Qty);
                cmd.Parameters.AddWithValue("@TCode", code);
                cmd.Parameters.AddWithValue("@Type", Globals.s_type);
                SqlParameter output = new SqlParameter("@RES", SqlDbType.Int);
                output.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(output);
                cmd.ExecuteNonQuery();
                con.Close();
                int id = Convert.ToInt32(output.Value);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

What I'm doing wrong here? 我在这里做错了什么?

SqlCommand cmd = new SqlCommand("Execute SPInsertLocal @PON,@TCode,@Qty,@Type,@RES", con);

我没有传递参数,解决了问题

You can refactor the code as follows where the using statement is used for the auto management of connection closing and avoid hardcoding Execute statement in c# code which is a bad practice 您可以按以下方式重构代码,其中using语句用于自动管理连接关闭,并避免在c#代码中对Execute语句进行硬编码,这是一种不好的做法

private void InsertPdtLocal(string code, string PON,string Qty)
        {
            string str = Properties.Settings.Default.conLocal;
            try
            {

            using (SqlConnection con = new SqlConnection(str))
            {
                using (SqlCommand cmd =  con.CreateCommand())
                {
                     cmd.Parameters.AddWithValue("@PON", PON);
                     cmd.Parameters.AddWithValue("@Qty", Qty);
                     cmd.Parameters.AddWithValue("@TCode", code);
                     cmd.Parameters.AddWithValue("@Type", Globals.s_type);
                     var output = cmd.Parameters.Add("@RES" , SqlDbType.Int);
                     output.Direction  = ParameterDirection.Output;
                     cmd.ExecuteNonQuery();
                     int id = Convert.ToInt32(output.Value);
                }
            }

            }
             catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 '程序或 function '...' 需要参数 '...',但未提供。 - 'Procedure or function '...' expects parameter '...', which was not supplied.' 过程或函数期望未提供的参数。 ExecuteNonQuery - Procedure or function expects parameter which was not supplied. ExecuteNonQuery ASPxGridView-“过程或函数“可删除”需要参数“ @id”,但未提供。” - ASPxGridView - “Procedure or function 'deletetable' expects parameter '@id', which was not supplied.” 即使提供了参数,也会引发异常“过程或函数需要参数,但未提供。” - Exception “Procedure or function expects parameter, which was not supplied.” thrown even after supplying the parameter 过程或函数需要参数,该参数未提供。 可空列抛出异常 - Procedure or function expects parameter, which was not supplied. A nullable column throwing exception 我不断收到这个“'Procedure or function'sp_UPDATEAdmin'需要参数'@Email',但没有提供。' - I keep getting this "'Procedure or function 'sp_UPDATEAdmin' expects parameter '@Email', which was not supplied.' SQL server stored proc“过程或函数需要参数,这是未提供的。” - SQL server stored proc “Procedure or function expects parameter, which was not supplied.” 过程或函数“uspExportGetMailinfoTest”需要未提供的参数“@CUSTOMER”。 我错过了什么? - Procedure or function 'uspExportGetMailinfoTest' expects parameter '@CUSTOMER', which was not supplied. What am I missing? 我如何解决此过程或函数“SelectFollow”需要未提供的参数“@userid”。 - How do i resolve this procedure or function 'SelectFollow' expects parameter '@userid', which was not supplied.'? 我收到错误消息“过程或函数'stSelect'期望参数'@stNo',但未提供。” - I get error “Procedure or function 'stSelect' expects parameter '@stNo', which was not supplied.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM