读取的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;
/// <summary>
/// 构建函数
/// </summary>
/// <param name="fileName">MDB文件(含完整路徑)</param>
public MDBHelp(string fileName)
{
this._fileName = fileName;
this._connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";";
}
/// <summary>
/// 建立连接(打开数据库文件)
/// </summary>
public void Open()
{
try
{
// 建立连接
this._odcConnection = new OleDbConnection(this._connectionString);
// 打开连接
this._odcConnection.Open();
}
catch (Exception)
{
throw new Exception("嘗試打开 " + this._fileName + " 失敗, 請確認文件是否存在!");
}
}
/// <summary>
/// 断开连接(关闭据库文件)
/// </summary>
public void Close()
{
this._odcConnection.Close();
}
/// <summary>
/// 根据sql命令返回一个DataSet
/// </summary>
/// <param name="sql">sql命令</param>
/// <returns>以DataTable形式返回数据</returns>
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<string> msgresult = new List<string>();
MDBHelp mdbHelp = new MDBHelp(mdbFile);
try
{
mdbHelp.Open(); // 打開數據庫
var d = new Dictionary<string, string>();
var ds1 = mdbHelp.GetDataSet("select * from BL_BUILDLANDITEM ");
var ColumnsList = ds1.Tables[0].Columns;
foreach (DataRow item in ds1.Tables[0].Rows)
{
msg = "<ROW ";
d = new Dictionary<string, string>();
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 += "></ROW>";
msgresult.Add(msg);
}
mdbHelp.Close(); // 關閉數據庫
}
finally
{
mdbHelp = null;
}
}
}
}
最终得到的结果如下:
<ROW PRO_ID="245c7a27-1093-4840-a1f3-f55f2ff7" PRO_CODE="TZ20190209" PRO_NAME="地块06" PRO_TYPE="" PRO_LANDUSEUNIT="华硕家具" PRO_SPACE="车街道" SUPPLY_TYPE="" REGION_CODE="3382" REMARK="" TOTALAREA="0.586" LANDAREA11="0" SUB_CODE="" LANDAREA1="0.586" PLAN_PROJ="" LAND_TYPE="2" LAND_NUM="" BP_TYPE="" CREATE_TIME="2019/3/28 8:43:51" TXM="3211831801123" ></ROW>
网友评论