C# 是否使用windows窗体检查行是否存在?
我正在创建一个约会表,我想在另一个用户输入之前检查该行是否包含相同的日期、时段、人力资源 在显示此代码之前,连接已打开C# 是否使用windows窗体检查行是否存在?,c#,sql,.net,windows,winforms,C#,Sql,.net,Windows,Winforms,我正在创建一个约会表,我想在另一个用户输入之前检查该行是否包含相同的日期、时段、人力资源 在显示此代码之前,连接已打开 SqlCommand slot_check = new SqlCommand("select * from Appointment where AppoinmentDate='"+textBox1.Text+"' and Slot='"+comboBox3.Text+ "'and HRName='" +comboBox2.Text+"'"); SqlDataReader Ex
SqlCommand slot_check = new SqlCommand("select * from Appointment where AppoinmentDate='"+textBox1.Text+"' and Slot='"+comboBox3.Text+ "'and HRName='" +comboBox2.Text+"'");
SqlDataReader Exist = slot_check.ExecuteReader();
if (Exist.HasRows)
{
string message = "Appointment Already Exists!!!!!";
MessageBox.Show(message);
}
else
{
string message = "Update";
MessageBox.Show(message);
}
System.InvalidOperationException:“ExecuteReader:连接属性尚未初始化。”要执行命令,必须提供两个信息:
要执行的sql字符串和连接到数据库的连接。 如果没有连接,您的命令将无法执行,因为框架不知道如何读取或写入数据库 SqlCommand构造函数有一个重载,它接受两个必需的参数:
SqlCommand cmd = new SqlCommand(sqlText, connectionInstance);
所以你的代码应该是这样的
// The command text to run, without string concatenations and with parameters placeholders
string sqlText = @"select * from Appointment
where AppoinmentDate=@aptDate
and Slot=@slot
and HRName=@name";
// Using statement to correctly close and dispose the disposable objects
using(SqlConnection cnn = new SqlConnection(connectionString))
using(SqlCommand slot_check = new SqlCommand(sqlText, cnn))
{
// A parameter for each placeholder with the proper datatype
cmd.Parameters.Add("@aptDate", SqlDbType.Date).Value = Convert.ToDateTime(textBox1.Text);
cmd.Parameters.Add("@slot", SqlDbType.NVarChar).Value = comboBox3.Text;
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = comboBox2.Text;
cnn.Open();
// Even the SqlDataReader is a disposable object
using(SqlDataReader Exist = slot_check.ExecuteReader())
{
if (Exist.HasRows)
{
string message = "Appointment Already Exists!!!!!";
MessageBox.Show(message + " " + Exist + comboBox2.Text);
}
else
{
string message = "Update";
MessageBox.Show(message);
}
}
}
正如您所看到的,代码现在有一个传递到命令构造函数的连接和一个未连接字符串但使用参数构建的命令文本。对于任何类型的数据库相关操作,使用参数都是必须的。如果没有参数,您的代码可能会被众所周知的参数所利用,但是,如果值中只存在一个引号,则可能会破坏sql语法,从而导致语法错误异常
请注意,这段代码可能仍然是错误的,因为我不知道WHERE语句中使用的列中的表中存储了什么类型的数据。我假设了某种类型,但您应该对照您的表进行检查,并验证它们是否正确 要执行命令,两个信息是必不可少的:
要执行的sql字符串和连接到数据库的连接。 如果没有连接,您的命令将无法执行,因为框架不知道如何读取或写入数据库 SqlCommand构造函数有一个重载,它接受两个必需的参数:
SqlCommand cmd = new SqlCommand(sqlText, connectionInstance);
所以你的代码应该是这样的
// The command text to run, without string concatenations and with parameters placeholders
string sqlText = @"select * from Appointment
where AppoinmentDate=@aptDate
and Slot=@slot
and HRName=@name";
// Using statement to correctly close and dispose the disposable objects
using(SqlConnection cnn = new SqlConnection(connectionString))
using(SqlCommand slot_check = new SqlCommand(sqlText, cnn))
{
// A parameter for each placeholder with the proper datatype
cmd.Parameters.Add("@aptDate", SqlDbType.Date).Value = Convert.ToDateTime(textBox1.Text);
cmd.Parameters.Add("@slot", SqlDbType.NVarChar).Value = comboBox3.Text;
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = comboBox2.Text;
cnn.Open();
// Even the SqlDataReader is a disposable object
using(SqlDataReader Exist = slot_check.ExecuteReader())
{
if (Exist.HasRows)
{
string message = "Appointment Already Exists!!!!!";
MessageBox.Show(message + " " + Exist + comboBox2.Text);
}
else
{
string message = "Update";
MessageBox.Show(message);
}
}
}
正如您所看到的,代码现在有一个传递到命令构造函数的连接和一个未连接字符串但使用参数构建的命令文本。对于任何类型的数据库相关操作,使用参数都是必须的。如果没有参数,您的代码可能会被众所周知的参数所利用,但是,如果值中只存在一个引号,则可能会破坏sql语法,从而导致语法错误异常
请注意,这段代码可能仍然是错误的,因为我不知道WHERE语句中使用的列中的表中存储了什么类型的数据。我假设了某种类型,但您应该对照您的表进行检查,并验证它们是否正确 在SqlCommand中,在何处设置连接?没有连接,您的命令无法到达数据库并执行。旁注:尽快学习如何使用参数化查询而不是串接字符串sez:在查询中使用参数!!如果以后要获取约会数据,可以将
If(Exist.HasRows)
更改为If(Exist.Read())
。.Read()
函数返回true
如果它确实读取了false
,如果它没有读取(如果不存在数据或如果所有数据都已读取),您在SqlCommand中在何处设置连接?没有连接,您的命令无法到达数据库并执行。旁注:尽快学习如何使用参数化查询而不是串接字符串sez:在查询中使用参数!!如果以后要获取约会数据,可以将If(Exist.HasRows)
更改为If(Exist.Read())
。.Read()
函数返回true
,如果它确实读取了false
,如果它没有读取(如果不存在数据或如果所有数据都已读取)