检索 ODBC 表并插入 SQL Server CE 数据库

编程入门 行业动态 更新时间:2024-10-27 04:36:59
本文介绍了检索 ODBC 表并插入 SQL Server CE 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个到数据库的 ODBC 连接,我需要其中的一个数据表.该表大约有 20 行,以及几千行数据.

I have an ODBC connection to a database of which I need one table of data. The table has about 20 rows, and a couple thousand lines of data.

我打算将此表插入到我的本地 SQL Server CE 数据库中,以便我可以进一步使用它.两种连接都经过测试并且可以正常工作.

I intend to insert this table into my local SQL Server CE database, where I can put it to further use. Both connections have been tested and work.

我的尝试只是插入一列以保持简单(我是 C#、编程和 stackoverflow 的新手).

My attempt was at just inserting one column to keep things simple (I'm new to C#, programming, and stackoverflow).

OdbcConnection c = new OdbcConnection(ConnectionString1); SqlCeConnection d = new SqlCeConnection(ConnectionString2); c.Open(); d.Open(); string sqlC = "SELECT * FROM ODBCTABLE WHERE ODBCCOLUMN='12345'"; OdbcCommand commandC = new OdbcCommand(sqlC, c); string sqlD = "INSERT INTO SQLCETABLE(SQLCECOLUMN) VALUES (@sql)"; SqlCeCommand commandD = new SqlCeCommand(sqlD, d); OdbcDataReader reader = commandC.ExecuteReader(); while (reader.Read()) { string x = reader[0].ToString(); commandD.Parameters.Add("@sql",SqlDbType.NVarChar, 5).Value = x; commandD.ExecuteNonQuery(); } c.Close(); c.Dispose(); d.Close(); d.Dispose();

我收到错误此 SqlCeParameterCollection 已包含具有此名称的 SqlCeParameter.

  • 为什么这是错误的?
  • 有没有办法解决这个问题?
  • 有没有更好的方法来做到这一点转移?(我相信 odbc 不存在 sqlbulktransfer)
  • 作为我在 Stackoverflow 上的第一篇文章,我是否打算发布提问正确吗?
  • 推荐答案

    更改这部分代码

    commandD.Parameters.Add("@sql",SqlDbType.NVarChar, 5); while (reader.Read()) { string x = reader[0].ToString(); commandD.Parameters["@sql"].Value = x ; commandD.ExecuteNonQuery(); }

    问题的产生是因为,在每个循环中,您重复将相同命名的参数添加到集合中,从而导致上述错误.将参数的创建移到循环外并仅更新循环内的值应该可以解决错误.

    The problem arises because, in every loop, you repeat the add of the same named parameter to the collection resulting in the error above. Moving the creation of the parameter outside the loop and updating only the value inside the loop should resolve the error.

    是的,我认为您已经正确地发布了问题.

    Yes, I think you have posted the question correctly.

    更多推荐

    检索 ODBC 表并插入 SQL Server CE 数据库

    本文发布于:2023-10-12 17:37:23,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1485405.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数据库   SQL   ODBC   CE   Server

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!