无法将类型 'System.String' 的对象转换为类型 'System.Array' Oracle 批量插入
Unable to cast object of type 'System.String' to type 'System.Array' Oracle bulk insert
我正在尝试在 Oracle Table 中插入批量数据,但很遗憾收到此错误:
Unable to cast object of type 'System.String' to type 'System.Array'.
我试过这些链接的答案,但没有用。
why does this oracle bulk insert not work
这是我的代码:
private void InsertInTempMachineResults(ResultObject resultObject)
{
string command = @"INSERT INTO ANALYZERS.TEMP_MACHINE_RESULTS (SAMPLE_ID, TEST_ID, TEST_VALUES, " +
"TEST_VALUES_SYMBOL, TEST_VALUE_UNIT, RESULT_DATE, STRING_ID, MACHINE_ID, PARSING_SOURCE) VALUES(" +
":sampleId, :testId, :testValue, :testValueSymbol, :testUnit, SYSDATE, :stringId, :machineId, " +
":parsingSource)";
OracleCommand oracleCommand = new OracleCommand(command, Utils.Connection)
{
CommandType = System.Data.CommandType.Text
};
oracleCommand.ArrayBindCount = resultObject.ResultItems.Count;
oracleCommand.BindByName = true;
OracleParameter[] parameters = {
new OracleParameter("sampleId", resultObject.SampleId),
new OracleParameter("testId", OracleDbType.Varchar2,
resultObject.ResultItems.Select(r => r.TestId).ToArray(), ParameterDirection.Input),
new OracleParameter("testValue", OracleDbType.Varchar2,
resultObject.ResultItems.Select(r => r.TestValue).ToArray(), ParameterDirection.Input),
new OracleParameter("testValueSymbol", OracleDbType.Varchar2,
resultObject.ResultItems.Select(r => r.Symbol).ToArray(), ParameterDirection.Input),
new OracleParameter("testUnit", OracleDbType.Varchar2,
resultObject.ResultItems.Select(r => r.Unit).ToArray(), ParameterDirection.Input),
new OracleParameter("stringId", OracleDbType.Varchar2, resultObject.StringId, ParameterDirection.Input),
new OracleParameter("machineId", OracleDbType.Varchar2, resultObject.MachineId, ParameterDirection.Input),
new OracleParameter("parsingSource", OracleDbType.Varchar2, "C#", ParameterDirection.Input),
};
oracleCommand.Parameters.AddRange(parameters);
if (Utils.Connection.State != ConnectionState.Open)
{
Utils.Connection.Open();
}
try
{
int result = oracleCommand.ExecuteNonQuery();
if (result <= 0)
{
Console.WriteLine("ERROR_INSERT_TEMP_MACHINE_RESULTS: {0}", "Unable to add data.");
}
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION_INSERT_TEMP_MACHINE_RESULTS: {0}", e.Message);
throw e;
}
}
谢谢
正如评论中Patrick Hofman建议的那样,如果要批量插入,则必须将所有参数转换为数组。
问题已解决。
我正在尝试在 Oracle Table 中插入批量数据,但很遗憾收到此错误:
Unable to cast object of type 'System.String' to type 'System.Array'.
我试过这些链接的答案,但没有用。
why does this oracle bulk insert not work
这是我的代码:
private void InsertInTempMachineResults(ResultObject resultObject)
{
string command = @"INSERT INTO ANALYZERS.TEMP_MACHINE_RESULTS (SAMPLE_ID, TEST_ID, TEST_VALUES, " +
"TEST_VALUES_SYMBOL, TEST_VALUE_UNIT, RESULT_DATE, STRING_ID, MACHINE_ID, PARSING_SOURCE) VALUES(" +
":sampleId, :testId, :testValue, :testValueSymbol, :testUnit, SYSDATE, :stringId, :machineId, " +
":parsingSource)";
OracleCommand oracleCommand = new OracleCommand(command, Utils.Connection)
{
CommandType = System.Data.CommandType.Text
};
oracleCommand.ArrayBindCount = resultObject.ResultItems.Count;
oracleCommand.BindByName = true;
OracleParameter[] parameters = {
new OracleParameter("sampleId", resultObject.SampleId),
new OracleParameter("testId", OracleDbType.Varchar2,
resultObject.ResultItems.Select(r => r.TestId).ToArray(), ParameterDirection.Input),
new OracleParameter("testValue", OracleDbType.Varchar2,
resultObject.ResultItems.Select(r => r.TestValue).ToArray(), ParameterDirection.Input),
new OracleParameter("testValueSymbol", OracleDbType.Varchar2,
resultObject.ResultItems.Select(r => r.Symbol).ToArray(), ParameterDirection.Input),
new OracleParameter("testUnit", OracleDbType.Varchar2,
resultObject.ResultItems.Select(r => r.Unit).ToArray(), ParameterDirection.Input),
new OracleParameter("stringId", OracleDbType.Varchar2, resultObject.StringId, ParameterDirection.Input),
new OracleParameter("machineId", OracleDbType.Varchar2, resultObject.MachineId, ParameterDirection.Input),
new OracleParameter("parsingSource", OracleDbType.Varchar2, "C#", ParameterDirection.Input),
};
oracleCommand.Parameters.AddRange(parameters);
if (Utils.Connection.State != ConnectionState.Open)
{
Utils.Connection.Open();
}
try
{
int result = oracleCommand.ExecuteNonQuery();
if (result <= 0)
{
Console.WriteLine("ERROR_INSERT_TEMP_MACHINE_RESULTS: {0}", "Unable to add data.");
}
}
catch (Exception e)
{
Console.WriteLine("EXCEPTION_INSERT_TEMP_MACHINE_RESULTS: {0}", e.Message);
throw e;
}
}
谢谢
正如评论中Patrick Hofman建议的那样,如果要批量插入,则必须将所有参数转换为数组。 问题已解决。