C# SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受DBNull对象
每当我在文本框中键入nothing或null值时,就会出现一个错误。我的代码如下:C# SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受DBNull对象,c#,sql-server,C#,Sql Server,每当我在文本框中键入nothing或null值时,就会出现一个错误。我的代码如下: SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "INSERT INTO Records ([Student ID], [First Name], [Last Name], [Middle Initial], Gender, Address, Status, Ye
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.Any(p => DBNull.Value.Equals(p.Value)) )
if ( cmd.Parameters.Any(p => string.IsNullOrWhiteSpace((string)p.Value)) )
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.
}
错误发生在:
if(cmd.Parameters.Contains(System.DBNull.Value))
我使用的是SQL Server和C#。首先,您需要查找任何参数的value属性是否为DBNull。您正在尝试检查参数对象本身是否为DBNull,但您应该查看Value属性。您可以这样做:
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.Any(p => DBNull.Value.Equals(p.Value)) )
if ( cmd.Parameters.Any(p => string.IsNullOrWhiteSpace((string)p.Value)) )
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.
}
其次,textbox控件的.Text
属性永远不会为空。最坏情况下,它将是空字符串。所以您真正想要的是这样的代码:
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.Any(p => DBNull.Value.Equals(p.Value)) )
if ( cmd.Parameters.Any(p => string.IsNullOrWhiteSpace((string)p.Value)) )
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.
}
最后,顺便说一句,即使
.Text
属性可以是null
,这与DBNull
不同。它们是两种不同的东西。干净有效的方法。将在以后的开发中帮助您。更改代码,如下所示:
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.Any(p => DBNull.Value.Equals(p.Value)) )
if ( cmd.Parameters.Any(p => string.IsNullOrWhiteSpace((string)p.Value)) )
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.
}
FirstName
、LastName
和其他对象创建属性。大概是这样的:
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.Any(p => DBNull.Value.Equals(p.Value)) )
if ( cmd.Parameters.Any(p => string.IsNullOrWhiteSpace((string)p.Value)) )
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));
}
请记住,这是未经测试的,我现在没有VS,并且所有内容都是手动编写的,因此可能无法以这种方式访问控件集合。在将任何内容分配给命令参数之前,需要检查所设置的值是否为
null
。如果它是一个文本框,那么您可能需要检查(!string.IsNullOrEmpty(textbox.text))是否//然后添加参数
我按照您所说的做了,但即使数据为空,它仍会继续插入数据。为什么在CreateParameterList
中将Student
和SqlCommand
声明为ref参数?Student和SqlCommand都是类,因此默认情况下通过引用传递。没有必要使用ref。请看@tuzo我写ref不是强制性的,他必须使用ref。如果你能看到我的代码,我跳过了这么多属性和参数,所以这并不意味着OP也应该跳过这些。上面的代码只是为探索Stuff实现的新方法而给出的建议或提示。如果他能做研究,那么他必须能够找到几十种同样的方法。这取决于他,他需要什么样的方式,他更喜欢什么样的方式?Joel,这段代码不错,但我认为这对中级开发人员和以上级别的开发人员来说肯定是好的,从OP的代码来看,他似乎是新手,不适合从LINQ和匿名之类的东西开始。但是如果他理解了,那么这对他来说真的会很有帮助。