C#和VB.NET连接Access
一、直接通过软件连接Access表
1:新建一个windowsForm工程。
2:打开"检视\伺服器总管\资料连接"后,右击"资料连接"后打开"连接资料连接",选择正确的资料来源与资料库档名,若有密码请输入正确的密码,如无,则不输入。
3:点击"测试连接"后,显示连接成功。
4:添加一个DataGridView控件,点击Data Source属性,选择"Add Project Data Source",然后找到添加的Access表"Administrator"。如下图所示:
5:点击调试,及显示其表"Administrator"的全部。
二、C#连接Access数据库文件
1:通过OLEDB的Connection来连接数据库文件,具体代码如下:
/// <summary>
///连接数据库文件
/// </summary>
/// <param name="intFlag">连接标志位(0:普通方式打开;1:以独占方式打开)</param>
/// <param name="path">数据库文件的路径</param>
/// <returns>OleDbConnection</returns>
private static OleDbConnection getConnection(int intFlag,string path)
{
string strFilePath = null;//数据库文件路径
string strConn = null;//打开数据库语句
//普通方式打开
if (intFlag == 0)
{
//strFilePath = Application.StartupPath + @"\db\Database.mdb";
strFilePath = path;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";";
}
//以独占方式打开
if (intFlag == 1)
{
strFilePath = Application.StartupPath + @"\db\Database_1.mdb";
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";" +
"Jet OLEDB:Database Password=123;";
}
OleDbConnection oleConn = new OleDbConnection(strConn);
return oleConn;
}
2:成功连接数据库文件后,需要读取里面的内容并显示出来,其代码如下:
/// <summary>
///查看数据库文件并填充
/// </summary>
/// <param name="intFlag">填充标志位(0:DataGridView控件;1:ListView控件)</param>
/// <param name="path">数据库文件路径</param>
/// <param name="obj">填充控件</param>
public void DataFille(int intFlag,string path ,object obj)
{
OleDbConnection oleConn = getConnection(intFlag, path);
if (oleConn.State == ConnectionState.Open)//判断数据库是否关闭
{
oleConn.Close();
}
oleConn.Open();
string strSelect = "select * from Administrator";//查询语句(此句表示返回整张表)
if (intFlag == 0)
{
DataGridView dv = (DataGridView)obj; //拆箱操作
OleDbDataAdapter da = new OleDbDataAdapter(strSelect, oleConn);//建立适配器,通过SQL语句去搜索数据库
DataSet ds = new DataSet();
//建立数据集
da.Fill(ds, "0");
DataTable dt = ds.Tables["0"];
dv.DataSource = dt;//把表dt显示到DataGridView控件上
}
if (intFlag == 1)
{
ListView lv = (ListView)obj;
OleDbCommand oleCmd = new OleDbCommand(strSelect, oleConn);//执行的预存式
OleDbDataReader oleRed = oleCmd.ExecuteReader();//执行查询语句,并读取相应的数据
string[] str = new string[oleRed.FieldCount];
lv.Columns.Clear();//清除列名
lv.Items.Clear();//清除表格内容
//添加列名
for (int i = 0; i < oleRed.VisibleFieldCount; i++)//获取列名
{
lv.Columns.Add(oleRed.GetName(i));
}
while (oleRed.Read())
{
ListViewItem item = new ListViewItem(oleRed[0].ToString());//从第1行开始添加
for (int i = 1; i < oleRed.FieldCount; i++)
{
item.SubItems.Add(oleRed[i].ToString());//添加剩余行
}
lv.Items.Add(item);//添加到listview控件上
}
}
三、VB.NET连接Access数据库文件
''' <summary>
''' 查看数据库
''' </summary>
''' <param name="filepath">数据库文件的路径</param>
''' <param name="obj">填充控件内容</param>
''' <param name="flag">填充标志位(0:DataGridView控件;1:listview控件)</param>
''' <param name="password">数据库文件密码</param>
''' <returns>flase:打开失败;true:打开成功</returns>
''' <remarks></remarks>
Public Function DataFille(ByVal filepath As String, ByVal obj As Object, Optional ByVal flag As Integer = 0, Optional ByVal password As String = Nothing) As Boolean
Dim bolReturn As Boolean = False
Try
'连接数据库
Dim oleCon As OleDbConnection = getConnection(filepath)
If (oleCon.State = ConnectionState.Open) Then
oleCon.Close()
End If
oleCon.Open()
'查询语句
Dim strSelect As String = "select * form Administrator"
'以DataGridView填充
If flag = 0 Then
Dim dgv As DataGridView = obj
'执行语句
Dim da As OleDbDataAdapter = New OleDbDataAdapter(strSelect, oleCon)
'声明连接池
Dim ds As DataSet = New DataSet()
'填充数据集
da.Fill(ds, "0")
'填充到表dt中
Dim dt As DataTable = ds.Tables("0")
'填充控件
dgv.DataSource = dt
End If
'以listview填充
If flag = 1 Then
Dim lv As ListView = obj
'执行语句
Dim oleCmd As OleDbCommand = New OleDbCommand(strSelect, oleCon)
'读取语句
Dim oleRed As OleDbDataReader = oleCmd.ExecuteReader()
'清除控件内容
lv.Columns.Clear()
lv.Items.Clear()
'添加字段名
For i = 0 To oleRed.VisibleFieldCount - 1
lv.Columns.Add(oleRed.GetName(i))
Next
'添加数据表内容
While (oleRed.Read())
Dim item As ListViewItem = New ListViewItem(oleRed(0).ToString())
For i = 0 To oleRed.FieldCount - 1
item.SubItems.Add(oleRed(i).ToString())
Next
lv.Items.Add(item)
End While
End If
bolReturn = True
Catch ex As Exception
bolReturn = False
End Try
Return bolReturn
End Function
作者:dongweian