C#实现的SQL备份与还原功能示例
作者:a771948524 时间:2021-09-03 17:53:17
本文实例讲述了C#实现的SQL备份与还原功能。分享给大家供大家参考,具体如下:
//记得加 folderBrowserDialog1 openFileDialog1 控件
using System.Data.SqlClient; //连接数据库 公共变量
namespace WindowsApplication1.GoodMenhod
{
class getSqlConnection
{
string sql = "Data Source=win7-pc;database=Kc;uid=sa;pwd=sa";
SqlConnection conn;
public SqlConnection GetCon()
{
conn = new SqlConnection(sql);
conn.Open();
return conn;
}
}
}
using System.Data.SqlClient;
using WindowsApplication1.GoodMenhod; //引用命名空间
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //打开 备份路径
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtPath.Text = folderBrowserDialog1.SelectedPath.ToString();
}
}
private void button2_Click(object sender, EventArgs e) //备份名称 保存
{
try
{
if (txtPath.Text != "" )
{
getSqlConnection geCon = new getSqlConnection();
SqlConnection con = geCon.GetCon();
string strBacl = "backup database Kc to disk='" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak'";
SqlCommand Cmd = new SqlCommand(strBacl, con);
if (Cmd.ExecuteNonQuery() != 0)
{
MessageBox.Show("数据备份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
else
{
MessageBox.Show("数据备份失败!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("请填写备份的正确位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}// end
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
}
}
}
private void button3_Click(object sender, EventArgs e) //打开 将要还原的文件
{
openFileDialog1.FilterIndex = 0;
openFileDialog1.FileName = "";
openFileDialog1.Filter = "txt files (*.bak)|*.bak|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textPaht.Text = openFileDialog1.FileName.ToString();
}
}
private void button4_Click(object sender, EventArgs e) //还原
{
if (textPaht.Text != "")
{
getSqlConnection geCon = new getSqlConnection();
SqlConnection con = geCon.GetCon();
if (con.State == ConnectionState.Open)
{
con.Close();
}
//连接的数据库是master,所以要初始化新的连接字符串
string DateStr = "Data Source=win7-pc;Database=master;User id=sa;PWD=sa";
SqlConnection conn = new SqlConnection(DateStr);
conn.Open();
//-------------------杀掉所有连接 db_CSManage 数据库的进程--------------
// string sql = " SELECT spid FROM master..sysprocesses WHERE dbid=db_id('" + strDBName + "')";
string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'Kc') ";//读取连接当前数据库的进程
SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);
DataTable spidTable = new DataTable();
Da.Fill(spidTable);
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.Text;
Cmd.Connection = conn;
for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++)
{
Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString(); //强行关闭用户进程
Cmd.ExecuteNonQuery();
}
conn.Close();
conn.Dispose();
//--------------------------------------------------------------------
SqlConnection sqlcon = new SqlConnection(DateStr);
sqlcon.Open();
SqlCommand sqlCmd = new SqlCommand("backup database Kc to disk='" + textPaht.Text.Trim() + "' restore database Kc from disk='" + textPaht.Text.Trim() + "'", sqlcon);
sqlCmd.ExecuteNonQuery();
sqlCmd.Dispose();
sqlcon.Close();
sqlcon.Dispose();
MessageBox.Show("数据还原成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("为了必免数据丢失,在数据库还原后将关闭整个系统。");
Application.Exit();
}
else
{
MessageBox.Show("请选择备份文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
希望本文所述对大家C#程序设计有所帮助。
标签:C#,SQL,备份,还原
0
投稿
猜你喜欢
ComposeDesktop开发桌面端多功能APK工具
2022-02-11 16:57:04
Spring Security前后分离校验token的实现方法
2023-06-26 17:00:30
SpringMVC实现文件上传下载的全过程
2023-04-05 21:16:52
Java项目有中多个线程如何查找死锁
2023-01-06 06:17:35
基于spring 方法级缓存的多种实现
2022-12-14 18:31:38
Java string类型转换成map代码实例
2021-06-07 12:19:29
Java实现简易生产者消费者模型过程解析
2023-12-03 01:33:26
RollViewPager图片轮播效果开源框架使用方法详解
2021-12-06 07:22:59
Android自定义ScrollView实现阻尼回弹
2021-11-23 07:20:19
SpringBoot 如何使用RestTemplate发送Post请求
2022-03-03 09:35:47
Java:"失效"的private修饰符
2021-06-28 19:56:48
java使用POI操作excel文件
2023-05-17 00:37:12
C#调用百度翻译实现翻译HALCON的示例
2022-06-08 23:33:50
Android中控件GridView实现设置行列分割线的方法示例
2021-05-24 09:08:48
springboot创建线程池的两种方式小结
2022-07-12 09:27:19
SpringBoot事件机制相关知识点汇总
2021-05-29 06:53:45
SpringBoot详细讲解yaml配置文件的用法
2022-04-21 16:29:33
详解Android Activity之间跳转出现短暂黑屏的处理方法
2021-06-28 04:44:03
JVM内存模型/内存空间:运行时数据区
2023-01-14 13:12:23
Java中的静态绑定和动态绑定详细介绍
2023-01-18 19:54:06