views:

37

answers:

1

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

A: 

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() from reader[i].

Sam
Using a insert into select from statement I get malformed database error, as I have to attach a new SQLite database, and it turns out it doesn't work as supposed to.Removing .ToString() doesn't help, as for example non-null columns needs to have empty value, and the provider sends null for it. SQLite doesn't force type, but I think the provider forces it.
Pentium10