当前位置: 首页 > news >正文

C#备份数据库文件

c#备份数据库文件完整代码

sqlServer  存储过程:

USE [PSIDBase]
GO
/****** Object:  StoredProcedure [dbo].[sp_BackupDB]    Script Date: 2023/8/31 16:49:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GOALTER procedure [dbo].[sp_BackupDB]@savePath nvarchar(4000) -- 备份数据库保存位置(目录)   ,@dbName nvarchar(4000) -- 需要进行备份的数据库,@bakName nvarchar(4000) -- 备份文件的名称(不含扩展名)as begindeclare @sql nvarchar(4000)/* 验证路径 */if(charindex('/',reverse(@savePath))!=1) beginset @savePath=@savePath+'/'end/* 拼SQL并执行 */set @sql='backup database '+@dbName+' to disk='''+@savePath+@bakName+'.bak'''exec sp_executesql @sql/* 返回执行结果(1=成功,0=失败) */if(@@error=0) beginreturn 1endreturn 0end

App.config添加配置项

<?xml version="1.0" encoding="utf-8"?>
<configuration><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup><connectionStrings><add name="connStr" connectionString="server=PC-20191008BGNB\SQLEXPRESS;database=***;uid=**;pwd=***;" providerName="System.Data.SqlClient"/></connectionStrings><appSettings><add key="DbName" value="PSIDBase"/></appSettings>
</configuration>

DAL文件:

 /// <summary>/// 备份数据/// </summary>/// <param name="path"></param>/// <param name="dbName"></param>/// <param name="backupName"></param>/// <returns></returns>public int BackupData(string path, string dbName, string backupName){SqlParameter[] paras ={new SqlParameter("@savePath",path),new SqlParameter("@dbName",dbName),new SqlParameter("@bakName",backupName),new SqlParameter("@return",SqlDbType.Int,4)};paras[3].Direction = ParameterDirection.ReturnValue;//返回值参数SqlHelper.ExecuteNonQuery("sp_BackupDB", 2, paras);return paras[3].Value.GetInt();}

BLL:

/// <summary>
/// 备份数据
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public bool BackupData(string path)
{bool bl = false;string dbName = ConfigurationManager.AppSettings["DbName"].ToString();string backupName = dbName + DateTime.Today.ToString("yyyyMMdd");int reVal = sysDAL.BackupData(path, dbName, backupName);bl = reVal == 1 ? true : false;return bl;
}

窗体调用:

using PSI.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinPSI.Common;namespace WinPSI.SM
{public partial class FrmBackUpData : Form{public FrmBackUpData(){InitializeComponent();}private Verify verify = new Verify();private void btnCancel_Click(object sender, EventArgs e){this.Close();}private void FrmBackUpData_Load(object sender, EventArgs e){txtPath.Clear();}/// <summary>/// 选择备份位置/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnChoose_Click(object sender, EventArgs e){FolderBrowserDialog ofd = new FolderBrowserDialog();if(ofd.ShowDialog()==DialogResult.OK){txtPath.Text = ofd.SelectedPath;}}private void btnOK_Click(object sender, EventArgs e){string path = txtPath.Text.Trim();if (string.IsNullOrEmpty(path)){MsgBoxHelper.MsgErrorShow("请选择备份文件存放的位置!");return;}if (MsgBoxHelper.MsgBoxConfirm("备份数据", "您确定要备份数据库吗?") == DialogResult.Yes){SysBLL sysBLL = new SysBLL();bool bl = sysBLL.BackupData(path);if (bl){MsgBoxHelper.MsgBoxShow("备份数据", "系统数据备份完毕!");}else{MsgBoxHelper.MsgErrorShow("数据备份失败!");return;}}}}
}

http://www.lryc.cn/news/155247.html

相关文章:

  • 行军遇到各种复杂地形怎么处理?
  • Python Number(数字).............................................
  • 设置 Hue Server 与 Hue Web 界面之间的会话超时时间
  • openGauss学习笔记-57 openGauss 高级特性-并行查询
  • 软考(1)-面向对象的概念
  • 深度学习推荐系统(四)WideDeep模型及其在Criteo数据集上的应用
  • 第十二章 YOLO的部署实战篇(中篇)
  • 面试题查漏补缺 i++和 ++ i哪个效率更高
  • Docker的数据管理(持久化存储)
  • 定时脚本自动自动将文件push到git
  • 025: vue父子组件中传递方法控制:$emit,$refs,$parent,$children
  • 使用js搭建简易的WebRTC实现视频直播
  • LeetCode 2707. Extra Characters in a String【动态规划,记忆化搜索,Trie】1735
  • 设计模式行为型-模板模式
  • 9.3.tensorRT高级(4)封装系列-自动驾驶案例项目self-driving-车道线检测
  • django.core.exceptions.AppRegistryNotReady: Apps aren‘t loaded yet.
  • 【C#】C#调用进程打开一个exe程序
  • 宝塔面板定时监控和重启MySQL数据库(计划任务)
  • Beats:安装及配置 Metricbeat (二)- 8.x
  • Redis之哨兵模式解读
  • 题目:2644.找出可整除性得分最大的整数
  • 报错:axios 发送的接口请求 404
  • 三年前端还不会配置Nginx?刷完这篇就够了
  • blender 场景灯光基础设置
  • 如何查看 SQLyog 中数据库连接信息中的密码
  • 【SpringSecurity】八、集成图片验证码
  • 【本地代码问题】启动程序,报错:java.lang.IllegalArgumentException: No selectors
  • 手写RPC框架--4.服务注册
  • oracle 解锁表
  • 使用Dbeaver连接GaussDB