gpt4 book ai didi

使用mysql注销的C#更新代码

转载 作者:行者123 更新时间:2023-11-29 11:00:00 24 4
gpt4 key购买 nike

我正在执行更新语句,其中日期时间选择器(注销)将插入到与登录相同的行中,但当我注销时它会生成另一行,这里是链接:http://imgur.com/a/rAWhi

ps。这里的问题是注销按钮插入到另一行中..但我想将其插入到同一行中。

这是我的代码:

  private void button1_Click(object sender, EventArgs e)
{
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from empinfo where username = '" + label4.Text + "' and IDNUMBER = '" + textBox1.Text + "' ";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());


if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Input your id number");


}

else if (i == 0)
{

MessageBox.Show("Username and IDNUMBER didn't match.", "Log-In Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


}
else
{
updateuser();
login frmm = new login();
frmm.Show();
this.Close();

}
con.Close();



}
public void updateuser()
{
MySqlConnection cnn = new MySqlConnection(mysqlAddress);
MySqlCommand cmdupdate;
cnn.Open();
try
{
cmdupdate = cnn.CreateCommand();
cmdupdate.CommandText = "update employee set logout = @logout";
cmdupdate.CommandText = "Insert into employee (logout) values (@logout)";
cmdupdate.Parameters.AddWithValue("@logout", dateTimePicker1.Value);

cmdupdate.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
if (cnn.State == ConnectionState.Open)
{
cnn.Close();

MessageBox.Show("Data has been saved");
}
}
}

最佳答案

正如@Ben在评论中指出的那样,您不需要使用插入而只想更新,因此请像这样更改代码:

try
{
cmdupdate = cnn.CreateCommand();
cmdupdate.CommandText = "update employee set logout=@logout";
cmdupdate.CommandText += "WHERE IDNUMBER=@IDNUMBER";
cmdupdate.Parameters.AddWithValue("@IDNUMBER", textBox1.Text.Trim());
cmdupdate.Parameters.AddWithValue("@logout", dateTimePicker1.Value);

cmdupdate.ExecuteNonQuery();
}

Where 是为了确保它只更新文本框中的 IDNUMBER。

关于使用mysql注销的C#更新代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42429342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com