C#WinForms - SQL Server Compact Edition:不支持关键字:初始目录

时间:2015-07-15 15:31:29

标签: c# .net sql-server winforms sql-server-ce

我想从数据库服务器(SQL Server 2012)获取数据以写入我在本地数据库中的内容(SQL Server Compact Edition 4.0 - WinForms应用程序 - VS 2013)

  • 通过代码创建.SDF数据库文件 - >确定。
  • 打开与.SDF文件的本地连接 - >好。
  • 通过代码创建表格 - >确定。

  • 打开与数据库服务器的连接 - >不好 ! :(

我收到此错误消息:

  

System.ArgumentException:不支持关键字:'初始目录'

这是我的代码:

class MySqlCeEngine
{
    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=myDB;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_Address
            localCmd.CommandText = @"CREATE TABLE t_Address(
                                    Address_ID int IDENTITY(1,1) NOT NULL,
                                    Address_Main nvarchar(4000) NULL,
                                    Address_CityName nvarchar(50) NULL,
                                    Address_CityZipCode nvarchar(50) NULL,
                                    Address_CountryID int NULL,
                                    Address_CustomerID int NULL,
                                    Address_SiteID int NULL,
                                    Address_IsVisible bit NOT NULL,
                                 CONSTRAINT PK_t_AdressID PRIMARY KEY (Address_ID)
                                )";

            localCmd.ExecuteNonQuery();
            #endregion

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

                    SqlCeCommand SourceCmd = sourceCnx.CreateCommand();
                    SourceCmd.CommandText = "SELECT * FROM t_Address";
                    SqlCeDataReader reader = SourceCmd.ExecuteReader();

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(LOCAL_CONN_STRING))
                    {
                        bulkCopy.DestinationTableName = "t.Address";

                        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();
        }
    }
}

我在网上做了一些搜索,发现我应该添加" ProviderName = System.Data.SqlClient"但我仍有这个问题。

我也尝试过使用System.Data.Entity.Database类方法" OpenConnectionString"但它没有用(VS告诉我这个类不包含这个方法的定义)。

我不知道该怎么做,虽然我在这里和那里找到了一些东西。

谢谢,

地狱猫

1 个答案:

答案 0 :(得分:1)

使用SOURCE_CONN_STRING完整 SQL Server的连接必须使用SqlConnection - 而不是SqlCeConnection ...

所以改变这一行:

using (SqlCeConnection sourceCnx = new SqlCeConnection(SOURCE_CONN_STRING))

为:

using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))

并在处理完整的SQL Server实例时使用SqlCommand(而非SqlCeCommand)。