我有一个文本框,它将从用户那里获取输入并搜索插入的数据是否在SQL数据库表中可用。如果数据在表中,那么它将更新同一行的两列(time_out和day_out) 否则它将显示错误消息。以下代码无效。请帮忙。
try
{
SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
con3.Open();
SqlCommand cmd2 = new SqlCommand(@"SELECT Count(*) FROM Visitors WHERE Id=@id",con3);
cmd2.Parameters.AddWithValue("@id", textBox_VIex.Text);
SqlCommand cmd3 = new SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con3);
cmd3.Parameters.AddWithValue("@id", 1);
cmd3.Parameters.AddWithValue("@dO", DateTime.Now);
cmd3.Parameters.AddWithValue("@tO", DateTime.Now);
int o = cmd3.ExecuteNonQuery();
MessageBox.Show("Good Bye!");
this.Close();
FormCheck f2 = new FormCheck();
f2.Show();
}
catch
{
MessageBox.Show("Error!");
textBox_VIex.Clear();
}
答案 0 :(得分:1)
请参阅我对您的代码的更改
int o = cmd3.ExecuteNonQuery();
返回Query影响的行数。如果它为零则表示id不在数据库中。
try
{
SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
con3.Open();
SqlCommand cmd2 = new SqlCommand(@"SELECT Count(*) FROM Visitors WHERE Id=@id",con3);
cmd2.Parameters.AddWithValue("@id", textBox_VIex.Text);
SqlCommand cmd3 = new SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con3);
cmd3.Parameters.AddWithValue("@id", int.Parse(textBox_VIex.Text));
cmd3.Parameters.AddWithValue("@dO", DateTime.Now);
cmd3.Parameters.AddWithValue("@tO", DateTime.Now);
int o = cmd3.ExecuteNonQuery();
if(o > 0)
MessageBox.Show("Good Bye!");
else
MessageBox.Show("Error!");
this.Close();
FormCheck f2 = new FormCheck();
f2.Show();
}
catch
{
MessageBox.Show("Error!");
textBox_VIex.Clear();
}
答案 1 :(得分:0)
查看代码中的注释。 首先,这是处理连接等的好方法
int? result = null; // note nullable int
try
{
Using (SqlConnection con = New SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True"))
{
con.Open();
// You don't really care to check up front if the record with id=xx exists because
// if it doesn't, you get 0 records updated
// unless you do something with the result of that query
Using (SqlCommandcmd As New SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@id", 1); // <<-- Are you always passing "1"?????
cmd.Parameters.AddWithValue("@dO", DateTime.Now);
cmd.Parameters.AddWithValue("@tO", DateTime.Now);
result = cmd.ExecuteNonQuery();
}
con.Close()
}
}
catch
{
// optionally, log error or show error msg in second msgBox below. Save it to a variable here
}
// Separate query and result processing
// Check the result. If result is still null, your query failed. If not 0 - you updated your records
if (result != null && (int)result > 0)
{
MessageBox.Show("Updated record OK");
}
else
{
if (result == null)
{
MessageBox.Show("The construct failed"); // optionally add error msg here
}
else
{
MessageBox.Show("The record I was looking for is not in DB");
}
}
// I don't know what you do with your form logic
this.Close();