读取的MDB格式如下:
1,MDB操作类如下 :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
public class MDBHelp
{
private string _fileName;
private string _connectionString;
private OleDbConnection _odcConnection;
///
/// 构建函数
///
/// MDB文件(含完整路徑)
public MDBHelp(string fileName)
{
this._fileName = fileName;
this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";
}
///
/// 建立连接(打开数据库文件)
///
public void Open()
{
try
{
// 建立连接
this._odcConnection = new OleDbConnection(this._connectionString);
// 打开连接
this._odcConnection.Open();
}
catch (Exception)
{
throw new Exception("嘗試打开 " + this._fileName + " 失敗, 請確認文件是否存在!");
}
}
///
/// 断开连接(关闭据库文件)
///
public void Close()
{
this._odcConnection.Close();
}
///
/// 根据sql命令返回一个DataSet
///
/// sql命令
///
public DataSet GetDataSet(string sql)
{
DataSet ds = new DataSet();
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, this._odcConnection);
adapter.Fill(ds);
}
catch (Exception)
{
throw new Exception("sql語句: " + sql + " 執行失敗!");
}
return ds;
}
}
}
下面是执行方法:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
var a = "\"天才\"";
string mdbFile = @"C:\Users\Admin\Desktop\JSYD.mdb"; //默認路徑
var msg = "";
List
MDBHelp mdbHelp = new MDBHelp(mdbFile);
try
{
mdbHelp.Open(); // 打開數據庫
var d = new Dictionary
var ds1 = mdbHelp.GetDataSet("select * from BL_BUILDLANDITEM ");
var ColumnsList = ds1.Tables[0].Columns;
foreach (DataRow item in ds1.Tables[0].Rows)
{
msg = " d = new Dictionary foreach (DataColumn item1 in ColumnsList) { var ColumnName = item1.ColumnName; d.Add(ColumnName, item[ColumnName].ToString()); } foreach (var item3 in d) { msg += string.Format("{0}=\"{1}\" ", item3.Key,item3.Value); } msg += ">
msgresult.Add(msg);
}
mdbHelp.Close(); // 關閉數據庫
}
finally
{
mdbHelp = null;
}
}
}
}
最终得到的结果如下: