Pravin Ghadge

Pravin Ghadge

  • 565
  • 2.1k
  • 586.7k

Back up of SQL server 2000 Database

Jan 17 2011 2:37 AM

I have used foll. code:
This code is fine using SQl 2005 as server but not working using sql 2000

private void Form1_Load(object sender, EventArgs e)
{
// Create a DataTable where we enumerate the available servers
DataTable dtServers = SmoApplication.EnumAvailableSqlServers(true);
// If there are any servers at all
if (dtServers.Rows.Count > 0)
{
// Loop through each server in the DataTable
foreach (DataRow drServer in dtServers.Rows)
{
// Add the name to the combobox
cmbServer.Items.Add(drServer[
"Name"]);
}
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
// If a server was selected at all from the combobox
if (cmbServer.SelectedItem != null && cmbServer.SelectedItem.ToString() != "")
{
// Create a new connection to the selected server name
ServerConnection srvConn = new ServerConnection(cmbServer.SelectedItem.ToString());
// Log in using SQL authentication instead of Windows authentication
srvConn.LoginSecure =
false;
// Give the login username
srvConn.Login = txtUsername.Text;
// Give the login password
srvConn.Password = txtPassword.Text;
// Create a new SQL Server object using the connection we created
srvSql =
new Server(srvConn);
// Loop through the databases list
foreach (Database dbServer in srvSql.Databases)
{
// Add database to combobox
cmbDatabase.Items.Add(dbServer.Name);
}
}
else
{
// A server was not selected, show an error message
MessageBox.Show("Please select a server first", "Server Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnCreate_Click(object sender, EventArgs e)
{
 
if (srvSql != null)
{
 
if (saveBackupDialog.ShowDialog() == DialogResult.OK)
{
 
Backup bkpDatabase = new Backup();
 
bkpDatabase.Action =
BackupActionType.Database;
 
bkpDatabase.Database = cmbDatabase.SelectedItem.ToString();
 
BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName, DeviceType.File);
 
bkpDatabase.Devices.Add(bkpDevice);
 
bkpDatabase.SqlBackup(srvSql);
MessageBox.Show("Back Up completed...", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
 
}

Answers (4)