![](/img/trans.png)
[英]The given value of type String from the data source cannot be converted to type float of the specified target column
[英]The given value of type String from the data source cannot be converted to type nvarchar of the specified target column
我已經在數據表列中使用和不使用顯式轉換對其進行了測試,但它一直向我拋出異常 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();
}
你的問題在這里:
var dt = new DataTable();
//...
dt.Columns.Add("SpeedBand");
//...
dt.Columns.Add("StartLatitude");
dt.Columns.Add("StartLongitude");
dt.Columns.Add("EndLatitude");
dt.Columns.Add("EndLongitude");
默認情況下,方法Add(string columnName)
將添加一個string
類型的列。 因此,您與基礎數據庫表的類型不匹配,並且批量復制失敗。 閱讀此處了解更多詳情。
您使用DataAdapter
解決方案有效,因為適配器讀取底層表的架構並為您設置具有兼容類型的DataTable
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.