我一直在寻找,但还没有找到我需要完成的一个很好的例子。标题说明了一切。这就是我到目前为止所做的:
//gather the report details
DataTable dtReturn = new DataTable();
DataTable dtResults = new DataTable();
string strScript = "";
bool doReturnData = false;
try
{
//this function returns my SQL table in to a datatable dtReturn:
var ii =
Utility.db.Connection.EstablishDBConnection("usp_get_data_recap", dtReturn, null, null, true, 60);
//Clear out table before insert
dtResults = OracleDataPull(9, "TRUNCATE TABLE user.SIGNUP_1");
OracleParameter myparam = new OracleParameter();
OracleCommand mycommand = new OracleCommand();
int n;
//bulk insert to the signup_1 table from the datatable members. Datatable contains the same 4 fields being inserted in to signup_1 on the oracle side:
mycommand.CommandText = "INSERT INTO user.SIGNUP_1 ([ID], [ACCOUNT_NUMBER], [MAIN_CUSTOMER], [SIGNUP_DATE]) VALUES(?)";
mycommand.Parameters.Add(myparam);
for (n = 0; n < 100000; n++)
{
[what do i do here?]
}
}
我不确定这是否正确,或者是否有更简单的方法,但我需要将dtReturn.Rows [n] [0-3]分别映射到ID,account_number,main_customer和signup_date。
帮助很贴心!提前谢谢!
编辑:
我尝试了下面的建议,但是lambda表达式出错了: &#34;无法将lambda表达式转换为&#39; string&#39;因为它不是代表类型&#34; :
var ii =
Utility.db.Connection.EstablishDBConnection("usp_get_data", dtReturn, null, null, true, 60);
dtResults = OracleDataPull(9, "TRUNCATE TABLE user.PR_data");
OracleParameter myparam = new OracleParameter();
OracleCommand mycommand = new OracleCommand();
mycommand.ArrayBindCount = dtReturn.Rows.Count;
mycommand.Parameters.Add(":myparam", OracleDbType.Varchar2, dtReturn.Select(c => c.myparam).ToArray(), ParameterDirection.Input);
mycommand.ExecuteNonQuery();
int n;
mycommand.CommandText = "INSERT INTO user.PR_data ([ID], [ACCOUNT_NUMBER], [MAIN_CUSTOMER], [SIGNUP_DATE]) VALUES(?)";
mycommand.Parameters.Add(myparam);
for (n = 0; n < 100000; n++)
{
myparam.Value = n + 1;
mycommand.ExecuteNonQuery();
}
dtResults = Utility.db.Connection.oracletoDataTable(strScript, doReturnData);
我也不确定这是否设置为产生正确的结果。你能告诉我这里哪里出错吗?
答案 0 :(得分:2)
由于codeproject.com,我实际上找到了一种更有效的解决方案。
这是我最后的方法,效果很好。我所做的就是使用schema.table和datatable调用它,然后处理其余部分:
public static void WriteToServer(string qualifiedTableName, DataTable dataTable)
{
//**************************************************************************************************************************
// Summary: Hit the Oracle DB with the provided datatable. bulk insert data to table.
//**************************************************************************************************************************
// History:
// 10/03/2017 Created
//**************************************************************************************************************************
try
{
OracleConnection oracleConnection = new OracleConnection(Variables.strOracleCS);
oracleConnection.Open();
using (OracleBulkCopy bulkCopy = new OracleBulkCopy(oracleConnection))
{
bulkCopy.DestinationTableName = qualifiedTableName;
bulkCopy.WriteToServer(dataTable);
}
oracleConnection.Close();
oracleConnection.Dispose();
}
catch (Exception ex)
{
Utility.db.Log.Write(Utility.db.Log.Level.Error, "Utility", "db:WriteToServer: " + ex.Message);
throw;
}
}
参考:https://www.codeproject.com/Questions/228101/oracle-data-bulk-insert
答案 1 :(得分:0)
mycommand.ArrayBindCount = dtResults.Count;
mycommand.Parameters.Add(":parameterName", OracleDbType.Varchar2, dtResults.Select(c => c.ParameterName).ToArray(), ParameterDirection.Input);
mycommand.ExecuteNonQuery() ;
在CommandText设置之后,您可以使用上面的代码。