string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = XXX.mdb"; //Data Source = 数据库绝对路径
OleDbConnection myConnection = new OleDbConnection(myConnectionString);OleDbCommand myCommand = new OleDbCommand( select count(*) as maxrow from table", myConnection);myConnection.Open();OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
myReader.Read();//因为只有一行数据,所以不用循环读取.
if(dbReader["maxrow"].ToString()!="")//如果有数据
{
OleDbConnection myConnection = new OleDbConnection(myConnectionString); OleDbCommand myCommand = new OleDbCommand( "delete login where name="+txtName.text, myConnection); myCommand.Connection.Open(); myCommand.ExecuteNonQuery();//返回影响的行数
myConnection.Close();
//弹出删除成功提示
}
else
{
//弹出提示
}
myReader.Close();
大体思路
OleDbConnection myConnection = new OleDbConnection(myConnectionString);OleDbCommand myCommand = new OleDbCommand( select count(*) as maxrow from table", myConnection);myConnection.Open();OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
myReader.Read();//因为只有一行数据,所以不用循环读取.
if(dbReader["maxrow"].ToString()!="")//如果有数据
{
OleDbConnection myConnection = new OleDbConnection(myConnectionString); OleDbCommand myCommand = new OleDbCommand( "delete login where name="+txtName.text, myConnection); myCommand.Connection.Open(); myCommand.ExecuteNonQuery();//返回影响的行数
myConnection.Close();
//弹出删除成功提示
}
else
{
//弹出提示
}
myReader.Close();
大体思路
{
OleDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = test.mdb");
con.Open();
OleDbCommand cmd = new OleDbCommand(string.Format("select Count(*) form ClassInfo where ClaName = '{0}'", textBox.Text), con);
if ((int)cmd.ExecuteScalar() > 0)
{
string sql = "delete ClassInfo where name='"+ textBox.Text + "')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
MessageBox.Show("删除成功!");
}
else
{ MessageBox.Show("此人不存在!", "提示");
}
con.Close();
}
ExecuteScalar是执行结果
作也判断
最后再次用OleDbCommand操作数据库进行删除
{
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
OleDbCommand myCommand = new OleDbCommand("delete login where [姓名]=" + textBoxDeleteName.Text, myConnection);
这的myConnection和myCommand
是不是把变量改了?另外,dbReader显示上下文不存在。大神,看看怎么办,我是新手,懂得不太多!
用这个吧。这个简单些。
dbReader=》myReader myConnection这个你是不是在外面定义了?
http://msdn.microsoft.com/zh-cn/library/system.data.oledb.oledbconnection.aspx