创建ACCESS数据库,并且创建表和数据。重点:关闭ACCESS数据库引用

/// <summary>
        /// 创建ACCESS数据库,并且创建表和数据
        /// </summary>
        /// <param name="dictTable"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public int DataTableToAccess(Dictionary<string, DataTable> dictTable, string filePath)
        {
            int count = 0;
            string connStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=5", filePath);
            try
            {
                ADOX.Catalog catalog = new ADOX.Catalog();
                catalog.Create(connStr);   //创建ACCESS数据库

                ADODB.Connection cn = new ADODB.Connection();
                cn.Open(connStr, null, null, -1);
                catalog.ActiveConnection = cn;
                foreach (var item in dictTable)
                {
                    ADOX.Table table = new ADOX.Table();
                    table.Name = item.Key;
                    foreach (DataColumn c in item.Value.Columns)
                    {
                        ADOX.Column column = new ADOX.Column();
                        column.ParentCatalog = catalog;
                        column.Name = c.ColumnName;
                        column.Type = DataTypeEnum.adLongVarWChar;
                        //column.DefinedSize = 9;
                        column.Attributes = ColumnAttributesEnum.adColNullable;    //允许列有NULL值
                        table.Columns.Append(column);
                    }
                    catalog.Tables.Append(table);
                }
                #region 释放COM引用的对象   【该代码块在Release模式下运行有效】
                System.Runtime.InteropServices.Marshal.ReleaseComObject(catalog);
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(catalog);
                catalog = null;
                cn.Close();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }

            ArrayList array = new ArrayList();
            foreach (var item in dictTable)
            {
                StringBuilder sbColumns = new StringBuilder();
                StringBuilder sbValues = new StringBuilder();
                sbColumns.Append("insert into [" + item.Key + "](");
                foreach (DataColumn c in item.Value.Columns)
                {
                    sbColumns.Append("[" + c.ColumnName + "],");
                }
                sbColumns.Remove(sbColumns.Length - 1, 1);
                sbColumns.Append(")");

                foreach (DataRow row in item.Value.Rows)
                {
                    sbValues.Append(sbColumns.ToString());
                    sbValues.Append("values( ");
                    foreach (DataColumn c in item.Value.Columns)
                    {
                        if (row[c.ColumnName] == DBNull.Value)
                        {
                            sbValues.Append("NULL,");
                        }
                        else
                        {
                            sbValues.Append("'" + CommUtils.DtRowFilter(row[c.ColumnName] + "") + "',");
                        }
                    }
                    sbValues.Remove(sbValues.Length - 1, 1);
                    sbValues.Append(");");
                    array.Add(sbValues.ToString());

                    sbValues.Clear();
                }
            }

            DBHelperAccess access = new DBHelperAccess(connStr);
            access.ExecuteSqlTran(array);
            access.Conn.Dispose();
            access = null;
            return count;
        }

 

posted @ 2016-09-27 11:21  Vincent_void  阅读(1194)  评论(0编辑  收藏  举报