当前位置: 首页>>代码示例>>C#>>正文


C# Server.DetachDatabase方法代码示例

本文整理汇总了C#中Server.DetachDatabase方法的典型用法代码示例。如果您正苦于以下问题:C# Server.DetachDatabase方法的具体用法?C# Server.DetachDatabase怎么用?C# Server.DetachDatabase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Server的用法示例。


在下文中一共展示了Server.DetachDatabase方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReplaceDatabase

    public void ReplaceDatabase(string connectionString, string dbReplacementPath)
    {
      var connection = new SqlConnectionStringBuilder(connectionString);
      var databaseName = connection.InitialCatalog;
      if (!File.Exists(dbReplacementPath))
      {
        Log.Error($"Can't find file by path {dbReplacementPath}", this);
        return;
      }

      ServerConnection serverConnection = null;
      try
      {
        serverConnection = new ServerConnection(new SqlConnection(connectionString));
        var server = new Server(serverConnection);

        if (server.Databases[databaseName] != null)
        {
          server.KillAllProcesses(databaseName);
          server.DetachDatabase(databaseName, true);
        }

        server.AttachDatabase(databaseName,new StringCollection() {dbReplacementPath}, AttachOptions.RebuildLog);
      }
      catch (Exception ex)
      {
        Log.Error(ex.Message, ex, this);
      }
      finally
      {
        serverConnection?.Disconnect();
      }
    }
开发者ID:kamsar,项目名称:Habitat,代码行数:33,代码来源:DatabaseService.cs

示例2: DeleteDB

 public void DeleteDB()
 {
     ServerConnection c = new ServerConnection(".", "sa", "hudongsoft");
     Server s = new Server(c);
     if (s.Databases[publicationDatabase] != null)
     {
         s.DetachDatabase(publicationDatabase, false);
     }
 }
开发者ID:honj51,项目名称:ideacode,代码行数:9,代码来源:SMO.cs

示例3: DetachDatabase

 private void DetachDatabase(Server server, string logicalName)
 {
   try
   {
     server.DetachDatabase(logicalName, false);
     Application.Current.MainWindow.Close();
   }
   catch (Exception ex)
   {
     Helper.HandleError("Cannot detach the database. ", ex);
   }
 }
开发者ID:alienlab,项目名称:Database-Management-Tool,代码行数:12,代码来源:DetachDatabaseCommand.cs

示例4: DetachDatabase

        private static void DetachDatabase(string serverName, string databaseName)
        {
            Console.WriteLine(@"Attempting to detach database '{0}\{1}", serverName, databaseName);
            // Initialise server object.
            var server = new Server(serverName);

            // Check if database is current attached to sqlexpress.
            if (!server.Databases.Contains(databaseName))
            {
                Console.WriteLine("Server does not contain db '{0}'", databaseName);
                return;
            }
            Database db = server.Databases[databaseName];

            try
            {
                DataFile dataFile = db.FileGroups[0].Files[0];
                if (!File.Exists(dataFile.FileName))
                {
                    Console.WriteLine("'{0}' does not exist. Attempting to detach without altering db..",
                                      dataFile.FileName);
                }
                db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
            }
            catch (Exception e)
            {
                Console.WriteLine("Execution failure exception. Continuing.");
                Console.WriteLine(e);
            }

            try
            {
                server.KillAllProcesses(db.Name);
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not kill all processes");
                Console.WriteLine(e);
            }

            Console.WriteLine("Altering database '{0}'", db.Name);
            try
            {
                db.Alter(TerminationClause.FailOnOpenTransactions);
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not alter the database.");
                Console.WriteLine(e);
            }

            try
            {
                Console.WriteLine("Detaching existing database before restore ...");
                server.DetachDatabase(db.Name, false);
            }
            catch (Exception e)
            {
                Console.WriteLine("Detach failed, let's continue anyways.");
                Console.WriteLine(e);
            }
        }
开发者ID:rhoadsce,项目名称:Subtext,代码行数:62,代码来源:DatabaseHelper.cs

示例5: CreateDatabase

		public static void CreateDatabase(Identifier claimedId, string friendlyId, string databaseName) {
			const string SqlFormat = @"
{0}
GO
EXEC [dbo].[AddUser] 'admin', 'admin', '{1}', '{2}'
GO
";
			var removeSnippets = new string[] { @"
IF IS_SRVROLEMEMBER(N'sysadmin') = 1
    BEGIN
        IF EXISTS (SELECT 1
                   FROM   [master].[dbo].[sysdatabases]
                   WHERE  [name] = N'$(DatabaseName)')
            BEGIN
                EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
    SET HONOR_BROKER_PRIORITY OFF 
    WITH ROLLBACK IMMEDIATE';
            END
    END
ELSE
    BEGIN
        PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
    END


GO" };
			string databasePath = HttpContext.Current.Server.MapPath("~/App_Data/" + databaseName + ".mdf");
			StringBuilder schemaSqlBuilder = new StringBuilder();
			using (var sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(DefaultNamespace + ".CreateDatabase.sql"))) {
				schemaSqlBuilder.Append(sr.ReadToEnd());
			}
			foreach (string remove in removeSnippets) {
				schemaSqlBuilder.Replace(remove, string.Empty);
			}
			schemaSqlBuilder.Replace("$(Path1)", HttpContext.Current.Server.MapPath("~/App_Data/"));
			schemaSqlBuilder.Replace("WEBROOT", databasePath);
			schemaSqlBuilder.Replace("$(DatabaseName)", databaseName);

			string sql = string.Format(CultureInfo.InvariantCulture, SqlFormat, schemaSqlBuilder, claimedId, "Admin");

			var serverConnection = new ServerConnection(".\\sqlexpress");
			try {
				serverConnection.ExecuteNonQuery(sql);
			} finally {
				try {
					var server = new Server(serverConnection);
					server.DetachDatabase(databaseName, true);
				} catch (FailedOperationException) {
				}
				serverConnection.Disconnect();
			}
		}
开发者ID:jorgemuza,项目名称:dotnetopenid,代码行数:52,代码来源:Utilities.cs

示例6: upload_DoWork


//.........这里部分代码省略.........
                t.CopyData = true;
                t.DestinationServer = destinationServer;
                t.DestinationDatabase = destinationDatabase;

                // if username or password entered so server must be create for mixed authentication 
                // else connect to server simple only with its name for windows authentication
                t.DestinationLoginSecure = true;   // for windows authentication
                if (!String.IsNullOrEmpty(username) || !String.IsNullOrEmpty(pass))
                {
                    t.DestinationLoginSecure = false;
                    t.DestinationLogin = username;
                    t.DestinationPassword = pass;
                }

                t.Options.ContinueScriptingOnError = true;
                t.Options.IncludeIfNotExists = true;
                t.DropDestinationObjectsFirst = true;
                //t.UseDestinationTransaction = true;   <<Check Here>>  (long time exec)
                if (createDb == true) t.CreateTargetDatabase = true;
                progress = 10;

                // Transfer Schema and Data
                t.TransferData();
                progress = 11;

                #endregion


                #region DestructEveryThing

                progress = 12;
                if (!serverMode)
                {
                    server.DetachDatabase(sourceDatabase, false);
                }
                progress = 13;

                #endregion


                progress = 14;
                MessageBox.Show("پایگاه داده ها با موفقیت کامل بر روی وب قرار گرفت", "ارسال موفق", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                #region DetachDb

                if (attached && !serverMode)
                {
                    try
                    {
                        Server server = new Server(sourceServer);
                        server.DetachDatabase(sourceDatabase, false);
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show("خطایی در هنگام حذف پایگاه داده های موقتی در همین سیستم رخ داده است", "خطا", MessageBoxButton.OK, MessageBoxImage.Error);
                        MessageBox.Show("خطای تکنیکی \n" + exc.Message + "\n" + exc.StackTrace, "خطا", MessageBoxButton.OK, MessageBoxImage.Error);
                        if (exc.InnerException.InnerException != null)
                        {
                            MessageBox.Show("خطای تکنیکی داخلی \n" + exc.InnerException.InnerException.Message, "خطا", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }

                #endregion
开发者ID:omid55,项目名称:real_state_manager,代码行数:67,代码来源:UploadDbWnd.xaml.cs

示例7: Detach

 public void Detach(string database)
 {
     try
     {
         var cnn = new SqlConnection(_mConnectString);
         cnn.Open();
         var conn = new ServerConnection(cnn);
         var myServer = new Server(conn);
         myServer.KillAllProcesses(database);
         myServer.DetachDatabase(database, true);
         RaiseDetachCompleteFinishEventHander();
         cnn.Close();
     }
     catch (Exception ex)
     {
         RaiseDetachErrorEventHander(ex.Message);
     }
 }
开发者ID:gojila,项目名称:Quan_Ly_Cam_Do,代码行数:18,代码来源:DataController.cs


注:本文中的Server.DetachDatabase方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。