数据库access的连接
private void button1_Click(object sender, EventArgs e){
OleDbConnection strConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "SQLTDDC.mdb" + ";Persist Security Info=False");
//建立数据库引擎连接,注意数据表(后缀为.db)应放在DEBUG文件下
OleDbDataAdapter myda = new OleDbDataAdapter("select * from fbf" ,strConnection);
//建立适配器,通过SQL语句去搜索数据库
DataSet myds = new DataSet();
//建立数据集
myda.Fill(myds, "fbf");
//用FILL的方式将适配器已经连接好的数据表填充到数据集MYDS这张表
dataGridView1.DataSource = myds.Tables["yzbm"];
//用显示控件来显示表
}
各位大大~晚上好
这是我百度来的代码~,我也把mdb文件放到debug目录下了~但是 点按钮的时候就是没反应~求解~
Try
dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source=" & dbFileDir
Using db_AccessCon As OleDbConnection = New OleDbConnection(dbProvider & dbSource)
db_AccessCon.Open()
Using Access_Dataset As DataSet = New DataSet
Using Access_CMD As OleDbCommand = New OleDbCommand(cmdString, db_AccessCon)
Access_CMD.CommandTimeout = ProcessTime
Using Access_Adapter As OleDbDataAdapter = New OleDbDataAdapter(Access_CMD)
Using EMBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(Access_Adapter)
Access_Adapter.Fill(Access_Dataset, cmdTable) ' Access_Adapter.FillSchema(AccessDataset, SchemaType.Source)
End Using
End Using
End Using
Return Access_Dataset.Tables(0)
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, ErrorString & " Error")
Return Nothing
Finally
End Try
页:
[1]