コード例 #1
0
    public void CreateDatabase(string fileName, string password)
    {
        string connectionString;

        if (System.IO.File.Exists(fileName))
        {
            return;
        }
        //System.IO.File.Delete(fileName);

        connectionString = ConfigurationParameters.GetConnectionString(fileName, password);

        // we'll use the SqlServerCe connection object to get the database file path
        using (SqlCeConnection localConnection = new SqlCeConnection(connectionString))
        {
            // The SqlCeConnection.Database property contains the file parth portion
            // of the database from the full connectionstring
            if (!System.IO.File.Exists(localConnection.Database))
            {
                using (SqlCeEngine sqlCeEngine = new SqlCeEngine(connectionString))
                {
                    sqlCeEngine.CreateDatabase();
                    CreateInitialDatabaseObjects(connectionString);
                }
            }
        }
    }
コード例 #2
0
    public static void ExecuteScript(string fileName, string password, string scriptFile, bool embeddedResource)
    {
        string connectionString = ConfigurationParameters.GetConnectionString(fileName, password);

        using (SqlCeConnection connection = new SqlCeConnection(connectionString))
        {
            string script = String.Empty;
            if (embeddedResource)
            {
                Assembly assem = Assembly.GetExecutingAssembly();
                using (Stream stream = assem.GetManifestResourceStream(scriptFile))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        script = reader.ReadToEnd();
                    }
                }
            }
            else
            {
                using (StreamReader reader = new StreamReader(scriptFile))
                {
                    script = reader.ReadToEnd();
                }
            }

            // Using the SQL Management Studio convention of using GO to identify individual commands
            // Create a list of commands to execute
            string[] commands = script.Split(
                new string[] { "GO" }, StringSplitOptions.RemoveEmptyEntries);

            SqlCeCommand cmd = new SqlCeCommand();
            cmd.Connection = connection;
            connection.Open();
            foreach (string command in commands)
            {
                string command2 = command.Trim();
                if (!String.IsNullOrEmpty(command2))
                {
                    cmd.CommandText = command2;
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }