C#数据库的备份(SQL2005)
[csharp]
using System;
[csharp]
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.txtName.Text = "数据库名" + System.DateTime.Now.ToShortDateString();
}
//将数据库备份到应用程序的跟目录下
string connectionString = "server=服务器名;database=数据库名;uid=用户名;pwd=密码";
private void btn_beifen_Click(object sender, EventArgs e)
{
try
{
string strg = Application.StartupPath.ToString();
strg = strg.Substring(0, strg.LastIndexOf("\\"));
strg = strg.Substring(0, strg.LastIndexOf("\\"));
strg += @"\Data";
string sqltxt = @"BACKUP DATABASE 数据库名 TO Disk='" + strg + "\\" + txtPath.Text + ".bak" + "'";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand(sqltxt, con);
cmd.ExecuteNonQuery();
con.Close();
if (MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
//选择保存目录事件
private void btn_open_Click(object sender, EventArgs e)
{
folderBrowserDialog1.Description = "请选择备份文件将要保存到的文件夹,如有必要你可以\n通过单击左下角的‘新建文件夹’按钮新建文件夹";
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtPath.Text = folderBrowserDialog1.SelectedPath.ToString();
}
}
//开始备份数据到指定目录下的指定文件名
private void btn_ok_Click(object sender, EventArgs e)
{
string filepath="D:\\机房设备资源管理系统数据库备份\\";
try
{
if (txtName.Text != "")
{
if (txtPath.Text != "")
{
filepath=txtPath.Text.Trim()+"\\";
}else
{
if (MessageBox.Show("你确定要要将数据备份到默认位置:" + filepath + "下吗?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
{
MessageBox.Show("请选择保存位置!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btn_open_Click(sender,
补充:软件开发 , C# ,