日期:2014-05-18 浏览次数:20553 次
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;
}