1、执行查询操作:(ExecuteReader方法)
string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "+Server.MapPath("~/") +"App_Data/access.mdb"; //使用相对路径连接数据库
string mySelectQuery = "select * from login where userName ='lwjj'";//查询语句
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
//GetString(0,1,2....)
}
myReader.Close();
2、执行增、删、改操作:(ExecuteNonQuery方法)
private void dbcz()
{
string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "+Server.MapPath("~/") +"App_Data/IPTJ.mdb"; //Data Source = 数据库绝对路径
{
string myConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "+Server.MapPath("~/") +"App_Data/IPTJ.mdb"; //Data Source = 数据库绝对路径
string myExecuteQuery = "insert into accIP (IP,地址) values ('" + 参数1+ "','" + 参数2+ "')";//增、删、改等语句
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
OleDbCommand myCommand = new OleDbCommand(myExecuteQuery,myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();//返回影响的行数
myConnection.Close();
}
原文地址: