今天因为项目需要,要做个小工具,把txt里的内容保存到access表中去,折腾了半天,历经曲折艰辛,终于把问题解决了,现在把代码写下,留作后用。
一、创建表结构
/// <summary>
/// 创建mdb数据库,数据表及字段
/// </summary>
/// <param name="strAccessFileName">mdb文件名</param>
/// <param name="strTableName">表名</param>
/// <param name="listFieldName">字段列表</param>
private void CreateAccessDBAndTable(string strAccessFileName,string strTableName,List<string> listFieldName)
{
CatalogClass cat = new CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source="+strAccessFileName+";Jet OLEDB:Engine Type=5");
TableClass tableClass = new TableClass();
tableClass.ParentCatalog = cat;
tableClass.Name = strTableName;
//添加字段列表
if(listFieldName != null && listFieldName.Count > 0)
{
foreach(string strFieldName in listFieldName)
{
ColumnClass column = new ColumnClass();
column.ParentCatalog = cat;
column.Name = strFieldName;
//column.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
column.Attributes = ColumnA