【问题标题】:C# WinForms - SQL Server CE - SqlCeDataReader loses its data?C# WinForms - SQL Server CE - SqlCeDataReader 丢失数据?
【发布时间】:2015-07-16 09:44:39
【问题描述】:

我正在尝试将数据从 SQL Server 数据库复制到 SQL Server CE 本地数据库。

这是我的方法:

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();
        }
    }
  • 创建 SQL Server CE 文件和示例表 (t_TypeConfig) -> 好的。
  • 从 SQL Server 连接获取数据到阅读器 -> 好的。

但是当我想通过 SqlCeBulkCopy 用我之前填写的阅读器填充我的本地数据库表时,我通过 Visual Studio watcher 看到 reader 不再包含数据!

我不知道为什么。仍在寻找解决方案,但我不明白?也许是因为使用?

我还尝试将SqlDataReader 转换为SqlCeDataReader 变量以提供SqlCeBulkCopy.WriteToServer() 参数,但它无法从一个转换为另一个。

有什么想法吗?

非常感谢,

地狱猫。

【问题讨论】:

  • @marc_s SqlCeBulkCopy 可以很好地与 DbDataReader 一起使用,所以这应该可以。您是否让该过程完成并检查数据库?你找对地方了吗:bin/debug 文件夹!
  • 是的,我查看了 bin/debug 文件夹以找到我的 .sdf 文件。我还尝试处理 IDataReader 而不是 SqlDataReader。我通过 Visual Studio 的 SQL Server Compact/SQLite 工具箱检查 .sdf 文件内容。但它是空的。第一次,“reader”包含行,第二次使用“reader”,它是空的,我不明白。

标签: c# sql-server winforms sql-server-ce sqlbulkcopy


【解决方案1】:

好的,感谢 cmets 中的人,解决方案是给 SqlCeBulkCopy.WriteToServer() 一个 DataTable 而不是 SqlDataReaderIDataReader 作为参数。 它现在对我有用。

添加了这个:

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

然后:

bulkCopy.WriteToServer(SqlDatatable);

谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    相关资源
    最近更新 更多