参数化插入查询中的数据类型不匹配问题
我有这段代码将记录从一个表传输到另一个表
cmd2.CommandText = "select * from " + TableName;
reader = cmd2.ExecuteReader();
if (reader != null)
{
String s = "".PadLeft(reader.FieldCount - 1, 'X').Replace("X", "?,") + "?";
cmd.CommandText = String.Format("{0} into {1} values ({2})", insertorreplace, TableName, s);
StringBuilder sb = new StringBuilder();
while (reader.Read())
{
int i = 0;
for (i = 0; i < reader.FieldCount; ++i)
{
cmd.Parameters.AddWithValue(null,reader[i].ToString()); // data type issue
}
cmd.ExecuteNonQuery();
}
reader.Close();
}
可能是因为注释行我遇到了数据类型问题。我希望所有的东西都通过字符串传递。
仍在寻找答案
I have this code to transfer records from one-table to the other
cmd2.CommandText = "select * from " + TableName;
reader = cmd2.ExecuteReader();
if (reader != null)
{
String s = "".PadLeft(reader.FieldCount - 1, 'X').Replace("X", "?,") + "?";
cmd.CommandText = String.Format("{0} into {1} values ({2})", insertorreplace, TableName, s);
StringBuilder sb = new StringBuilder();
while (reader.Read())
{
int i = 0;
for (i = 0; i < reader.FieldCount; ++i)
{
cmd.Parameters.AddWithValue(null,reader[i].ToString()); // data type issue
}
cmd.ExecuteNonQuery();
}
reader.Close();
}
Probably because of the commented line I get data type issue. I want all things to be passed by string.
Still looking for answers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要从一个表复制到另一个表,最好的办法是使用
INSERT INTO ... SELECT FROM
在一个查询中完成所有操作。要修复您的代码,只需从
reader[i]
中删除.ToString()
即可。If you're copying from one table to another, the best thing is to do it all in one query with
INSERT INTO ... SELECT FROM
.To fix your code, just remove
.ToString()
fromreader[i]
.