I'm trying to write a data migration app. There are a few dozen tables with a LOT of columns.
I'm having trouble importing the data from a datatable to the SQL Server table.
When trying to use SqlBulkCopy
, I'm getting the error:
The given value of type String from the data source cannot be converted to type image of the specified target column.
I've constructed a test which generates the same error.
Create the table in a Test db.
CREATE TABLE [dbo].[CALTEST]
(
[ACCOUNTNO] [VARCHAR](20) NULL,
[NOTES] [IMAGE] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Paste the code into a button click event
private void button3_Click(object sender, EventArgs e)
{
//Convert json to Datatable
//bulkcopy datatable into CALTEST
string json =
@"[{
""ACCOUNTNO"": ""B1082380914D!J5_8BAR"",
""NOTES"": ""WyBDViBTZW50IF0NCkNhbmRpZGF0ZSA6IEFsYmVydCBHYXZpbnMNCkRlc2NyaXB0aW9uIDoNCg0KDQo=""
},
{
""ACCOUNTNO"": ""B1082380914D!J5_8BAR"",
""NOTES"": ""WyBJbnRlcnZpZXcgXQ0KQ2FuZGlkYXRlIDogQWxiZXJ0IEdhdmlucw0KRGVzY3JpcHRpb24gOg0KDQoNCg==""
},
{
""ACCOUNTNO"": ""B1090241471P!-R0?ELI"",
""NOTES"": ""WyBDViBTZW50IF0NCkNhbmRpZGF0ZSA6IEFsYmVydCBHYXZpbnMNCkRlc2NyaXB0aW9uIDoNCg0KDQo=""
}]
";
string connectionString = string.Format($"Server=SERVERNAME;Database=TEST;Trusted_Connection=True;");
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
DataTable dt = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT TOP 0 * FROM CALTEST", conn))
{
adapter.Fill(dt);
};
DataTable dtJson = JsonConvert.DeserializeObject<DataTable>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
});
//foreach (DataRow dr in dtJson.Rows)
//{
// dt.Rows.Add(dr.ItemArray);
//}
using (var bulkCopy = new SqlBulkCopy(conn))
{
// dt.Columns.Cast<DataColumn>().ToList().ForEach(x => bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(x.ColumnName, x.ColumnName)));
bulkCopy.DestinationTableName = "CALTEST";
bulkCopy.BatchSize = 50000;
bulkCopy.BulkCopyTimeout = 60; //seconds
bulkCopy.WriteToServer(dt);
}
}
}
I'm retrieving the table data structure from the database, which works. The NOTES column is of type image
.
But when adding the json datatable, that data schema is overwritten.
How would I go about setting the datatypes of the fields in the retrieved from json?
Should I give up on SqlBulkCopy
and generate insert queries for each record?
What is the best way of getting json data into a SQL Server table?
Thanks.