C# WinForms - SQL 服务器精简版:不支持关键字:初始目录

C# WinForms - SQL Server Compact Edition : keyword not supported : initial catalog

我想从数据库服务器 (SQL Server 2012) 获取数据,将我得到的内容写入本地数据库 (SQL Server Compact Edition 4.0 - WinForms app - VS 2013)

我收到此错误消息:

System.ArgumentException: Keyword not supported: 'initial catalog'

这是我的代码:

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 class 方法 "OpenConnectionString" 但它不起作用(VS 告诉我这个 class 不包含这个的定义方法)。

虽然我正在尝试一些随处可见的东西,但我不知道该怎么做。

谢谢,

地狱猫

使用 SOURCE_CONN_STRINGfull SQL 服务器的连接必须使用 SqlConnection - 而不是 SqlCeConnection .. .

因此更改此行:

using (SqlCeConnection sourceCnx = new SqlCeConnection(SOURCE_CONN_STRING))

至:

using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))

并在处理完整的 SQL 服务器实例时使用 SqlCommand(而不是 SqlCeCommand)。