使用ASP.NET MVC中的存储过程在ExecuteNonQuery中缺少参数

时间:2018-05-17 17:40:54

标签: stored-procedures ado.net

以下是我的代码,我在ExecuteNonQuery处收到错误:

  

缺少@Name参数。

我在程序构建过程中尝试过很多次甚至没有错误。存储过程包含一个带有4个参数的insert语句,3个varchar类型和一个integer类型作为主键。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace CompanyMaster
{
    public class Master
    {
         public IEnumerable<Company> Companies
         {
            get
            {
                string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

                List<Company> companies = new List<Company>();

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand("spGetAllCompany", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();

                    SqlDataReader rdr = cmd.ExecuteReader();

                    while (rdr.Read())
                    {
                        Company company = new Company();
                        company.CompanyCode = Convert.ToInt32(rdr["CompanyCode"]);
                        company.CompanyName = rdr["CompanyName"].ToString();
                        company.CompanyAddress = rdr["CompanyAddress"].ToString();
                        company.CompanyMail = rdr["CompanyMail"].ToString();

                        companies.Add(company);
                    }
                }
                return companies;
            }
       }

       public void Addcompany(Company company)
       {    
           string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

           using (SqlConnection con = new SqlConnection(connectionString))
           {
               SqlCommand cmd = new SqlCommand("spAddCompany", con);
               cmd.CommandType = CommandType.StoredProcedure;

               cmd.Parameters.Clear();

               SqlParameter paramCode = new SqlParameter();
               paramCode.ParameterName = "@Code";
               paramCode.Value = company.CompanyCode;
               cmd.Parameters.Add(paramCode);

               SqlParameter PName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
               //PName.ParameterName = "@Name";
               PName.Value = company.CompanyName;
               cmd.Parameters.Add(PName);

               SqlParameter paramAddress = new SqlParameter();
               paramAddress.ParameterName = "@Address";
               paramAddress.Value = company.CompanyAddress;
               cmd.Parameters.Add(paramAddress);

               SqlParameter paramMail = new SqlParameter();
               paramMail.ParameterName = "@Mail";
               paramMail.Value = company.CompanyMail;
               cmd.Parameters.Add(paramMail);

               con.Open();
               cmd.ExecuteNonQuery();-- error is occurring here
           }
       }
    }
}

这是我的存储过程:

CREATE PROCEDURE spAddCompany
    @Code INT,
    @Name NVARCHAR(50),
    @Address NVARCHAR(60),
    @Mail NVARCHAR(50)
AS
BEGIN
    INSERT INTO CompanyMaster (CompanyCode, CompanyName, CompanyAddress, CompanyMail)
    VALUES (@Code, @Name, @Address, @Mail)
END
当代码到达@Name时,

ExecuteNonQuery参数丢失。

1 个答案:

答案 0 :(得分:0)

我认为您的问题与null vs DBNull.Value有关 检查company.CompanyName是否为空(在c#中)。如果是,您应该通过DBNull.Value 有关两者之间差异的更多信息,请阅读What is the difference between null and System.DBNull.Value?

来自Configuring Parameters and Parameter Data Types

  

注意

     

将null参数值发送到服务器时,必须指定DBNull,而不是null(在Visual Basic中为Nothing)。系统中的null值是一个没有值的空对象。 DBNull用于表示空值。有关数据库空值的更多信息,请参阅Handling Null Values

此外,您可以向命令添加参数并在一行代码中设置它们的值,如下所示:

cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = company.CompanyName;

这将使您的代码更短,更易读。

以下是我对您的代码所做的更改,我认为应该解决您的问题:

public void Addcompany(Company company)
{    
    string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

    using (var con = new SqlConnection(connectionString))
    {
        // SqlCommand also implements the IDisposable interface
        using(var cmd = new SqlCommand("spAddCompany", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Code",  SqlDbType.Int).Value = company.CompanyCode;
            cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = company.CompanyName as object ?? (object)DBNull.Value;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = company.CompanyAddress as object ?? (object)DBNull.Value;
            cmd.Parameters.Add("@Mail", SqlDbType.VarChar, 50).Value = company.CompanyMail as object ?? (object)DBNull.Value;
            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
}

请注意使用空合并运算符(??)和转换为object