
备份部分代码:
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 WindowsFormsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
string backupPath = "";
string restorePath = "";
private void Form5_Load(object sender, EventArgs e)
{
}
public static string connstr = "server=.;database=小区物业;uid=sky;pwd=";
public static SqlConnection conn = new SqlConnection(connstr);
private void button1_Click(object sender, EventArgs e)//查找button
{
saveFileDialog1.FilterIndex = 0;
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "Bak Files (*.bak)|*.bak|All Files(*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = saveFileDialog1.FileName.ToString();
textBox1.ReadOnly = true;
}
backupPath = textBox1.Text.Trim();
}
private void button2_Click(object sender, EventArgs e)// 备份button
{
try
{
if (backupPath == "")
{
MessageBox.Show("请先选择数据库备份路径", "提示");
return;
}
if (File.Exists(backupPath))
{
File.Delete(backupPath);
}
string sqlstr;
sqlstr = "backup database 小区物业 to disk='" + backupPath + "'";
SqlCommand sqlCom = new SqlCommand(sqlstr, conn);
conn.Open();
sqlCom.ExecuteNonQuery();
conn.Close();
if (MessageBox.Show("数据库备份成功", "提示", MessageBoxButtons.OK) == DialogResult.OK)
{
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
}

求大神指点,我大后天就要交毕设了,手贱在毕设任务书上写了这个备份还原功能。
能成功的代码复制过来也行。
------解决思路----------------------
http://blog.sina.com.cn/s/blog_54c367d401017i2m.html