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

查看:110
本文介绍了数据源中类型字符串的给定值无法转换为指定目标列的int类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将excel数据导入sql server表



代码如下



importing excel data into sql server table

Code as follows

protected void btnexport_Click(object sender, EventArgs e)
        {
            if (!FileUpload1.HasFile)
            {
      string CurrentFilePath = string.Concat(Server.MapPath("~/masterdata/Test1.xlsx" + FileUpload1.FileName));
                 FileUpload1.SaveAs(CurrentFilePath);

     string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);

                    OleDbConnection connection = new OleDbConnection();
                    connection.ConnectionString = constr;
     OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                    connection.Open();
       System.Data.Common.DbDataReader dr = command.ExecuteReader();
     string sqlConnectionString = @"Data Source = DESKTOP; Initial Catalog = Sample; User ID = sa; Password = 12345";
                     
                    SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
                    bulkInsert.DestinationTableName = "tbl_farmerregistration_ivorycoast";
                    bulkInsert.WriteToServer(dr);





在运行模式下,我点击导出按钮并调试并显示如下错误



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



如何解决这个错误。



我尝试过:



将excel数据导入sql server表



代码如下





In run mode i click th export button and debug and shows error as follows

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

how to solve this error.

What I have tried:

importing excel data into sql server table

Code as follows

protected void btnexport_Click(object sender, EventArgs e)
        {
            if (!FileUpload1.HasFile)
            {
      string CurrentFilePath = string.Concat(Server.MapPath("~/masterdata/Test1.xlsx" + FileUpload1.FileName));
                 FileUpload1.SaveAs(CurrentFilePath);

     string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);

                    OleDbConnection connection = new OleDbConnection();
                    connection.ConnectionString = constr;
     OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                    connection.Open();
       System.Data.Common.DbDataReader dr = command.ExecuteReader();
     string sqlConnectionString = @"Data Source = DESKTOP; Initial Catalog = Sample; User ID = sa; Password = 12345";
                     
                    SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
                    bulkInsert.DestinationTableName = "tbl_farmerregistration_ivorycoast";
                    bulkInsert.WriteToServer(dr);





在运行模式下,我点击导出按钮并调试并显示如下错误



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



如何解决这个错误。



In run mode i click th export button and debug and shows error as follows

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

how to solve this error.

推荐答案

,连接);
connection.Open();
系统.Data.Common.DbDataReader dr = command.ExecuteReader();
string sqlConnectionString = @ 数据源= DESKTOP;初始目录=样本;用户ID = sa;密码= 12345;

SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
bulkInsert.DestinationTableName = tbl_farmerregistration_ivorycoast;
bulkInsert.WriteToServer(dr);
", connection); connection.Open(); System.Data.Common.DbDataReader dr = command.ExecuteReader(); string sqlConnectionString = @"Data Source = DESKTOP; Initial Catalog = Sample; User ID = sa; Password = 12345"; SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString); bulkInsert.DestinationTableName = "tbl_farmerregistration_ivorycoast"; bulkInsert.WriteToServer(dr);





在运行模式下,我点击导出按钮并调试并显示如下错误



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



如何解决此错误。



我尝试过:



将excel数据导入sql server表



代码如下





In run mode i click th export button and debug and shows error as follows

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

how to solve this error.

What I have tried:

importing excel data into sql server table

Code as follows

protected void btnexport_Click(object sender, EventArgs e)
        {
            if (!FileUpload1.HasFile)
            {
      string CurrentFilePath = string.Concat(Server.MapPath("~/masterdata/Test1.xlsx" + FileUpload1.FileName));
                 FileUpload1.SaveAs(CurrentFilePath);

     string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);

                    OleDbConnection connection = new OleDbConnection();
                    connection.ConnectionString = constr;
     OleDbCommand command = new OleDbCommand("select * from [Sheet1


,连接);
connection.Open();
System.Data.Common.DbDataReader dr = command.ExecuteReader();
string sqlConnectionString = @ 数据源= DESKTOP;初始目录=样本;用户ID = sa;密码= 12345;

SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
bulkInsert.DestinationTableName = tbl_farmerregistration_ivorycoast;
bulkInsert.WriteToServer(dr);
", connection); connection.Open(); System.Data.Common.DbDataReader dr = command.ExecuteReader(); string sqlConnectionString = @"Data Source = DESKTOP; Initial Catalog = Sample; User ID = sa; Password = 12345"; SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString); bulkInsert.DestinationTableName = "tbl_farmerregistration_ivorycoast"; bulkInsert.WriteToServer(dr);





在运行模式下,我点击导出按钮并调试并显示如下错误



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



如何解决此错误。



In run mode i click th export button and debug and shows error as follows

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

how to solve this error.


查看错误消息:
Quote:

给定数据源中String类型的值不能转换为指定目标列的int类型。

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

它非常明确:您有一个包含非数字数据的列,您试图在整数列中显示。< br $> b $ b

我们无法做任何事情 - 我们无法访问您的Excel数据或您的数据库......所以请查看您的数据,找出相关内容应该是,它是什么,为什么。然后你可以考虑修理它。



但我们不能为你做任何事情!

It's pretty explicit: you have a column which contains non-numeric data that you are trying to display in an integer column.

We can't do anything about that - we have no access to your excel data, or to your database ... so look at your data, and find out what it should be, what it is, and why. Then you can think about fixing it.

But we can't do any of that for you!


这篇关于数据源中类型字符串的给定值无法转换为指定目标列的int类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆