简体   繁体   English

数据源中String类型的给定值无法转换为指定目标列的int类型

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

I am trying to read values from xml file and then use the bulkcopy to insert the data into my database. 我试图从xml文件中读取值,然后使用bulkcopy将数据插入到我的数据库中。

I am using a custom class which is: 我正在使用一个自定义类:

class Customer
    {
        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int ShowsNumber { get; set; }
        public int VisitNumber { get; set; }
        public int Cancellation { get; set; }
    }

and I read the data like this: 我读了这样的数据:

List<Customer> customersList =
                (
                    from e in XDocument.Load(file).Root.Elements("cust")
                    select new Customer
                    {
                        CustomerID = (int)e.Attribute("custid"),
                        FirstName = (string)e.Attribute("fname"),
                        LastName = (string)e.Attribute("lname"),
                        ShowsNumber = (int)e.Attribute("count_noshow"),
                        VisitNumber = (int)e.Attribute("count_resos"),
                        Cancellation = (int)e.Attribute("count_cancel"),
                    }).ToList();

Then I insert that customersList to a datatabe like this: 然后我将customersList插入到这样的数据表中:

DataTable dataTable = getBasicDataTable();
for (int i = 0; i < customersList.Count; i++)
                {
                    DataRow datarows = dataTable.NewRow();
                    datarows[0] = customersList[i].CustomerID;
                    datarows[1] = customersList[i].FirstName;
                    datarows[2] = customersList[i].LastName;
                    datarows[3] = customersList[i].ShowsNumber;
                    datarows[4] = customersList[i].VisitNumber;
                    datarows[5] = customersList[i].Cancellation;
                    dataTable.Rows.Add(datarows);
                }

then I insert the data to my database like this: 然后我将数据插入我的数据库,如下所示:

but I got this exception 但我得到了这个例外

using (SqlBulkCopy sbc = new SqlBulkCopy(GetConnectionString()))
            {
                sbc.DestinationTableName = XMLReader.databaseTable;
                sbc.WriteToServer(dataTable);
            }

The given value of type String from the data source cannot be converted to type int of the specified target column. 数据源中String类型的给定值无法转换为指定目标列的int类型。

as you see, when I extract the data from my xml, I already use the cast to int and to string and it is working. 如你所见,当我从我的xml中提取数据时,我已经使用了强制转换为intstring ,它正在工作。 so why when inserting to the database I got that exception? 那么为什么在插入数据库时​​我得到了那个例外?

Note 注意

In order to give you the whole code, this is the getBasicDataTable fuction 为了给你整个代码,这是getBasicDataTable函数

private DataTable getBasicDataTable()
        {
            DataTable dataTable = new DataTable();
            dataTable.Clear();
            dataTable.Columns.Add("customerID");
            dataTable.Columns.Add("firstName");
            dataTable.Columns.Add("lastName");
            dataTable.Columns.Add("showsNumber");
            dataTable.Columns.Add("visitNumber");
            dataTable.Columns.Add("cancellation");
            return dataTable;
        }

You need to specify the column as integer while defining the datatable . 你需要为整到指定列在定义datatable Like this:- 像这样:-

dataTable.Columns.Add("customerID", typeof(int));

Edit: 编辑:
The other reason i suspect is probably the way you are binding your datatable (I mean the order of columns) doesn't match with that of database table. 我怀疑的另一个原因可能是你绑定数据表的方式(我的意思是列的顺序)与数据库表的绑定方式不匹配。 Reason being I think default mapping is not Name to Name rather its Index to Index in SqlBulkCopy . 原因是我认为默认映射不是Name to Name而不是SqlBulkCopy Index to Index。 So Kindly re-check your database table order, It should look like:- 所以请重新检查您的数据库表顺序,它应该如下: -

CustomerID (INT)
FirstName (VARCHAR\NVARCHAR)
LastName (VARCHAR\NVARCHAR)
ShowsNumber (INT)
VisitNumber (INT)
Cancellation (INT)

As far as I am aware, you need to set the Type of the column on the datatable, otherwise it will presume string (because nearly everything can be converted as such). 据我所知,你需要在数据表上设置列的类型,否则它将设定字符串(因为几乎所有东西都可以这样转换)。

Before you set the values, try: 在设置值之前,请尝试:

dataTable.Columns[2].Type = typeof (int);

or, alternatively, you can specify it when defining the columns: 或者,您可以在定义列时指定它:

dataTable.Columns.Add("ShowsNumber", typeof(int));

暂无
暂无

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

相关问题 来自数据源的 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 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 nvarchar 类型 - The given value of type String from the data source cannot be converted to type nvarchar 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 SqlBulkCopy - 来自数据源的 String 类型的给定值无法转换为指定目标列的货币类型 - SqlBulkCopy - The given value of type String from the data source cannot be converted to type money 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' 无法将源类型system.nullable转换为目标类型int - Cannot convert source type system.nullable to target type int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM