Monday, March 19, 2012

DB restore with SMO

Am using

if (currentDb != null)

myServer.KillAllProcesses(destinationDatabaseName);

before i restore the currentDb.

After restoring i use

currentDb.SetOnline();

but if am trying to connect to the database after the restore operation it throwing an error.

"A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)"

Am trying to restore the DB on which am currently working.

Can you post all of the code including the restore code?|||

protected void btnRestore_Click(object sender, EventArgs e)

{

string[] words = ConfigurationManager.AppSettings["ConnectionString1"].ToString().Split(';');

ServerConnection srvConn = new ServerConnection(words[0]);

// Log in using SQL authentication instead of Windows authentication

srvConn.LoginSecure = false;

// Give the login username

srvConn.Login = words[2];

// Give the login password

srvConn.Password = words[3];

// Create a new SQL Server object using the connection we created

srvSql = new Server(srvConn);

// If there was a SQL connection created

if (srvSql != null)

{

try

{

Database currentDb = srvSql.Databases[words[1]];

if (currentDb != null)

srvSql.KillAllProcesses(words[1]);

// Create a new database restore operation

Restore rstDatabase = new Restore();

// Set the restore type to a database restore

rstDatabase.Action = RestoreActionType.Database;

// Set the database that we want to perform the restore on

rstDatabase.Database = words[1];

// Set the backup device from which we want to restore, to a file

BackupDeviceItem bkpDevice = new BackupDeviceItem("C:\\Inetpub\\wwwroot\\take_backup_SMO\\bakup1.BAK", DeviceType.File);

rstDatabase.Devices.Add(bkpDevice);

// If the database already exists, replace it

rstDatabase.ReplaceDatabase = true;

// Perform the restore

rstDatabase.SqlRestore(srvSql);

currentDb = srvSql.Databases[words[1]];

currentDb.SetOnline();

show();

}

catch (Exception ex)

{

string s = ex.Message.ToString();

Label1.Text = s;

}

}

}

protected void show()

{

SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString2"].ToString());

con1.Open();

SqlCommand cmd = new SqlCommand("select * from std1", con1);

SqlDataAdapter ad = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

ad.Fill(ds);

DropDownList1.DataSource =ds;

DropDownList1.DataTextField = "name";

DropDownList1.DataValueField = "name";

DropDownList1.DataBind();

con1.Close();

}

No comments:

Post a Comment