/// <summary> ///Create an Access database, and create tables and data/// </summary> /// <param name= "dicttable" ></param> /// <param name= "FilePath" ></param> /// <returns></returns> Public intDatatabletoaccess (dictionary<string, Datatable> dicttable,stringFilePath) { intCount =0; stringConnStr =string. Format ("Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}; Jet Oledb:engine type=5", FilePath); Try{ADOX. Catalog Catalog=NewADOX. Catalog (); Catalog. Create (CONNSTR); //Create an Access databaseADODB. Connection cn=NewADODB. Connection (); cn. Open (ConnStr,NULL,NULL, -1); Catalog. ActiveConnection=cn; foreach(varIteminchdicttable) {ADOX. Table Table=NewADOX. Table (); Table. Name=item. Key; foreach(DataColumn Cinchitem. value.columns) {table. Columns.Append (C.columnname, Datatypeenum.adlongvarwchar); } catalog. Tables.append (table); } #regionRelease COM-referenced object "This code block runs in release mode is valid"System.Runtime.InteropServices.Marshal.ReleaseComObject (catalog); System.Runtime.InteropServices.Marshal.FinalReleaseComObject (catalog); Catalog=NULL; cn. Close (); Gc. WaitForPendingFinalizers (); Gc. Collect (); #endregion } Catch(Exception ex) {Throwex; } foreach(varIteminchdicttable) {StringBuilder Sbcolumns=NewStringBuilder (); StringBuilder sbvalues=NewStringBuilder (); Sbcolumns.append ("INSERT INTO ["+ Item. Key +"]("); foreach(DataColumn Cinchitem. Value.columns) {sbcolumns.append ("["+ C.columnname +"],"); } sbcolumns.remove (Sbcolumns.length-1,1); Sbcolumns.append (")"); foreach(DataRow rowinchitem. Value.rows) {sbvalues.append (sbcolumns.tostring ()); Sbvalues.append ("VALUES ("); foreach(DataColumn Cinchitem. Value.columns) {sbvalues.append ("'"+ Commutils.dtrowfilter (Row[c.columnname] +"") +"',"); } sbvalues.remove (Sbvalues.length-1,1); Sbvalues.append (");"); Sbvalues.appendline (""); //using (dbhelperaccess access = new Dbhelperaccess (connstr))//{dbhelperaccess Access =Newdbhelperaccess (CONNSTR); //dbhelperaccess access = new Dbhelperaccess (CONNSTR);access. Executesqlnonquery (Sbvalues.tostring ()); Access. Conn.dispose (); Access=NULL; //}sbvalues.clear (); } GC. Collect (); } returncount; }
The
Creates an Access database and creates tables and data. Important: Close the Access database reference