【问题标题】:How to supply parameters to a stored procedure如何为存储过程提供参数
【发布时间】:2013-04-28 02:38:18
【问题描述】:

朋友们,我正在制作一个 windows 窗体应用程序,我在 sql-server-2008 中创建了一个存储过程,然后像这样在 windows 窗体中创建了一个外部类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jain_milan.Common
{
   public class Personal
    {
        string name, fathername, mothername, familyhead, dateofbirth,educationlvl, education, blood, gotra, panth, marrital;

        public string Educationlvl
        {
           get { return educationlvl; }
           set { educationlvl = value; }
        }

        public string Panth
        {
            get { return panth; }
            set { panth = value; }
        }

        public string Gotra
        {
            get { return gotra; }
            set { gotra = value; }
        }

        public string Blood
        {
            get { return blood; }
            set { blood = value; }
        }

        public string Education
        {
            get { return education; }
            set { education = value; }
        }

        public string DateOfBirth
        {
            get { return dateofbirth; }
            set { dateofbirth = value; }
        }

        public string FamilyHead
        {
            get { return familyhead; }
            set { familyhead = value; }
        }

        public string MotherName
        {
            get { return mothername; }
            set { mothername = value; }
        }

        public string FatherName
        {
            get { return fathername; }
            set { fathername = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Marrital
        {
            get { return marrital; }
            set { marrital = value; }
        }

    }

}

我为所有参数创建了 get set 函数 在另一个外部类中,我像这样定义所有存储过程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jain_milan.DataAction
{
    public enum InsertStoredProcedure
    {
        insertpersonal,
        insertressicontact,
        insertoccupcontact,
        insertspouse,
        insert1children,
        insert2children,
        insert3children,
        insert4children,
        insert5children
    }
}

将其插入数据库后,我再次创建了一个单独的类并像这样传递参数

  public class InsertAction
    {
       public bool Insertpersonal(Personal per)
       {
               SqlCommand cmd = Database.GetConnection().CreateCommand();
               cmd.CommandText=InsertStoredProcedure.insertpersonal.ToString();
               cmd.CommandType= CommandType.StoredProcedure;
               cmd.Parameters.Add(new SqlParameter("@Name",per.Name));
               cmd.Parameters.Add(new SqlParameter("@FatherName",per.FatherName));
               cmd.Parameters.Add(new SqlParameter("@MotherName",per.MotherName));
               cmd.Parameters.Add(new SqlParameter("@FamilyHead",per.FamilyHead));
               cmd.Parameters.Add(new SqlParameter("@DateOfBirth",per.DateOfBirth));
               cmd.Parameters.Add(new SqlParameter("@EducationLevel", per.Educationlvl));
               cmd.Parameters.Add(new SqlParameter("@Education",per.Education));
               cmd.Parameters.Add(new SqlParameter("@Blood",per.Blood));
               cmd.Parameters.Add(new SqlParameter("@Gotra",per.Gotra));
               cmd.Parameters.Add(new SqlParameter("@Panth",per.Panth));
               cmd.Parameters.Add(new SqlParameter("@Marrital",per.Marrital));
               bool ans = cmd.ExecuteNonQuery() > 0;
               cmd.Dispose();
               Database.CloseConnection();
               return ans;
           }}

然后在提交按钮上我已经完成了提交操作的编码......

  private void submit_addbtn_Click(object sender, EventArgs e)
        {
            try
            {
                //personal data insert
                Personal per = new Personal();
                per.Name = nametxt.Text;
                per.FatherName = f_nametxt.Text;
                per.MotherName = m_nametxt.Text;
                per.Gotra = gotra_txt.Text;
                per.Panth = panthcb.Text;
                per.FamilyHead = fhntext.Text;
                per.Educationlvl = edulvlcb.Text;
                per.Education = educb.Text;
                per.Blood = bloodcb.Text;
                per.Marrital = MarritalStatus;
                per.DateOfBirth = (day + '/' + month + '/' + year).ToString();
                if (new InsertAction().Insertpersonal(per))
                {
                    MessageBox.Show("Personal Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Personal Insertion does not Happen ");
                }
}}

在运行程序时显示错误

过程或函数“insertpersonal”需要未提供的参数“@educationlvl”

但是我已经像所有其他参数一样提供了它,那么发生了什么问题..... 请解决这个问题。拜托拜托

【问题讨论】:

    标签: c# .net sql-server stored-procedures


    【解决方案1】:

    看起来像是命名问题。尝试改变:

    cmd.Parameters.Add(new SqlParameter("@EducationLevel", per.Educationlvl));
    

    cmd.Parameters.Add(new SqlParameter("@EducationLvl", per.Educationlvl));
    

    编辑,婚姻问题:

    尝试改变:

    cmd.Parameters.Add(new SqlParameter("@Marrital",per.Marrital));
    

    cmd.Parameters.Add(new SqlParameter("@Marrital", per.Marrital ?? DBNull.Value));
    

    对于数据库,C# null 的等价物是 DBNull.Value。您不能将 C# null 传递给数据库,它不喜欢它。因此,您要做的是检查您要发送的值是否为null。如果是,请改为发送DBNull.Value。这就是per.Marrital ?? DBNull.Value 所做的。与执行此操作相同,但语法要短得多:

    object marritalValue = per.Marrital;
    if(marritalValue == null)
        marritalValue = DBNull.Value
    cmd.Parameters.Add(new SqlParameter("@Marrital", marritalValue ));
    

    http://msdn.microsoft.com/en-us/library/ms173224.aspx

    【讨论】:

    • 嘿,现在它为婚姻提供相同的味精
    • 它的名字是一样的..所以命名问题
    • 检查您的存储过程 - 它在寻找什么?我猜它正在寻找“marital”(一个 r)(它告诉你的确切错误是什么?)。
    • 不,亲爱的 'r' 没有问题,看上面的代码,即使我在 sql-server-2008 中检查过
    • 尝试将其设置为特定值,否则传入 DBNull.Value。我将编辑我的帖子以显示一个示例。
    【解决方案2】:

    参数必须同名@educationlvl@EducationLevel 不同。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      相关资源
      最近更新 更多