将一个数据库复制到另一个数据库,删除重复行

时间:2013-02-27 18:17:15

标签: c# sql-server database

我有一个名为“ovhidhan_e_word”的数据库,它有一个名为Table1的表,其中包含重复的行。但我想将Table1复制到另一个名为dic的表中数据库命名为“ovidhan”。但这次我不想重复行。我使用了这段代码:

  string english_w = "";

        SqlConnection mssql_con = new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Imon-Bayazid\Documents\ovhidhan_e_word.mdf;MultipleActiveResultSets=true;Integrated Security=True;Connect Timeout=30;User Instance=True");
        SqlConnection con2 = new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Imon-Bayazid\Documents\ovidhan.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

        mssql_con.Open();

        SqlCommand mssql_cmnd = new System.Data.SqlClient.SqlCommand("SELECT * from Table1", mssql_con);

        SqlDataReader rd = mssql_cmnd.ExecuteReader();
        while (rd.Read())
        {
            english_w = rd.GetString(0);

            con2.Open();

            SqlCommand cmnd2 = new System.Data.SqlClient.SqlCommand("select * from dic where english=@h",con2);
            cmnd2.Parameters.AddWithValue("@h",english_w);

            SqlDataReader r = cmnd2.ExecuteReader();
            if (r.Read())
            { }
               else {
                    SqlCommand c = new System.Data.SqlClient.SqlCommand("insert into dic values(@k)", con2); ;
                    c.Parameters.AddWithValue("@k", english_w);
                    c.ExecuteNonQuery();
                 }

            con2.Close();      
        }

        mssql_con.Close();

但它显示异常“已经有一个与此命令关联的开放DataReader必须先关闭。”在“ c.ExecuteNonQuery(); 行上“

我该如何解决这个问题? 另一个问题我如何从mssql中的表中删除重复行?

3 个答案:

答案 0 :(得分:0)

这是因为mssql_cmnd与您用于第一个SQLReader的SQLCommand相同。您可能想要调用SQLCommand c:

c.Parameters.AddWithValue("@k", english_w);
c.ExecuteNonQuery();

对于重复,您可以在读取数据时始终执行SELECT DISTINCT。请参阅以下链接中的标题C:

http://msdn.microsoft.com/en-us/library/ms187731.aspx

答案 1 :(得分:0)

我认为它应该是“c”而不是“mssql_cmnd”,并且还建议在连接,命令和datareader对象周围使用“using”语句。

修改

我没有测试代码,但试图重构它并用execute scalar替换数据读取器。

        string english_w = string.Empty;
        using (SqlConnection mssql_con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Imon-Bayazid\Documents\ovhidhan_e_word.mdf;MultipleActiveResultSets=true;Integrated Security=True;Connect Timeout=30;User Instance=True"))
        {
            using (SqlCommand mssql_cmnd = new SqlCommand("SELECT * from Table1", mssql_con))
            {
                mssql_con.Open();
                using (SqlDataReader rd = mssql_cmnd.ExecuteReader())
                {
                    while (rd.Read())
                    {
                        english_w = rd.GetString(0);
                        using (SqlConnection con2 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Imon-Bayazid\Documents\ovidhan.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"))
                        {
                            con2.Open();
                            using (SqlCommand cmnd2 = new SqlCommand("select top 1 * from dic where english=@h", con2))
                            {
                                cmnd2.Parameters.AddWithValue("@h", english_w);
                                object obj = cmnd2.ExecuteScalar();
                                if (obj == null)
                                {
                                    using (SqlCommand c = new SqlCommand("insert into dic values(@k)", con2))
                                    {
                                        c.Parameters.AddWithValue("@k", english_w);
                                        c.ExecuteNonQuery();
                                    }
                                }
                            }
                            con2.Close();
                        }
                    }
                    rd.Close();
                }
            }
            mssql_con.Close();
        }

答案 2 :(得分:0)

你在哪里得到错误? mssql_cmnd没有ExecuteNonQuery()如果是c,请在c.Dispose()后尝试c.ExecuteNonQuery()。试试这个删除重复的行

select * from table1 h1 where H1.ID in (select max(id) from table1 group by duplicate_row_id);