I am trying to simply and quickly inserts 1000s of rows into an Oracle table using a C# application.
I thought to use this method of insertion, where an array of data passed through as parameters instead of creating my won long command string containing INTOs: https://www.c-sharpcorner.com/article/two-ways-to-insert-bulk-data-into-oracle-database-using-c-sharp/
My issue is that I am using DateTime values, which needs to be specified, can I do that for the full array?
Code:
public System.TimeSpan oracleInsertData(string tableName, List<DateTime> dates, List<double> data, OracleConnection con)
{
try
{
DateTime start = DateTime.Now;
//first empty the table
oracleEmptyTable(tableName, con);
con.Open();
// create command and set properties
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO " + tableName + " (DateStamp, SensorData) VALUES (:1, :2)";
cmd.ArrayBindCount = dates.Count;
cmd.Parameters.Add(dates.ToArray());
cmd.Parameters.Add(data.ToArray());
cmd.ExecuteNonQuery();
con.Close();
DateTime finish = DateTime.Now;
return finish - start;
}
catch (OracleException ex)
{
con.Close();
Console.WriteLine("Error: {0}", ex.ToString());
return new TimeSpan(0);
}
}
I get the following error:
Additional information: Unable to cast object of type 'System.DateTime[]' to type 'Oracle.DataAccess.Client.OracleParameter'.
Edit: honey_ramgarhia pointed out my obvious mistake... with the udpated code it works. Code to hopefully help for some people:
public System.TimeSpan oracleInsertData(string tableName, List<DateTime> dates, List<double> data, OracleConnection con)
{
try
{
DateTime start = DateTime.Now;
//first empty the table
oracleEmptyTable(tableName, con);
con.Open();
OracleParameter oracleDates = new OracleParameter();
oracleDates.OracleDbType = OracleDbType.TimeStamp;
oracleDates.Value = dates.ToArray();
OracleParameter oracleData = new OracleParameter();
oracleData.OracleDbType = OracleDbType.Double;
oracleData.Value = data.ToArray();
// create command and set properties
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO " + tableName + " (DateStamp, SensorData) VALUES (:1, :2)";
cmd.ArrayBindCount = dates.Count;
cmd.Parameters.Add(oracleDates);
cmd.Parameters.Add(oracleData);
cmd.ExecuteNonQuery();
con.Close();
DateTime finish = DateTime.Now;
return finish - start;
}
catch (OracleException ex)
{
try
{
con.Close();
}
catch { }
Console.WriteLine("Error: {0}", ex.ToString());
return new TimeSpan(0);
}
}