C#,将DataTable中的数据批量导入到Oracle数据库中
我尝试着用SQLServer的方法批量导入了一下,但是Update方法不执行,也不报异常,我的代码如下:
private OracleDataAdapter odad; public static int InsertBatch(DataTable dt, List<OracleType> DbTypeList,string strtable) { getConnection(); int count = 0; string strfields = ""; string strvalues = ""; for (int i = 0; i < dt.Columns.Count; i++) { string strfiled = dt.Columns[i].ColumnName; strfields += strfiled + ","; strvalues += "@" + strfiled + ","; } if (strfields != "") { strfields = strfields.Substring(0, strfields.LastIndexOf(",")); strvalues = strvalues.Substring(0, strvalues.LastIndexOf(",")); } try { if (ocon.State != ConnectionState.Open) ocon.Open(); odad = new OracleDataAdapter(); //建立InsertCommand StringBuilder sb = new StringBuilder(""); sb.Append("INSERT into " + strtable + " (" + strfields + ") VALUES("); sb.Append(strvalues + ")"); odad.InsertCommand = new OracleCommand(); odad.InsertCommand.CommandText = sb.ToString(); odad.InsertCommand.Connection = ocon; for (int i = 0; i < dt.Columns.Count; i++) { string filed = dt.Columns[i].ColumnName; string strvalue = "@" + filed; OracleParameter oparam = new OracleParameter(); oparam.ParameterName = strvalue; oparam.OracleType = DbTypeList[i]; oparam.SourceVersion = DataRowVersion.Current; oparam.SourceColumn = filed; odad.InsertCommand.Parameters.Add(oparam); } count = odad.Update(dt); } catch (Exception ex) { count = 0; } finally { if (ocon != null) ocon.Close(); } return count; }