Download the attached project for more details.
The code in this project looks like this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//namespace import avaiable in Microsoft.SqlServer.ConnectionInfo and Microsoft.SqlServer.Smo
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace BackUp_Utility
{
public partial class frmbackup : Form
{
//Object if server class
public static Server srv;
public frmbackup()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
//cmbsrv_name is name of combo box in which server name will be loaded
if (cmbsrv_name.SelectedItem != null && cmbsrv_name.SelectedItem.ToString() != "")
{
//ServerConnection Class to connect with sql server.
ServerConnection srvcon = new ServerConnection(cmbsrv_name.SelectedItem.ToString());
srvcon.LoginSecure = false;
//txtuserid is name of textbox to accept servr user name from user.
srvcon.Login = txtuserid.Text;
//txtpassword name of textbox to accept ppassword of server to be entered by user.
srvcon.Password = txtPassword.Text;
srv = new Server(srvcon);
//loop to load all database that are available in that particular server
foreach (Database db in srv.Databases)
{
//cmbdatabse in which all database realted to server willl be loaded
cmbDatabase.Items.Add(db.Name);
}
}
else
{
MessageBox.Show("Please select a server first", "Server Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//btnBackup is name of button to take backup
private void btnBackup_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog sfd = new SaveFileDialog();
if (srv != null)
{
sfd.Title = "BackUp To...";
sfd.Filter = "Backup File|*.bak";
sfd.FileName = "Backup_" + DateTime.Now.ToLongDateString() + ".bak";
if (sfd.ShowDialog() == DialogResult.OK)
{
Zeeshan AnwarPosted Aug 31, 2009, 7:25 AM
Error occured in this application, how can i solve it {"Backup failed for Server '.\\SQLEXPRESS'. "}<!--Session data-->