Tool For Database Backup and Query Execution

This article is about how to take a backup of a SQL Server database using a Windows application. I wrote this application using my online forum friends, first I will thank them. I use this tool to backup my databases and it executes query quickly, I hope this may be useful for you.

Let's see how it works.

1. Click the database backup.

2. Then Database (this form shows the data of the corresponding databases and tables).
 
data backup

First we get the connection of the SQL Server instance, so we give the host name, user name and password. This gets the instance of the corresponding host name.

Note: If we want to list all of the SQL Server host names then we can use the following SQL query.
  1. select * from sysservers where srvproduct='SQL Server'   
Here we add the 3 columns of the result of query in the other server name.
  1. private void btnConnect_Click(object sender, EventArgs e)  
  2.  {  
  3.           try  
  4.           {  
  5.   
  6.               string constring = "Data source="+txtServerName.Text+"; uid=" + username + "; pwd=" + password;  
  7.               string query = "select *  from sysservers  where srvproduct='SQL Server'";  
  8.               SqlDataReader dr;  
  9.               using (SqlConnection con = new SqlConnection(constring))  
  10.               {  
  11.                   con.Open();  
  12.                   using (SqlCommand cmd = new SqlCommand(query, con))  
  13.                   {  
  14.                       dr = cmd.ExecuteReader();  
  15.                       while (dr.Read())  
  16.                       {  
  17.                           cmbServer.Items.Add(dr[2]);  
  18.                       }  
  19.                   }  
  20.   
  21.               }  
  22.               dr.Close();  
  23.               if (cmbServer.Items.Count > 0)  
  24.               {  
  25.                   cmbServer.SelectedIndex = 0;  
  26.               }  
  27.           }  
  28.           catch (Exception ex)  
  29.           {  
  30.               MessageBox.Show("Please check username and password... :) ","Login error",MessageBoxButtons.OK,MessageBoxIcon.Information);  
  31.               cmbDatabase.Items.Clear();  
  32.               cmbTable.Items.Clear();  
  33.               TxtQuery.Text = "";  
  34.               GvTable.DataSource = null;  
  35.           }  
  36.       } 
After successful login we list the database using the following method.
  1. select * from sysdatabases .  
Here we get the connection of the master database, because this database has a sysdatabases table with the details of all the other databases.
  1. private void cmbServer_SelectedIndexChanged(object sender, EventArgs e)    
  2. {    
  3.            SqlDataReader dr;    
  4.            using (SqlConnection con = new SqlConnection("Data Source=" + txtServerName.Text + ";Database=Master; uid=" + username + "; pwd=" + password))    
  5.            {    
  6.                con.Open();    
  7.                string query = "select * from sysdatabases";    
  8.                using (SqlCommand cmd = new SqlCommand(query,con))    
  9.                {    
  10.                    dr = cmd.ExecuteReader();    
  11.                    while (dr.Read())    
  12.                    {    
  13.                        cmbDatabase.Items.Add(dr[0]);    
  14.                    }    
  15.                }    
  16.                if (cmbDatabase.Items.Count > 0)    
  17.                {    
  18.                    cmbDatabase.SelectedIndex = 0;    
  19.                }    
  20.            }    
  21.        }  
Respectively we add the table of the database using database cmbDatabase_SelectedIndexChanged.

Here we need the table name of the corresponding databases.

The following is the procedure.

Respectively we add the table of the database using the database cmbDatabase_SelectedIndexChanged.

Here we need the table name of the corresponding databases.

The following is the procedure.
  1. Getting the connection of the database. using Data Source=" + txtServerName.Text + ";uid=" + username + "; pwd=" + password.

  2.  "use " + cmbDatabase.Text + "; select * from sys.tables;";
    above query like
    use databasename;
    select * from tablename.

  3. We add the table names in cmbTable. 
  1. private void cmbDatabase_SelectedIndexChanged(object sender, EventArgs e)  
  2.      {  
  3.          cmbTable.Items.Clear();  
  4.          try  
  5.          {  
  6.              SqlDataReader dr;  
  7.              using (SqlConnection con = new SqlConnection("Data Source=" + txtServerName.Text + ";uid=" + username + "; pwd=" + password))  
  8.              {  
  9.                  con.Open();  
  10.                  string query = "use " + cmbDatabase.Text + "; select * from sys.tables;";  
  11.                  using (SqlCommand cmd = new SqlCommand(query, con))  
  12.                  {  
  13.                      dr = cmd.ExecuteReader();  
  14.                      while (dr.Read())  
  15.                      {  
  16.                          cmbTable.Items.Add(dr[0]);  
  17.                      }  
  18.                  }  
  19.                  if (cmbTable.Items.Count > 0)  
  20.                  {  
  21.                      cmbTable.SelectedIndex = 0;  
  22.                  }  
  23.              }  
  24.          }  
  25.          catch (Exception ex)  
  26.          {  
  27.              MessageBox.Show(ex.Message);  
  28.          }  
  29.      } 
At the same time we get the data of the selected table. The result will show in the datagrid.
  1. private void cmbTable_SelectedIndexChanged(object sender, EventArgs e)  
  2.  {  
  3.      lblrows.Text = "";  
  4.      using (SqlConnection con = new SqlConnection("Data Source="+txtServerName.Text+";Database=" + cmbDatabase.Text + ";uid=" + username + "; pwd=" + password))  
  5.      {  
  6.          con.Open();  
  7.          string query = "select * from " + cmbTable.Text;  
  8.           DataTable dt = new DataTable();  
  9.          using (SqlCommand cmd = new SqlCommand(query,con))  
  10.          {  
  11.              SqlDataAdapter da = new SqlDataAdapter(cmd);  
  12.               
  13.              da.Fill(dt);  
  14.              GvTable.DataSource = dt;  
  15.   
  16.          }  
  17.          TxtQuery.Text = query;  
  18.          if (dt.Rows.Count > 0)  
  19.          {  
  20.              lblrows.Text = dt.Rows.Count + " Rows found";  
  21.          }  
  22.          else  
  23.          {  
  24.              lblrows.Text = " ";  
  25.          }  
  26.      }  
  27.  } 
When we choose the table, by default all records of the table will show in the datagrid, we can also modify the query using the TxtQuery TextBox. Press the execute button.

You can also save the data grid to Excel

Backup the database.
  1. private void btnBackup_Click(object sender, EventArgs e)    
  2. {    
  3.      string path="";    
  4.      DialogResult result=folderBrowserDialog1.ShowDialog();    
  5.      if (result == DialogResult.OK)    
  6.      {    
  7.           path = folderBrowserDialog1.SelectedPath.ToString();    
  8.           string backup = "backup database " + cmbDatabase.Text + " to disk='" + path + "\\" + cmbDatabase.Text + ".bak'";    
  9.           ExecuteQuery(backup);    
  10.           MessageBox.Show("Database Bacukup taken");    
  11.      }    
  12.  }  
I hope this utility to use full for you.


Similar Articles