2013-04-28 22 views
1

朋友,我想提出的,我已經在SQL服務器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; } 
     } 

    } 

} 

在那裏我已經創建獲取設置功能適用於所有的paramerets 在另一個外部類我定義所有的存儲過程,這樣

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 
    } 
} 

之後將其插入到我再次創造了一個seprate類數據庫,並通過類似的參數這

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'未提供

Bu我已經提供了像所有其他參數然後最新問題發生..... 請解決這一點。請請請

回答

3

看起來像一個命名問題。嘗試改變:

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

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

編輯,爲Marrital問題:

嘗試改變:

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

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

數據庫的等效C#nullDBNull.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

+0

嘿,現在它給同樣的味精marrital – user2327043 2013-04-28 03:04:36

+0

它的名字是一樣的..所以命名問題 – user2327043 2013-04-28 03:05:26

+0

檢查你的存儲過程 - 它在尋找什麼?我猜想它正在尋找「婚姻」(一個r)(它告訴你的確切錯誤是什麼?)。 – Gjeltema 2013-04-28 03:06:13

1

的參數必須具有相同的名稱@educationlvl是不一樣的@EducationLevel。希望這可以幫助。