繁体   English   中英

C#WinForms-SQL Server CE-SqlCeDataReader丢失其数据?

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

我正在尝试将数据从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()参数,但无法将其相互转换。

有任何想法吗?

非常感谢,

地狱猫

好的,由于有评论的人们,解决方案是为SqlCeBulkCopy.WriteToServer()一个DataTable而不是SqlDataReaderIDataReader作为参数。 现在对我有用。

添加了:

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

然后 :

bulkCopy.WriteToServer(SqlDatatable);

多谢你们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM