zoukankan      html  css  js  c++  java
  • 创建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;
            }
  • 相关阅读:
    WPF中StringToImage和BoolToImage简单用法
    linux定时执行任务
    进程管理
    防火墙和网络安全
    ICMP
    茶叶知识
    插花、花艺、种植
    AD域中添加了一个策略导致的问题
    写一个工具,将批量的“规章制度”文件上传到广西门户网站对应的文档库中
    SharePoint Online 创建门户网站系列之定制栏目
  • 原文地址:https://www.cnblogs.com/vincentvoid/p/5912150.html
Copyright © 2011-2022 走看看