private void button2_Click(object sender, System.EventArgs e) //登陆access 可以ok
{
string strAccessConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+this.textBox1.Text.ToString();
string strAccessSelect = "SELECT * FROM sfbhb";
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch(Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. /n{0}", ex.Message);
return;
}
try
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(myDataSet,"sfbhb");
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase./n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
this.dataGrid1.DataSource=myDataSet.Tables[0];
}
以上代码用来实现c#程序读入access数据库中的表内容。