本文整理汇总了C#中Server.AttachDatabase方法的典型用法代码示例。如果您正苦于以下问题:C# Server.AttachDatabase方法的具体用法?C# Server.AttachDatabase怎么用?C# Server.AttachDatabase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Server
的用法示例。
在下文中一共展示了Server.AttachDatabase方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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();
}
}
示例2: LoadDatabase
private static void LoadDatabase()
{
//Connect to the server
var connectionString = "Data Source=localhost\\sqlexpress;Initial Catalog=master;Integrated Security=SSPI";
var con = new SqlConnection(connectionString);
ServerConnection serverConnection = new ServerConnection(con);
Server sqlServer = new Server(serverConnection);
System.IO.FileInfo mdf = new System.IO.FileInfo(@"Buzzle.mdf");
System.IO.FileInfo ldf = new System.IO.FileInfo(@"Buzzle_log.LDF");
var dbPath = sqlServer.MasterDBPath;
var databasePath = dbPath + @"\Buzzle.mdf";
var databaseLogPath = dbPath + @"\Buzzle_log.LDF";
File.Copy(mdf.FullName, databasePath, true);
File.Copy(ldf.FullName, databaseLogPath, true);
var databasename = mdf.Name.ToLower().Replace(@".mdf", @"");
System.Collections.Specialized.StringCollection databasefiles = new System.Collections.Specialized.StringCollection();
databasefiles.Add(databasePath);
databasefiles.Add(databaseLogPath);
sqlServer.AttachDatabase(databasename, databasefiles);
}
示例3: CreatDB
public void CreatDB(string dataFileDir)
{
ServerConnection c = new ServerConnection(".", "sa", "hudongsoft");
Server s = new Server(c);
//s.AttachDatabase(
//Database db = new Database(s, DataBaseName);
//db.Create();
System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
sc.Add(dataFileDir + @"\HdHouse.mdf");
sc.Add(dataFileDir + @"\HdHouse_log.ldf");
s.AttachDatabase(publicationDatabase, sc);
}
示例4: AttachDatabase
private static void AttachDatabase(string databaseName, string databaseFile)
{
// Connect to the SQL Server
var server = new Server("(local)");
// Attach the database
server.AttachDatabase(databaseName, new StringCollection { databaseFile });
while (server.Databases[databaseName].State != SqlSmoState.Existing)
{
Thread.Sleep(100);
}
}
示例5: AttachDatabase
private void AttachDatabase(Server server, string logicalName, string physicalPath)
{
try
{
server.AttachDatabase(logicalName, new StringCollection { physicalPath });
Application.Current.MainWindow.Close();
}
catch (Exception ex)
{
Helper.HandleError("Cannot attach the database. ", ex);
}
}
示例6: AttachDatabase
/// <summary>
/// Attaches the database.
/// </summary>
/// <param name="databaseName">Name of the database.</param>
/// <param name="databaseFile">The database file.</param>
public static void AttachDatabase(string databaseName, string databaseFile)
{
var server = new Server(TestConfig.DatabaseServer);
// Drop the database
DropDatabase(server, databaseName);
server.AttachDatabase(databaseName, new StringCollection { databaseFile });
while (server.Databases[databaseName].State != SqlSmoState.Existing)
{
Thread.Sleep(100);
}
}
示例7: ExecuteTask
protected override void ExecuteTask()
{
var server = new Server(ServerName);
server.ConnectionContext.LoginSecure = true;
server.ConnectionContext.Connect();
File.Move(MDFPath, Path.Combine(server.Settings.DefaultFile, DBName + ".mdf"));
File.Move(LDFPath, Path.Combine(server.Settings.DefaultFile, DBName + ".ldf"));
var mdf = Path.Combine(server.Settings.DefaultFile, DBName + ".mdf");
var ldf = Path.Combine(server.Settings.DefaultFile, DBName + ".ldf");
if (!ReplaceDB)
{
if (server.Databases.Contains(DBName))
{
Log(Level.Info, DBName + " exists on " + ServerName + " and will not be replaced");
return;
}
}
if (server.Databases.Contains(DBName))
{
Log(Level.Info, "Removing existing database and replacing it");
try
{
server.Databases[DBName].Drop();
}
catch (Exception e)
{
if (Verbose)
Log(Level.Info, "Killing connections to " + DBName);
server.KillAllProcesses(DBName);
server.Databases[DBName].Drop();
}
Log(Level.Info, "Database removed");
}
Log(Level.Info, "Attaching database " + DBName + " to " + ServerName);
var sc = new StringCollection {mdf, ldf};
if (string.IsNullOrEmpty(Owner))
server.AttachDatabase(DBName, sc);
else
server.AttachDatabase(DBName, sc, Owner);
Log(Level.Info, DBName + " attached to " + ServerName);
if (server.ConnectionContext.IsOpen)
server.ConnectionContext.Disconnect();
}
示例8: upload_DoWork
void upload_DoWork(object sender, DoWorkEventArgs e)
{
if (upload.CancellationPending)
{
e.Cancel = true;
return;
}
bool again = false;
bool attached = false;
First:
try
{
#region BackUp First
if (!serverMode)
{
BackUpDatabase bak = new BackUpDatabase();
bak.doBackUp();
}
#endregion
#region InitVariables and Save Info In File
progress = 1;
again = false;
attached = false;
progress = 2;
StreamWriter sw = new StreamWriter("UploadConfig.conf");
sw.WriteLine(destinationServer);
sw.WriteLine(destinationDatabase);
sw.WriteLine(username);
sw.WriteLine(MyCrypt.encryptWithDefaultValues(pass));
if (serverMode)
{
sw.WriteLine(sourceServer);
sw.WriteLine(sourceDatabase);
}
sw.Close();
sw.Dispose();
#endregion
#region ServerCreation and Delete All Objects On Destination Database
progress = 3;
ServerConnection destinationCon;
if (!String.IsNullOrEmpty(username) || !String.IsNullOrEmpty(pass))
{
destinationCon = new ServerConnection(destinationServer, username, pass);
}
else
{
destinationCon = new ServerConnection(destinationServer);
}
Server destServer = new Server(destinationCon);
String fp = Environment.CurrentDirectory + "\\query.sql";
FileInfo file = new FileInfo(fp);
string script = file.OpenText().ReadToEnd();
script = script.Replace("dbo", username);
try
{
destServer.ConnectionContext.ExecuteNonQuery(script);
}
catch (Exception excep)
{
MessageBox.Show("Error In Removing All Objects \n" + excep.Message, "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
//MessageBox.Show("خطای تکنیکی \n" + excep.InnerException.Message + "\n" + excep.StackTrace, "خطا", MessageBoxButton.OK, MessageBoxImage.Error);
}
// Connect to server
Server server = new Server(sourceServer);
progress = 4;
#endregion
#region AttachDatabaseOnServer
progress = 5;
if (!serverMode)
{
System.Collections.Specialized.StringCollection files = new System.Collections.Specialized.StringCollection();
string datab = Environment.CurrentDirectory + "\\Negindb.mdf";
string logDb = Environment.CurrentDirectory + "\\Negindb_log.ldf";
files.Add(datab);
server.AttachDatabase(sourceDatabase, files);
attached = true;
server.Refresh();
}
progress = 6;
#endregion
#region DatabaseCreation
progress = 7;
//.........这里部分代码省略.........