Esempio n. 1
0
        /// <summary>Initializes the store, ensuring that an instance of the server is running and a database is attached.</summary>
        protected override void InternalInitialize()
        {
            DbConnectionStringBuilder builder = new DbConnectionStringBuilder();

            builder.ConnectionString = ConnectionString;

            if (builder.ContainsKey("MultipleActiveResultSets"))
            {
                _supportsMARS = "True" == ((string)builder["MultipleActiveResultSets"]);
            }


            _supportsUpdatableCursor = false;

            if (_shouldEnsureDatabase)
            {
                string databaseName = null;
                if (builder.ContainsKey("Initial Catalog"))
                {
                    databaseName = (string)builder["Initial Catalog"];
                    builder["Initial Catalog"] = "master";
                }
                else if (builder.ContainsKey("Database"))
                {
                    databaseName        = (string)builder["Database"];
                    builder["Database"] = "master";
                }

                if (!String.IsNullOrEmpty(databaseName))
                {
                    if (!Parser.IsValidIdentifier(databaseName))
                    {
                        throw new ArgumentException("Database name specified in store connection string is not a valid identifier.");
                    }

                    try
                    {
                                                #if USESQLCONNECTION
                        using (MSSQLConnection connection = new MSSQLConnection(builder.ConnectionString))
                        {
                            connection.Execute(String.Format("if not exists (select * from sysdatabases where name = '{0}') create database {0}", databaseName));
                                                        #else
                        SqlConnection connection = new SqlConnection(builder.ConnectionString);
                        connection.Open();
                        SqlCommand command = connection.CreateCommand();
                        command.CommandType = CommandType.Text;
                        command.CommandText = String.Format("if not exists (select * from sysdatabases where name = '{0}') create database {0}", databaseName);
                        command.ExecuteNonQuery();
                                                        #endif
                        }
                    }
                    catch
                    {
                        // Do not rethrow, this does not necessarily indicate failure, let the non-existence of the database throw later
                    }
                }
            }
        }