Monday, 3 December 2012

backup a database in c#.net

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;


namespace WindowsFormsApplication23
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region
        SqlConnection cn = new SqlConnection(@"Data Source=xxx;Initial Catalog=Test;UID=yyy;PWD=zzz");
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand cm = new SqlCommand();
        DataTable dt = new DataTable();
        #endregion
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
        string s;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                cn.Open();
                string destdir = @"\\DBS\Netlogon\Jyoyi\backupdb4";
                if (!Directory.Exists(destdir))
                {
                    Directory.CreateDirectory(@"\\DBS\Netlogon\Jyoyi\backupdb4");
                }
                 s = "" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak";
                cm = new SqlCommand("backup database Test to disk='" + s + "'", cn);
                cm.ExecuteNonQuery();
                MessageBox.Show("database backuped.");
                cn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
}

No comments:

Post a Comment