C#链接操作Access数据库

C#链接操作Access数据库

引用 system.Data.OleDb

//Access_path为Access数据库的路径  Access数据库要保存为.mdb格式(2013版默认为.accdb格式)
OleDbConnection Access_Con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Access_Path);
OleDbCommand Access_Com = Sino_AccessCon.CreateCommand();
Access_Com.CommandText = "select * from "+ Table_Name +" where User_Name='" + UserName +"'";
Access_Con.Open();
OleDbDataReader Access_Read = Access_Com.ExecuteReader();
Access_Con.Close();

比如登陆账号密码为防止数据泄露时可以写成:

Access_Com.CommandText = "select * from " + Table_Name + " where User_Name='" + UserName + "'AND User_Key='" + UserKey + "'";
Access_Con.Open();
OleDbDataReader Access_Read = Access_Com.ExecuteReader();
if (Access_Read.HasRows)
{
    MessageBox.Show("可以登陆");
}

执行增删改命令可以直接写为:

Access_Con.Open();
OleDbCommand Access_Com = new OleDbCommand("命令语句", Access_Con);
Sino_AccessCom.ExecuteNonQuery();

存储read的内容,举个name的栗子:

OleDbConnection Access_Con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Access_Path);
OleDbCommand Access_Com = Sino_AccessCon.CreateCommand();
Access_Com.CommandText = "select * from "+ Table_Name ;
Access_Con.Open();
OleDbDataReader Access_Read = Access_Com.ExecuteReader();
List Temp_List=new List();
while (Sino_AccessRead.Read())
{
    Temp_List.Add(Sino_AccessRead["User_Name"].ToString());
}
Access_Con.Close();

你可能感兴趣的:(C#)