删除选定的任何记录,但不删除当前记录。 C#
delete any record selected but not the current one. c#
private void button4_Click(object sender, EventArgs e)
{
myConn.Open();
string query = "Delete * from tbl_attendance where [attendance_id] =" + textBox1.Text + "";
OleDbCommand cmd = new OleDbCommand();
string message = "Are you sure you want to delete this data?";
string caption = "Delete";
var result = MessageBox.Show(message, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
if (result == DialogResult.OK)
{
cmd.Connection = myConn;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully deleted recorded data");
button4.Visible = false;
button2.Visible = true;
label5.Visible = false;
textBox1.Visible = false;
textBox1.Text = "";
myConn.Close();
try
{
myConn.Open();
OleDbCommand cmd2 = new OleDbCommand();
cmd2.Connection = myConn;
string query2 = "Select attendance_id as [ID], username as [Username], time_in as [Time In],time_out as [Time Out] from tbl_attendance";
cmd.CommandText = query2;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
myConn.Close();
}
else
{
this.DialogResult = DialogResult.Cancel;
button4.Visible = false;
button2.Visible = true;
label5.Visible = false;
textBox1.Visible = false;
textBox1.Text = "";
}
myConn.Close();
}
我不确定在什么地方插入一条语句,最后插入到may数据库的数据不会被删除。就像,可以删除我的 datagridview 中的任何选定记录,但不能删除当前记录,因为它是记录的最新时间。请帮忙。谢谢。
注意事项:
没有你的数据库我无法测试这个
你的代码结构不合理,我不会尝试更正这个
您正在使用访问权限。差
我只是想写 selects 等而不是完整的代码
1: select 您要删除的行
string id = textBox1.Text;
deleterow = "Select * from tbl_attendance where id=" + id;
2 :获取用户在删除行上的最长时间
maxtime = "select max(timein) from tbl_attendance where username='" + deleterow["username"] + "'";
3: 获取用户的行和最长时间
lastrows = "select * from tbl_attendance where username='" + deleterow["username" + "' and timein='" + maxtimein + "'";
4: 将最后一行 id 与您尝试删除的行进行比较
if (lastrows["id] == id)
{
//dont delete!
}
private void button4_Click(object sender, EventArgs e)
{
myConn.Open();
string query = "Delete * from tbl_attendance where [attendance_id] =" + textBox1.Text + "";
OleDbCommand cmd = new OleDbCommand();
string message = "Are you sure you want to delete this data?";
string caption = "Delete";
var result = MessageBox.Show(message, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
if (result == DialogResult.OK)
{
cmd.Connection = myConn;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully deleted recorded data");
button4.Visible = false;
button2.Visible = true;
label5.Visible = false;
textBox1.Visible = false;
textBox1.Text = "";
myConn.Close();
try
{
myConn.Open();
OleDbCommand cmd2 = new OleDbCommand();
cmd2.Connection = myConn;
string query2 = "Select attendance_id as [ID], username as [Username], time_in as [Time In],time_out as [Time Out] from tbl_attendance";
cmd.CommandText = query2;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
myConn.Close();
}
else
{
this.DialogResult = DialogResult.Cancel;
button4.Visible = false;
button2.Visible = true;
label5.Visible = false;
textBox1.Visible = false;
textBox1.Text = "";
}
myConn.Close();
}
我不确定在什么地方插入一条语句,最后插入到may数据库的数据不会被删除。就像,可以删除我的 datagridview 中的任何选定记录,但不能删除当前记录,因为它是记录的最新时间。请帮忙。谢谢。
注意事项:
没有你的数据库我无法测试这个
你的代码结构不合理,我不会尝试更正这个
您正在使用访问权限。差
我只是想写 selects 等而不是完整的代码
1: select 您要删除的行
string id = textBox1.Text;
deleterow = "Select * from tbl_attendance where id=" + id;
2 :获取用户在删除行上的最长时间
maxtime = "select max(timein) from tbl_attendance where username='" + deleterow["username"] + "'";
3: 获取用户的行和最长时间
lastrows = "select * from tbl_attendance where username='" + deleterow["username" + "' and timein='" + maxtimein + "'";
4: 将最后一行 id 与您尝试删除的行进行比较
if (lastrows["id] == id)
{
//dont delete!
}