Visual Studio 2010 C#中,删除Access数据库中的记录的方法 100
展开全部
直接用SQL删除语句即可
#region 连接字符串
//string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\程序书籍软件\c#程序代码\access数据库操作\addressList.mdb"; //绝对路径
string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Environment.CurrentDirectory+"\\addressList.mdb"; //相对路径
#endregion
#region 删除
private void tsbDelete_Click(object sender, EventArgs e)
{
delete();
}
#endregion
#region 删除语句
private void delete()
{
try
{
int[] rowCount = this.gridView1.GetSelectedRows();
string strSql = "";
foreach (int i in rowCount)
{
this.gridView1.FocusedRowHandle = -1;
string strID = "";
strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString();
strSql = "delete from new1 where ZMID=" + strID + ""; //注意where处没有单引号
}
OleDbConnection connet1 = new OleDbConnection(strcon);
connet1.Open();
OleDbCommand cmd = new OleDbCommand(strSql, connet1);
int a = cmd.ExecuteNonQuery();
connet1.Close();
if (a > 0)
{
MessageBox.Show("删除成功");
}
query();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#region 连接字符串
//string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\程序书籍软件\c#程序代码\access数据库操作\addressList.mdb"; //绝对路径
string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Environment.CurrentDirectory+"\\addressList.mdb"; //相对路径
#endregion
#region 删除
private void tsbDelete_Click(object sender, EventArgs e)
{
delete();
}
#endregion
#region 删除语句
private void delete()
{
try
{
int[] rowCount = this.gridView1.GetSelectedRows();
string strSql = "";
foreach (int i in rowCount)
{
this.gridView1.FocusedRowHandle = -1;
string strID = "";
strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString();
strSql = "delete from new1 where ZMID=" + strID + ""; //注意where处没有单引号
}
OleDbConnection connet1 = new OleDbConnection(strcon);
connet1.Open();
OleDbCommand cmd = new OleDbCommand(strSql, connet1);
int a = cmd.ExecuteNonQuery();
connet1.Close();
if (a > 0)
{
MessageBox.Show("删除成功");
}
query();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询