public void bulkInsert(DataTable inputTable) { DAO.DBEngine dbEngine = new DAO.DBEngine(); DAO.Database db = dbEngine.OpenDatabase(FilePath); DAO.Recordset rs = db.OpenRecordset(inputTable.TableName); DAO.Field[] myFields = new DAO.Field[inputTable.Columns.Count]; int k = 0; foreach (DataColumn column in inputTable.Columns) { myFields[k] = rs.Fields[column.ColumnName]; k++; } for (int i = 0; i < inputTable.Rows.Count; i++) { rs.AddNew(); k = 0; foreach (DataColumn column in inputTable.Columns) { Type t = column.DataType; if (t == typeof(string)) { myFields[k].Value = inputTable.Rows[i].Field <string>(k); } else { myFields[k].Value = inputTable.Rows[i].Field <Single>(k); } k++; } rs.Update(); } rs.Close(); db.Close(); }
public bool BulkCopy(IDataReader reader, string tableName, string command = null, SqlBulkCopyOptions options = SqlBulkCopyOptions.Default) { HDAAccessReader accessReader = reader as HDAAccessReader; if (accessReader == null) { CallException("批量拷贝数据到Access数据库失败:BulkCopy 函数中,IDataReader 转 HDAAccessReader 失败!"); return(false); } DAO.DBEngine dbEngine = new DAO.DBEngine(); DAO.Database db = dbEngine.OpenDatabase(DataHelper.DataPath); if (!string.IsNullOrEmpty(command)) { db.Execute(command); } DAO.Recordset rs = db.OpenRecordset(tableName); try { DAO.Field[] myFields = new DAO.Field[accessReader.FieldCount]; myFields[0] = rs.Fields[accessReader.GetName(0)]; myFields[1] = rs.Fields[accessReader.GetName(1)]; myFields[2] = rs.Fields[accessReader.GetName(2)]; while (accessReader.Read()) { rs.AddNew(); myFields[0].Value = accessReader.GetValue(0); myFields[1].Value = accessReader.GetValue(1); myFields[2].Value = accessReader.GetValue(2); rs.Update(); } } catch (Exception e) { CallException(e.Message); return(false); } finally { rs.Close(); db.Close(); } return(true); }
/// <summary> /// 数据批量插入到数据库 /// </summary> /// <param name = "sList" ></ param > public static void add(List <Model.Record> sList) { DAO.DBEngine dbEngine = new DAO.DBEngine(); DAO.Database db = dbEngine.OpenDatabase(FileName); DAO.Recordset rs = db.OpenRecordset("DataSheet"); DAO.Field[] myFields = new DAO.Field[18]; myFields[0] = rs.Fields["serialNumber"]; myFields[1] = rs.Fields["operatorName"]; myFields[2] = rs.Fields["jobNumber"]; myFields[3] = rs.Fields["voltage"]; myFields[4] = rs.Fields["current"]; myFields[5] = rs.Fields["power"]; myFields[6] = rs.Fields["frequency"]; myFields[7] = rs.Fields["energy"]; myFields[8] = rs.Fields["temperature1"]; myFields[9] = rs.Fields["temperature2"]; myFields[10] = rs.Fields["temperature3"]; myFields[11] = rs.Fields["temperature4"]; myFields[12] = rs.Fields["flow1"]; myFields[13] = rs.Fields["flow2"]; myFields[14] = rs.Fields["flow3"]; myFields[15] = rs.Fields["pressure"]; myFields[16] = rs.Fields["heatTime"]; myFields[17] = rs.Fields["coolingTime"]; for (int i = 0; i < sList.Count; i++) { rs.AddNew(); myFields[0].Value = sList[i].serialNumber; myFields[1].Value = sList[i].operatorName; myFields[2].Value = sList[i].jobNumber; myFields[3].Value = sList[i].voltage; myFields[4].Value = sList[i].current; myFields[5].Value = sList[i].power; myFields[6].Value = sList[i].frequency; myFields[7].Value = sList[i].energy; myFields[8].Value = sList[i].temperature1; myFields[9].Value = sList[i].temperature2; myFields[10].Value = sList[i].temperature3; myFields[11].Value = sList[i].temperature4; myFields[12].Value = sList[i].flow1; myFields[13].Value = sList[i].flow2; myFields[14].Value = sList[i].flow3; myFields[15].Value = sList[i].pressure; myFields[16].Value = sList[i].heatTime; myFields[17].Value = sList[i].coolingTime; rs.Update(); } rs.Close(); db.Close(); }
public void UploadDataToAccess(DataTable sourceData, string DBPath, string TblName, bool ClearTbl) { Boolean CheckFl = false; DAO.DBEngine dbEngine = new DAO.DBEngine(); try { DAO.Database db = dbEngine.OpenDatabase(DBPath); DAO.Recordset AccessRecordset = db.OpenRecordset(TblName); DAO.Field[] AccessFields = new DAO.Field[sourceData.Columns.Count]; //Whether to clear table before pasting if (ClearTbl) { db.Execute("DELETE FROM " + TblName); } for (Int32 rowCount = 0; rowCount < sourceData.Rows.Count; rowCount++) { AccessRecordset.AddNew(); for (Int32 colCount = 0; colCount < sourceData.Columns.Count; colCount++) { if (!CheckFl) { AccessFields[colCount] = AccessRecordset.Fields[sourceData.Columns[colCount].ColumnName]; } AccessFields[colCount].Value = sourceData.Rows[rowCount][colCount]; } try { AccessRecordset.Update(); } catch (Exception ex) { MessageBox.Show(ex.ToString() + sourceData.Rows[rowCount][3].ToString()); } CheckFl = true; } AccessRecordset.Close(); db.Close(); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(dbEngine); dbEngine = null; } }
public static int BulkExportToAccess(DataTable dtOutData, String DBPath, String TableNm) { DAO.DBEngine dbEngine = new DAO.DBEngine(); Boolean CheckFl = false; int rowInserted = 0; try { DAO.Database db = dbEngine.OpenDatabase(DBPath); DAO.Recordset AccesssRecordset = db.OpenRecordset(TableNm); DAO.Field[] AccesssFields = new DAO.Field[dtOutData.Columns.Count]; //Loop on each row of dtOutData for (Int32 rowCounter = 0; rowCounter < dtOutData.Rows.Count; rowCounter++) { AccesssRecordset.AddNew(); //Loop on column for (Int32 colCounter = 0; colCounter < dtOutData.Columns.Count; colCounter++) { // for the first time... setup the field name. if (!CheckFl) { AccesssFields[colCounter] = AccesssRecordset.Fields[dtOutData.Columns[colCounter].ColumnName]; } AccesssFields[colCounter].Value = dtOutData.Rows[rowCounter][colCounter]; } AccesssRecordset.Update(); CheckFl = true; rowInserted = rowCounter; } AccesssRecordset.Close(); db.Close(); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(dbEngine); dbEngine = null; } return(rowInserted); }