[英]The given value of type String from the data source cannot be converted to type int of the specified target column.'
I'm working on an insert method but it gives me 2 errors I can't seem to resolve, I am also an intern who doesn't get enough guidance in this so that's why I'm asking it here.我正在研究一种插入方法,但它给了我 2 个我似乎无法解决的错误,我也是一名实习生,在这方面没有得到足够的指导,所以这就是我在这里问它的原因。
int werknemerId = 12345;
int knowhowLenght = knowhowMatches.Count;
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Kantoor", typeof(int)));
dt.Columns.Add(new DataColumn("Taal", typeof(string)));
dt.Columns.Add(new DataColumn("Spreken", typeof(string)));
dt.Columns.Add(new DataColumn("Lezen", typeof(string)));
dt.Columns.Add(new DataColumn("Schrijven", typeof(string)));
dt.Columns.Add(new DataColumn("Talen_op_werknemerID", typeof(int)));
for (int x = 0; x < languageMatches.Count; x++)
{
string lang = languageMatches[x];
string known = knowhowMatches[x];
dt.Rows.Add(1, new string[]
{
lang, known, known, known
}, werknemerId);
}
using(SqlConnection Conn = new SqlConnection(connstring))
{
Conn.Open();
using(SqlBulkCopy bc = new SqlBulkCopy(Conn))
{
bc.DestinationTableName = "UzkTalen";
bc.WriteToServer(dt);
bc.WriteToServer(dt);
}
}
I keep getting this exception:我不断收到此异常:
System.InvalidOperationException: 'The given value of type String from the data source cannot be converted to type int of the specified target column.'
System.InvalidOperationException:“来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。”
and these two inner exceptions:这两个内部异常:
FormatException: Failed to convert parameter value from a String to an Int32.
FormatException:无法将参数值从 String 转换为 Int32。
FormatException: Input string was not in a correct format.
FormatException:输入字符串的格式不正确。
If someone could help me with this that would be great so I could learn from it如果有人可以帮助我,那就太好了,所以我可以从中学习
I faced this problem at today.我今天遇到了这个问题。 Problem is your table column order is not same with dataTable's column order.
问题是您的表格列顺序与 dataTable 的列顺序不同。
For example:例如:
If your table like as below:如果您的表格如下所示:
Then your code must be like as below:那么您的代码必须如下所示:
table.Columns.Add("Firm", typeof(short));
table.Columns.Add("TigerId", typeof(int));
table.Columns.Add("LogixId", typeof(int));
table.Columns.Add("Code", typeof(string));
table.Columns.Add("TigerItemId", typeof(int));
table.Columns.Add("RegisteredDate", typeof(DateTime));
If we change Code column's order (that it is string) to first, then bulk insert will not work.如果我们将 Code 列的顺序(它是字符串)更改为 first,那么批量插入将不起作用。 For example below code doesn't work:
例如下面的代码不起作用:
table.Columns.Add("Code", typeof(string)); // It is string
table.Columns.Add("Firm", typeof(short));
table.Columns.Add("TigerId", typeof(int));
table.Columns.Add("LogixId", typeof(int));
table.Columns.Add("TigerItemId", typeof(int));
table.Columns.Add("RegisteredDate", typeof(DateTime));
Bulk insert prefers column order, meaning if dataTable's column order is not same database's table like above example, then bulk insert will try insert Code data to Firm column that it will throw exception.批量插入更喜欢列顺序,这意味着如果 dataTable 的列顺序与上例中的数据库表不同,那么批量插入将尝试将 Code 数据插入到 Firm 列中,这将引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.