![](/img/trans.png)
[英]SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects
[英]The SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects
每當我在文本框中不輸入任何內容或返回null值時,就會出現錯誤。 我的代碼是這樣的:
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO Records
([Student ID], [First Name], [Last Name], [Middle Initial],
Gender, Address, Status, Year, Email, Course,
[Contact Number])
VALUES (@StudentID, @FirstName, @LastName , @MiddleInitial,
@Gender, @Address, @Status, @Year, @Email, @Course,
@ContactNumber)";
SqlParameter p1 = new SqlParameter("@StudentID", SqlDbType.NChar);
p1.Value = textBox1.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("@FirstName", SqlDbType.NVarChar);
p2.Value = textBox2.Text;
cmd.Parameters.Add(p2);
SqlParameter p3 = new SqlParameter("@LastName", SqlDbType.NVarChar);
p3.Value = textBox3.Text;
cmd.Parameters.Add(p3);
SqlParameter p4 = new SqlParameter("@MiddleInitial", SqlDbType.NChar);
p4.Value = comboBox1.Text;
cmd.Parameters.Add(p4);
SqlParameter p5 = new SqlParameter("@Gender", SqlDbType.NChar);
p5.Value = comboBox2.Text;
cmd.Parameters.Add(p5);
SqlParameter p6 = new SqlParameter("@Address", SqlDbType.VarChar);
p6.Value = textBox4.Text;
cmd.Parameters.Add(p6);
SqlParameter p7 = new SqlParameter("@Status", SqlDbType.NChar);
p7.Value = comboBox3.Text;
cmd.Parameters.Add(p7);
SqlParameter p8 = new SqlParameter("@Year", SqlDbType.VarChar);
p8.Value = comboBox4.Text;
cmd.Parameters.Add(p8);
SqlParameter p9 = new SqlParameter("@Email", SqlDbType.VarChar);
p9.Value = textBox5.Text;
cmd.Parameters.Add(p9);
SqlParameter p10 = new SqlParameter("@Course", SqlDbType.VarChar);
p10.Value = comboBox5.Text;
cmd.Parameters.Add(p10);
SqlParameter p11 = new SqlParameter("@ContactNumber", SqlDbType.VarChar);
p11.Value = textBox6.Text;
cmd.Parameters.Add(p11);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
comboBox4.Text = "";
comboBox5.Text = "";
if (cmd.Parameters.Contains(System.DBNull.Value))
{
MessageBox.Show("Please complete the fields", "Information...",
MessageBoxButtons.OK, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
}
else
{
MessageBox.Show("Data Inserted!", "Information ... ",
MessageBoxButtons.OK, MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
}
cmd.ExecuteNonQuery();
con.Close();
錯誤發生在:
if(cmd.Parameters.Contains(System.DBNull.Value))
我正在使用SQL Server和C#。
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO Records
([Student ID], [First Name], [Last Name], [Middle Initial],
Gender, Address, Status, Year, Email, Course,
[Contact Number])
VALUES (@StudentID, @FirstName, @LastName , @MiddleInitial,
@Gender, @Address, @Status, @Year, @Email, @Course,
@ContactNumber)";
Control[] controls = {textBox1,textBox2, textBox3, textBox4, textBox5, textBox6, comboBox1, comboBox2, comboBox3, comboBox4, comboBox5};
foreach(Control c in controls){
if(c.Text.Trim() == "") {
MessageBox.Show("Please complete the fields", "Information...",
MessageBoxButtons.OK, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
c.Focus();//Focus it to let user enter some value again.
return;
}
}
//Initialize your parameters here
//....
//....
//....
try {
cmd.ExecuteNonQuery();
MessageBox.Show("Data Inserted!", "Information ... ",
MessageBoxButtons.OK, MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
foreach(Control c in controls) c.Text = "";
}catch{}
finally {
con.Close();
}
首先,您需要查看任何參數的value屬性是否為DBNull。 您正在嘗試檢查參數對象本身是否為DBNull,但是您應該查看Value屬性。 您可以這樣做:
if ( cmd.Parameters.Any(p => DBNull.Value.Equals(p.Value)) )
其次,文本框控件的.Text
屬性將永遠不會為null。 最糟糕的是,它將是空字符串。 因此,您真正想要的是這樣的代碼:
if ( cmd.Parameters.Any(p => string.IsNullOrWhiteSpace((string)p.Value)) )
最后, .Text
,即使.Text
屬性可以為null
,這也與DBNull
。 他們是兩個不同的東西。
清潔有效的方法。 將在以后的開發中為您提供幫助。 更改您的代碼,如下所示:
創建一個稱為“學生”的類,並在該類中根據需要為FirstName
, LastName
和其他屬性創建屬性。 像這樣:
public class Student { public int StudentId {get;set;} public string FirstName {get;set;} public string FirstName {get;set;} public string MiddleInitial{get;set;} // upto ContactNumber or as required. }
創建用於將參數分配給SqlCommand
的單獨方法。 在這些方法中,如果需要使用三元運算(例如?),也可以在此處進行驗證:
private void CreateParameterList(ref Student s, ref SqlCommand cmd) { var parameters = new [] { new SqlParameter("@FirstName",s.FirstName), new SqlParameter("@LastName",s.LastName), : : new SqlParameter("@ContactNumber",s.ContactNumber) } cmd.Parameters.AddRange(parameters); }
創建一個保存方法,並將上述類作為參數傳遞。 您也可以使用Student
名稱創建單獨的局部類,以便使用相同的實例訪問成員。 在步驟2中調用方法,以使用參數。
protected void MySaveFunction(Student s) { using(SqlConnection con= new SqlConnection(your_Connection_String)) { using(SqlCommmand cmd = new SqlCommand()) { cmd.CommandType = CommandType.Text/StoredProcedure; // it depends on what you are using cmd.Connection = con; cmd.CommandText=Your Query or Name of SP; con.Open(); CreateParameterList(ref s, ref cmd); try { int i = cmd.ExecuteNonQuery(); if(i>0) { //Show Success or return Success } } catch(SqlException sqlex) { //catch and log exception } finally { con.Close(); con.Dispose() } } } }
您為什么不重新開始並進行一些適當的驗證,然后再進行此操作? 如果沒有必要,永遠不要開始創建對象和東西。 因此,請幫自己一個忙,並添加一個驗證方法,例如
private bool IsValid()
{
return !Controls.OfType<TextBox>().Any(c=> String.IsNullOrEmpty(c.Text));
}
然后在代碼之前添加一個調用以構造sqlcommand和參數集合
if(IsValid())
{
// do you stuff here
}
else
{
MessageBox.Show("Please complete the fields", "Information...",
MessageBoxButtons.OK, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
}
請記住,這是未經測試的,我現在沒有VS,並且所有代碼都是手動編寫的,因此可能無法以這種方式訪問Control集合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.