If the desintation table has the same column structure as your DataTable, you do not need to map the columns. If the desintation table differs in structure from your DataTable, than you have to map each column.
public void BulkLoadToTemp(DataTable dt, String tableName, int bulkLoadBatchSize)
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(this._connection))
{
bulkCopy.DestinationTableName = tableName;
bulkCopy.BulkCopyTimeout = 120;
bulkCopy.BatchSize = bulkLoadBatchSize;
bulkCopy.WriteToServer(dt);
bulkCopy.Close();
}
}
Just realized, that you are not using a varchar for your sql column. In this case, I think best bet is to use a sp to populate the table, and execute the stored procedure using array binding. Here's an example that I did using Oracle. You can modify it and simplify it a lot more when using sql server.
public void BulkLoadWithArrayBinding(System.Data.DataTable dt)
{
StringBuilder sb = new StringBuilder();
List<OracleParameter> parameters = new List<OracleParameter>(dt.Columns.Count);
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
sb.Append("INSERT INTO \"" + dt.TableName + "\" (");
foreach (DataColumn dc in dt.Columns)
{
sb.Append("\"" + dc.ColumnName.ToUpper() + "\"");
if (dc.Ordinal < dt.Columns.Count - 1)
sb.AppendLine(",");
}
sb.Append(") VALUES(");
foreach (DataColumn dc in dt.Columns)
{
string parameterName = dc.ColumnName.ToUpper();
sb.Append(":" + parameterName);
if (dc.Ordinal < dt.Columns.Count - 1)
sb.AppendLine(",");
OracleString[] sArray = null;
OracleDate[] dArray = null;
OracleDecimal[] dbArray = null;
OracleParameter p = null;
if (dc.DataType.Name == "String")
{
sArray = new OracleString[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][dc.Ordinal] != DBNull.Value)
sArray[i] = dt.Rows[i][dc.Ordinal].ToString();
else
sArray[i] = OracleString.Null;
}
p = new OracleParameter(parameterName,OracleDbType.Varchar2, dt.Rows.Count, ParameterDirection.Input);
p.Size = sArray.Length;
p.Value = sArray;
}
else if (dc.DataType.Name == "DateTime")
{
dArray = new OracleDate[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][dc.Ordinal] != DBNull.Value)
try
{
dArray[i] = (OracleDate)Convert.ToDateTime(dt.Rows[i][dc.Ordinal]);
}
catch
{
object o = dt.Rows[i][dc.Ordinal];
dArray[i] = OracleDate.Null;
}
else
{
dArray[i] = OracleDate.Null;
}
}
p = new OracleParameter(parameterName,OracleDbType.Date, dt.Rows.Count, ParameterDirection.Input);
p.Size = dArray.Length;
p.Value = dArray;
}
else if (dc.DataType.Name == "Double")
{
dbArray = new OracleDecimal[dt.Rows.Count]; ;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][dc.Ordinal] != DBNull.Value)
dbArray[i] = Convert.ToDecimal(dt.Rows[i][dc.Ordinal]);
else
dbArray[i] = OracleDecimal.Null;
}
p = new OracleParameter(parameterName, OracleDbType.Decimal, dt.Rows.Count, ParameterDirection.Input);
p.Value = dbArray;
}
else if (dc.DataType.Name == "Boolean")
{
dbArray = new OracleDecimal[dt.Rows.Count]; ;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][dc.Ordinal] != DBNull.Value)
dbArray[i] = Convert.ToDecimal(dt.Rows[i][dc.Ordinal]);
else
dbArray[i] = OracleDecimal.Null;
}
p = new OracleParameter(parameterName, OracleDbType.Decimal, dt.Rows.Count, ParameterDirection.Input);
p.Value = dbArray;
}
cmd.Parameters.Add(p);
}
sb.AppendLine(")");
cmd.CommandText = sb.ToString();
cmd.CommandType = CommandType.Text;
cmd.ArrayBindCount = dt.Rows.Count;
cmd.BindByName = true;
cmd.AddToStatementCache = true;
cmd.ExecuteNonQuery();
foreach (OracleParameter p in cmd.Parameters)
{
p.Dispose();
}
cmd.Dispose();
}
Where I build up an Insert statement, you can put a stored procedure call, and parameterize as necessary.
SqlGeometry
exist in? – Laroy