Currently I am importing large sets of data from an ASCII file containing a Guid. A sample row of import data would be:
35313532-3200-0000-0000-000000000000,PRT100,MYCORP ENTERPRISES, ...
The problem is with the first column which is in SQL server mapped as Guid. I get the following error: The given value of type String from the data source cannot be converted to type uniqueidentifier of the specified target column.
The column seems ok to me. Anyone knows how to import a string representation of a Guid into a real Guid?
Here's the function in question:
private static void ImportBc(string sInputConnectionString,
string sOutputConnectionString,
string sInputTable,
string sOutputTable,
string[,] arrMap,
bool bDelete)
{
Console.WriteLine("Import datoteke {0}......", sInputTable);
if (bDelete)
{
var sqlCnn = new SqlConnection { ConnectionString = sOutputConnectionString };
sqlCnn.Open();
//var deleteCommand = new SqlCommand("TRUNCATE TABLE " + sOutputTable) { Connection = sqlCnn };
var deleteCommand = new SqlCommand("DELETE FROM " + sOutputTable) { Connection = sqlCnn };
deleteCommand.ExecuteNonQuery();
sqlCnn.Close();
}
string oledbConnectionString = sInputConnectionString;
var connection = new OleDbConnection(oledbConnectionString);
var command = new OleDbCommand(sInputTable, connection);
connection.Open();
// Create DbDataReader
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = sOutputConnectionString;
// Bulk Copy to SQL Server
using (var bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.BatchSize = 10000;
bulkCopy.NotifyAfter = 10000;
bulkCopy.SqlRowsCopied += OnSqlRowsCopied;
bulkCopy.DestinationTableName = sOutputTable;
/* Zbog timeout expired problema */
bulkCopy.BulkCopyTimeout = 9000;
for (int i = 0; i < arrMap.GetLength(0); i++)
{
bulkCopy.ColumnMappings.Add(arrMap[i, 0], arrMap[i, 1]);
Console.WriteLine("{0} {1}", arrMap[i, 0], arrMap[i, 1]);
}
bulkCopy.WriteToServer(dr);
}
}
}