Click here to Skip to main content
15,918,243 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear friends,

I need to move tables and values from one database to another using windows service.. I tried the following link: I got an error invalid object name..

Sqlite: copy data from all db1 tables to db2 tables[^]

What I have tried:

public void Move()
        {
            <pre>StringBuilder strSql = new StringBuilder();

            using (con)
            {
                con.Open();

                using (cmd1 = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE  TABLE_CATALOG ='AWACS' and TABLE_NAME ='DRL_MenuMaster_hdr'", con))
                {
                    using (SqlDataReader rdr = cmd1.ExecuteReader())
                    {
                        while (rdr.Read()) // Reading Rows
                        {
                            strSql.Append("INSERT INTO MOVEDB.dbo.");
                            strSql.Append(rdr["TABLE_NAME"].ToString());
                            strSql.Append(" (");
                            strSql.Append(rdr["COLUMN_NAME"].ToString());
                            strSql.Append(") ");
                            strSql.Append("SELECT ");
                            strSql.Append(rdr["COLUMN_NAME"].ToString());
                            strSql.Append(" FROM AWACS.dbo.");
                            strSql.Append(rdr["TABLE_NAME"].ToString());
                            strSql.Append(";");
                            var tableName = rdr["TABLE_NAME"].ToString();
                            var Columns = rdr["COLUMN_NAME"].ToString();

                        }
                    }
                }
            }

            using (con1)
            {
                con1.Open();
                using (SqlCommand cmd2 = new SqlCommand(strSql.ToString(), con1))
                {
                    //cmd1.CommandText = strSql.ToString();
                    int i = cmd2.ExecuteNonQuery();
                    if (i > 0)
                    {
                        MessageBox.Show("Data dumped successfully ...!!!");
                    }
                }
            }
        }
Posted
Updated 3-May-17 18:29pm
v3

SQLite does not use SqlCommand objects - it uses SQLiteCommand, SQLiteConnection, and such like constructs. If you want to access SQLite databases, you have to use those instead SQL server classes.

There are many ways to copy a table between SQL server databases: copy a table between SQL server databases - Google Search[^] - each with thier own advantages and disadvantages depending on your situation. Have a look, follow a few links, and see what you can find that fits what you are trying to achieve.
 
Share this answer
 
Comments
Vivek.anand34 4-May-17 0:27am    
above my code executed by how save into db means: for eg. I have 10 rows. 1 st columns saved in 10 rows 2nd columns saved in next 10 rows, 3rd column saved in next 10 rows like this... i think using is problem... pls. c my code above..
You should maintain two connection string in web.Config or App.Config

1) Get all data from database1 and fill it into DataTable
2) Dump all data to database2 using SQL Bulk Copy

for more info regarding bulk copy, you should check these links

Bulk Copy in SQL server using c#.net[^]

SQL Bulk Copy with C#.Net[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900