用C#语言从ACCESS数据库中删除一个表格的内容怎么写啊
展开全部
#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);
}
}
//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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询