c# datatable批量更新到oracle数据库
直接看代码:
public static int OracleCopyDataTable(string table,DataTable dt, OracleConnection connOracle)
{
try
{
string strfields = "";
string strvalues = "";
string filed = "";
string strvalue = "";
int count = 0;
OracleDataAdapter da = new OracleDataAdapter();
OracleCommandBuilder ob = new OracleCommandBuilder(da);
da.InsertCommand = new OracleCommand();
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(","));
}
da.SelectCommand = new OracleCommand();
da.SelectCommand.Connection = connOracle;
da.SelectCommand.CommandText = "select id," + strfields + " from " + table;
da.InsertCommand.CommandText = "insert into " + table + "(" + strfields + ") values (" + strvalues + ")";
for (int i = 0; i < dt.Columns.Count; i++)
{
filed = dt.Columns[i].ColumnName;
strvalue = "@" + filed;
OracleParameter oparam = new OracleParameter();
oparam.ParameterName = strvalue;
oparam.SourceVersion = DataRowVersion.Current;
oparam.SourceColumn = filed;
da.InsertCommand.Parameters.Add(oparam);
}
count = da.Update(dt);
connOracle.Close();
return count;
}
catch(Exception ex)
{
string aa = ex.Message;
return 0;
}
}
oracleconnection 在传过来的时候是打开的,也就是说状态是open,现在是我的datatable的数据是从SQL数据库取出来的,在oracle数据库当中有一个自动增长列,我不知道自动增长列的值应该是多少,所以没有给赋值,在执行da.update(dt)的时候,报DataTable“Table”中缺少 SourceColumn“ID1”的 DataColumn“ID1”。的错误,我应该怎么去解决这个问题?
------解决方案--------------------
话说看的不是很明白,是不是想获得自增id的值?
在mssql里有个全局变量IDENTITY和函数可以获得自增id
我想在oracle里应该也有类似的东西