![](/img/trans.png)
[英]The given value of type String from the data source cannot be converted to type int of the specified target column
[英]The given value of type String from the data source cannot be converted to type int of the specified target column.'
我正在研究一种插入方法,但它给了我 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);
}
}
我不断收到此异常:
System.InvalidOperationException:“来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。”
这两个内部异常:
FormatException:无法将参数值从 String 转换为 Int32。
FormatException:输入字符串的格式不正确。
如果有人可以帮助我,那就太好了,所以我可以从中学习
我今天遇到了这个问题。 问题是您的表格列顺序与 dataTable 的列顺序不同。
例如:
如果您的表格如下所示:
那么您的代码必须如下所示:
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));
如果我们将 Code 列的顺序(它是字符串)更改为 first,那么批量插入将不起作用。 例如下面的代码不起作用:
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));
批量插入更喜欢列顺序,这意味着如果 dataTable 的列顺序与上例中的数据库表不同,那么批量插入将尝试将 Code 数据插入到 Firm 列中,这将引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.