C# WinForms - SQL Server CE - SqlCeDataReader loses its data?

c# sqlbulkcopy sql-server sql-server-ce winforms

Question

I am trying to copy data from SQL Server database to SQL Server CE local database.

Here is my method :

private const string LOCAL_SDF_FILE = "LocalDB.sdf";
private const string LOCAL_CONN_STRING = "Data Source='|DataDirectory|LocalDB.sdf'; LCID=1033; Password=3C670F044A; Encrypt=TRUE;";
private const string SOURCE_CONN_STRING = "Data Source=SQL\\SERVER;Initial Catalog=DB;Integrated Security=True;Pooling=False";

public static void CreateDB()
{
        if (File.Exists(LOCAL_SDF_FILE))
        {
            File.Delete(LOCAL_SDF_FILE);
        }

        SqlCeEngine engine = new SqlCeEngine(LOCAL_CONN_STRING);
        engine.CreateDatabase();
        engine.Dispose();

        SqlCeConnection localCnx = null;

        try
        {
            localCnx = new SqlCeConnection(LOCAL_CONN_STRING);
            localCnx.Open();

            SqlCeCommand localCmd = localCnx.CreateCommand();

            #region CREATE TABLE t_TypeConfig
            localCmd.CommandText = @"CREATE TABLE t_TypeConfig(
                                        TypeConfig_ID int IDENTITY(1,1) NOT NULL,
                                        TypeConfig_Name nvarchar(50) NOT NULL,
                                        TypeConfig_IsVisible bit NOT NULL,
                                        CONSTRAINT pk_TypeConfigID PRIMARY KEY (TypeConfig_ID)
                                    )";

            localCmd.ExecuteNonQuery();
            #endregion

            using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))
            {
                try
                {
                    sourceCnx.Open();

                    SqlCommand SourceCmd = sourceCnx.CreateCommand();
                    SourceCmd.CommandText = "SELECT * FROM t_TypeConfig";
                    SqlDataReader reader = SourceCmd.ExecuteReader();

                    using (SqlCeBulkCopy bulkCopy = new SqlCeBulkCopy(LOCAL_CONN_STRING))
                    {
                        bulkCopy.DestinationTableName = "t_TypeConfig";

                        try
                        {
                            // Write from the source (DB server) to the destination (local wibe)
                            bulkCopy.WriteToServer(reader);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "An error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "An error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    sourceCnx.Close();
                }
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString(), "An error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            localCnx.Close();
        }
    }
  • Create SQL Server CE file and sample table (t_TypeConfig) -> ok.
  • Get data into reader from SQL Server connection -> ok.

But when I want to fill, via SqlCeBulkCopy, my local database table with the reader I just filled previously, I see via Visual Studio watcher that reader does not contain the data anymore !

I don't know why. Still looking for a solution but I don't understand ? Maybe because the using ?

I also tried to convert the SqlDataReader into a SqlCeDataReader variable to give in SqlCeBulkCopy.WriteToServer() parameter but it cannot convert from one to other.

Any ideas?

Thanks a lot,

Hellcat.

Accepted Answer

Okay so the solution is, thanks to guys in comments, to give as parameter to SqlCeBulkCopy.WriteToServer() a DataTable instead of a SqlDataReader or IDataReader. It works for me right now.

Added this :

SqlDataReader reader = SourceCmd.ExecuteReader();
DataTable SqlDatatable = new DataTable();
SqlDatatable.Load(reader);

Then :

bulkCopy.WriteToServer(SqlDatatable);

Thanks guys.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why