c# - 来自数据源的 String 类型的给定值无法转换为指定目标列的 nvarchar 类型
问题描述
我已经在数据表列中使用和不使用显式转换对其进行了测试,但它一直向我抛出异常 System.InvalidOperationException。
仅供参考,那些带有 typeof(string) 的列在我的数据库中都是 nvarchar 。我传入了一个名为test的列表,它是用户定义的类型。
最初我不打算使用 sqlbulkcopy,但是自从移植到 azure 数据库后,我需要一个更快的插入查询。
任何意见,将不胜感激。
var dt = new DataTable();
dt.Columns.Add("LinkID", typeof(string));
dt.Columns.Add("RoadName", typeof(string));
dt.Columns.Add("RoadCategory", typeof(string));
dt.Columns.Add("SpeedBand");
dt.Columns.Add("MinimumSpeed", typeof(string));
dt.Columns.Add("MaximumSpeed", typeof(string));
dt.Columns.Add("StartLatitude");
dt.Columns.Add("StartLongitude");
dt.Columns.Add("EndLatitude");
dt.Columns.Add("EndLongitude");
dt.Columns.Add("Distance", typeof(string));
for (int i = 0; i < test.Count; i++)
{
dt.Rows.Add(test[i].LinkID, test[i].RoadName, test[i].RoadCategory,
test[i].SpeedBand, test[i].MinimumSpeed, test[i].MaximumSpeed,
test[i].StartLatitude, test[i].StartLongitude, test[i].EndLatitude,
test[i].EndLongitude, test[i].Distance);
}
string sqlConnectionString = "//secret";
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
try
{
conn.Open();
}
catch (Exception e)
{
Debug.WriteLine(e);
}
using (var sqlBulk = new SqlBulkCopy(conn))
{
//sqlBulk.BatchSize = 1000;
sqlBulk.DestinationTableName = "dbo.TrafficSpeedBands";
try
{
// Write from the source to the destination.
sqlBulk.WriteToServer(dt);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
解决方案
我已经切换到使用 dataAdapter 并且现在可以使用
string sqlConnectionString = "//secret";
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
try
{
conn.Open();
}
catch (Exception e)
{
Debug.WriteLine(e);
}
Debug.WriteLine("Connection opened");
var table = new DataTable();
// read the table structure from the database
using (var adapter = new SqlDataAdapter($"SELECT TOP 0 * FROM dbo.TrafficSpeedBands", conn))
{
adapter.Fill(table);
};
Debug.WriteLine("Filling in rows");
for (var i = 0; i < test.Count; i++)
{
var row = table.NewRow();
row["LinkID"] = test[i].LinkID;
row["RoadName"] = test[i].RoadName;
row["RoadCategory"] = test[i].RoadCategory;
row["SpeedBand"] = test[i].SpeedBand;
row["MinimumSpeed"] = test[i].MinimumSpeed;
row["MaximumSpeed"] = test[i].MaximumSpeed;
row["StartLatitude"] = test[i].StartLatitude;
row["StartLongitude"] = test[i].StartLongitude;
row["EndLatitude"] = test[i].EndLatitude;
row["EndLongitude"] = test[i].EndLongitude;
row["Distance"] = test[i].Distance;
table.Rows.Add(row);
}
using (var sqlBulk = new SqlBulkCopy(conn))
{
Debug.WriteLine("Ready to load live");
sqlBulk.DestinationTableName = "dbo.TrafficSpeedBands";
try
{
// Write from the source to the destination.
sqlBulk.WriteToServer(table);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
Debug.WriteLine("Done");
}
conn.Close();
}
推荐阅读
- python - 在 Tableau 中运行 python(预测)时出错
- php - 从 Python 加密到 PHP 的 MCRYPT_RIJNDAEL_256
- angular - “Route”类型没有共同的属性
- c# - 如何创建链接任务
- proxy - 无法使用量角器在 Chrome 浏览器中打开 UI
- angularjs - 哪种数据库和聊天库适合使用 Ionic 框架的基于聊天的应用程序
- angular - 在Angular6中的另一个FormArray中显示FormArray
- java - Windows 中的 Docker:无法为 Linux amd64 加载本机库“libnative-platform.so”
- android - 具有双向绑定的自定义组件
- latency - aerospike 与 aws 的不良延迟