access数据库用C#语言如何连接VIsual Studio2010
- 提问者网友:欺烟
- 2021-04-28 14:53
- 五星知识达人网友:动情书生
- 2021-04-28 16:20
首先要添加引用代码,(在这用的是OLE DB驱动实现的,使用该驱动需要添加该驱动引用)
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Reflection;
下面是要打开mydb文件代码
DataSet ds = new DataSet();
//定义连接字符串
string strFilePath = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + "\\MyDB.mdb";
//定义连接数据源
string sql = "select * from 序号";
//声明一个数据连接
System.Data.OleDb.OleDbConnection con = new OleDbConnection(strFilePath);
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
- 1楼网友:动情书生
- 2021-04-28 18:48
using System; using System.Data; using System.Data.OleDb;
namespace DataViewApp { class DataViewExample { static void Main() { string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\C#Program\C#100\Chapter3\NWIND.mdb"; OleDbConnection Conn = new OleDbConnection(conStr);
try { string SQL = "SELECt ContactName, Country FROM Customers"; OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = new OleDbCommand(SQL,Conn); DataSet ds = new DataSet(); adapter.Fill(ds, "Customers");
DataTable datatable = ds.Tables["Customers"];
DataView myView = new DataView(datatable, "Country = 'Germany'", "Country", DataViewRowState.CurrentRows);
foreach (DataRowView myDrv in myView) { for (int i = 0; i < myView.Table.Columns.Count; i++) Console.Write(myDrv[i] + "\t"); Console.WriteLine(); } } catch(Exception ex) { Console.WriteLine("Error Orccured: " + ex.Message); Conn.Close(); } finally { Conn.Close(); Console.ReadLine(); } } } }
- 2楼网友:千杯敬自由
- 2021-04-28 17:25
string sql="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=数据库名.mdb;Persist Security Info=False";
以上为联接字符串
OLEDBSqlConnection conn=new OLEDBSqlConnection(sql);
注意大小写可能有错误