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.
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.
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();
I get the error The SqlCeParameter with this name is already contained by this SqlCeParameterCollection.
Change this part of your code
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.