Пример #1
0
 internal static string GetEmptyDBName(string dBName)
 {
     return(DBImager.GetNewDBName("Empty_" + dBName));
 }
Пример #2
0
        /// <summary>
        /// Get Connection string
        /// </summary>
        /// <param name="DatabaseName">Name of the database (e.g. Northwind) </param>
        /// <param name="databaseType">Provider: SQL2000, SQL2005, DatabaseFile (Read from DomainServices.xml)</param>
        /// <param name="accessMode">Static Readonly database or dynamic created modifable database (Read from DomainServices.xml)</param>
        /// <returns></returns>
        public static string GetConnectionString(string dbName, List <DictionaryEntry> Ls, AccessMode accessMode)
        {
            string dbProvider = GetDBProvider(Ls);

            if (accessMode == AccessMode.Readonly)
            {
                return(GetConnectionStringForDatabaseFile(GetConnectionStringForReadOnlyDatabase(dbName, Ls)));
            }
            if (accessMode == AccessMode.Static)
            {
                // There are complications on handling DBFile (It is Read-Only after it got sync from Source Depot
                // For more information -- http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=5894
                // Work around -- copy it to the current directory
                switch (dbProvider)
                {
                case "DatabaseFile":
                    return(GetConnectionString(dbName, Ls, AccessMode.New));

                default:
                    return(GetConnectionStringForDatabaseFile(GetConnectionStringForReadOnlyDatabase(dbName + "_Static", Ls)));
                }
            }

            if (accessMode == AccessMode.Empty)
            {
                string Empty_DBName = GetEmptyDBName(dbName);
                DeleteDatabase(Empty_DBName, Ls);

                return(DBImager.GetConnectionString(Empty_DBName, Ls, DBImager.AccessMode.Readonly));
            }

            if (accessMode == AccessMode.EmptyAndCreate)
            {
                string Empty_DBName = GetEmptyDBName(dbName);
                DeleteDatabase(Empty_DBName, Ls);
                using (SqlConnection sqlcon = new SqlConnection(GetAdminConnectionString("Master", Ls)))
                {
                    SqlCommand sqlcom = new SqlCommand("CREATE DATABASE [" + Empty_DBName + "]", sqlcon);
                    sqlcom.Connection.Open();
                    sqlcom.ExecuteNonQuery();
                    sqlcom.Connection.Close();
                }

                return(DBImager.GetConnectionString(Empty_DBName, Ls, DBImager.AccessMode.Readonly));
            }
            //if (accessMode == AccessMode.New)
            //{

            //    StringBuilder sb = new StringBuilder(@"");
            //    switch (dbProvider)
            //    {
            //        case "DatabaseFile":
            //            string GUID = Guid.NewGuid().ToString();
            //            string SourceMDF = GetDBFile(dbName + ".mdf");
            //            string SourceLDF = GetDBFile(dbName + "_log.ldf");
            //            string DestMDF = Path.Combine(Directory.GetCurrentDirectory(), dbName + GUID + ".mdf");

            //            // If file is not exist, copy it to the local folder
            //            if (!File.Exists(DestMDF))
            //            {
            //                File.Copy(SourceMDF, DestMDF);
            //                File.SetAttributes(DestMDF, FileAttributes.Normal);
            //                File.Copy(SourceLDF, Path.Combine(Directory.GetCurrentDirectory(), dbName + GUID + "_log.ldf"));
            //                File.SetAttributes(Path.Combine(Directory.GetCurrentDirectory(), dbName + GUID + "_log.ldf"), FileAttributes.Normal);
            //            }

            //            return GetConnectionStringForDatabaseFile(DestMDF);
            //        default:
            //            return CreateDB(dbName, Ls);
            //    }


            //}
            if (accessMode == AccessMode.NewNoCreate || accessMode == AccessMode.New)
            {
                StringBuilder sb = new StringBuilder(@"");
                switch (dbProvider)
                {
                case "DatabaseFile":
                    Guid   GUID    = Guid.NewGuid();
                    string DestMDF = Path.Combine(Directory.GetCurrentDirectory(), String.Format("{0}{1}.mdf", dbName, GUID));
                    return(GetConnectionStringForDatabaseFile(DestMDF));

                default:
                    return(GetDBConnectionStringNoCreate(dbName, Ls));
                }
            }
            throw new Exception("Not a recognized access type");
        }