简体   繁体   English

来自数据源的 String 类型的给定值无法转换为指定目标列的 nvarchar 类型

[英]The given value of type String from the data source cannot be converted to type nvarchar of the specified target column

I've tested it with and without explicit conversion in the datatable columns, but it kept throwing me the exception System.InvalidOperationException.我已经在数据表列中使用和不使用显式转换对其进行了测试,但它一直向我抛出异常 System.InvalidOperationException。

FYI, those columns with typeof(string) are all nvarchar in my database.仅供参考,那些带有 typeof(string) 的列在我的数据库中都是 nvarchar。 I'm passing in a List called test which is of a user-defined type.我正在传入一个名为test的列表,它是用户定义的类型。

I wasn't going to use sqlbulkcopy initially, but ever since porting over to azure database, I need a faster insertion query.我最初不打算使用 sqlbulkcopy,但自从移植到 azure 数据库后,我需要一个更快的插入查询。

Any advice would be appreciated.任何意见,将不胜感激。

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);
        }
    }
}

i've switched to using a dataAdapter and it works now我已经改用 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();
            }

Your problem lies here:你的问题在这里:

var dt = new DataTable();
//...
dt.Columns.Add("SpeedBand");
//...
dt.Columns.Add("StartLatitude");
dt.Columns.Add("StartLongitude");
dt.Columns.Add("EndLatitude");
dt.Columns.Add("EndLongitude");

By default, the method Add(string columnName) will add a column of type string .默认情况下,方法Add(string columnName)将添加一个string类型的列。 You therefore have a type mismatch with the underlying db table and the bulk copy fails.因此,您与基础数据库表的类型不匹配,并且批量复制失败。 Read here for more details.阅读此处了解更多详情。

Your solution with DataAdapter works because the adapter reads the schema of the underyling table and sets up the DataTable with compatible types for you.您使用DataAdapter解决方案有效,因为适配器读取底层表的架构并为您设置具有兼容类型的DataTable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 来自数据源的String类型的给定值不能转换为指定目标列的varbinary类型 - The given value of type String from the data source cannot be converted to type varbinary of the specified target column 数据源中String类型的给定值无法转换为指定目标列的int类型 - The given value of type String from the data source cannot be converted to type int of the specified target column SqlBulkCopy - 来自数据源的 String 类型的给定值无法转换为指定目标列的货币类型 - SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。 - The given value of type String from the data source cannot be converted to type int of the specified target column.' SQlBulkCopy 无法将数据源中 DateTime 类型的给定值转换为指定目标列的 int 类型 - SQlBulkCopy The given value of type DateTime from the data source cannot be converted to type int of the specified target column SqlBulkCopy引发异常:无法将数据源中String类型的给定值转换为指定目标列的bigint类型 - SqlBulkCopy throws exception: The given value of type String from the data source cannot be converted to type bigint of the specified target column SQL批量复制“使用ASP.NET无法将数据源中String类型的给定值转换为指定目标列的类型日期时间” - SQL Bulk Copy “The given value of type String from the data source cannot be converted to type datetime of the specified target column” using ASP.NET 在LINQ dat source / gridview中使用文本框值作为参数:“String”类型的值无法转换为“Double”类型 - Using textbox value as parameter in LINQ dat source/gridview : A value of type 'String' cannot be converted to type 'Double' 无法在错误的varbinary(max)中插入空值:不允许从数据类型nvarchar隐式转换为varbinary(max) - Cannot insert null value in varbinary(max) with error : implicit conversion from data type nvarchar to varbinary(max) is not allowed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM