1. 程式人生 > >C#Winform連線並訪問Access資料庫

C#Winform連線並訪問Access資料庫

C#Winform連線並訪問Access資料庫

Access新建了一個名為user的資料庫,其中有張名為UserInfor的表。將之添入專案中後,訪問資料表中資料。這樣做:

OleDbConnection mycon = null;
OleDbDataReader myReader = null;
string str = "";
try
{
  string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=user.mdb;";
  mycon = new OleDbConnection(strcon);
  mycon.Open();
  string
sql = "SELECT * FROM UserInfor"; OleDbCommand mycom = new OleDbCommand(sql, mycon); myReader = mycom.ExecuteReader(); while (myReader.Read()) { str += myReader.GetString(0) + " " + myReader.GetString(1); } MessageBox.Show(str); } finally { myReader.Close(); mycon.Close(); }