• C#备份数据库文件


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

    sqlServer  存储过程

    1. USE [PSIDBase]
    2. GO
    3. /****** Object: StoredProcedure [dbo].[sp_BackupDB] Script Date: 2023/8/31 16:49:02 ******/
    4. SET ANSI_NULLS ON
    5. GO
    6. SET QUOTED_IDENTIFIER ON
    7. GO
    8. ALTER procedure [dbo].[sp_BackupDB]
    9. @savePath nvarchar(4000) -- 备份数据库保存位置(目录)
    10. ,@dbName nvarchar(4000) -- 需要进行备份的数据库
    11. ,@bakName nvarchar(4000) -- 备份文件的名称(不含扩展名)
    12. as begin
    13. declare @sql nvarchar(4000)
    14. /* 验证路径 */
    15. if(charindex('/',reverse(@savePath))!=1) begin
    16. set @savePath=@savePath+'/'
    17. end
    18. /* 拼SQL并执行 */
    19. set @sql='backup database '+@dbName+' to disk='''+@savePath+@bakName+'.bak'''
    20. exec sp_executesql @sql
    21. /* 返回执行结果(1=成功,0=失败) */
    22. if(@@error=0) begin
    23. return 1
    24. end
    25. return 0
    26. end

    App.config添加配置项

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

    DAL文件:

    1. /// <summary>
    2. /// 备份数据
    3. /// </summary>
    4. /// <param name="path"></param>
    5. /// <param name="dbName"></param>
    6. /// <param name="backupName"></param>
    7. /// <returns></returns>
    8. public int BackupData(string path, string dbName, string backupName)
    9. {
    10. SqlParameter[] paras =
    11. {
    12. new SqlParameter("@savePath",path),
    13. new SqlParameter("@dbName",dbName),
    14. new SqlParameter("@bakName",backupName),
    15. new SqlParameter("@return",SqlDbType.Int,4)
    16. };
    17. paras[3].Direction = ParameterDirection.ReturnValue;//返回值参数
    18. SqlHelper.ExecuteNonQuery("sp_BackupDB", 2, paras);
    19. return paras[3].Value.GetInt();
    20. }

    BLL:

    1. /// <summary>
    2. /// 备份数据
    3. /// </summary>
    4. /// <param name="path"></param>
    5. /// <returns></returns>
    6. public bool BackupData(string path)
    7. {
    8. bool bl = false;
    9. string dbName = ConfigurationManager.AppSettings["DbName"].ToString();
    10. string backupName = dbName + DateTime.Today.ToString("yyyyMMdd");
    11. int reVal = sysDAL.BackupData(path, dbName, backupName);
    12. bl = reVal == 1 ? true : false;
    13. return bl;
    14. }

    窗体调用:

    1. using PSI.BLL;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.ComponentModel;
    5. using System.Data;
    6. using System.Drawing;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. using System.Windows.Forms;
    11. using WinPSI.Common;
    12. namespace WinPSI.SM
    13. {
    14. public partial class FrmBackUpData : Form
    15. {
    16. public FrmBackUpData()
    17. {
    18. InitializeComponent();
    19. }
    20. private Verify verify = new Verify();
    21. private void btnCancel_Click(object sender, EventArgs e)
    22. {
    23. this.Close();
    24. }
    25. private void FrmBackUpData_Load(object sender, EventArgs e)
    26. {
    27. txtPath.Clear();
    28. }
    29. /// <summary>
    30. /// 选择备份位置
    31. /// </summary>
    32. /// <param name="sender"></param>
    33. /// <param name="e"></param>
    34. private void btnChoose_Click(object sender, EventArgs e)
    35. {
    36. FolderBrowserDialog ofd = new FolderBrowserDialog();
    37. if(ofd.ShowDialog()==DialogResult.OK)
    38. {
    39. txtPath.Text = ofd.SelectedPath;
    40. }
    41. }
    42. private void btnOK_Click(object sender, EventArgs e)
    43. {
    44. string path = txtPath.Text.Trim();
    45. if (string.IsNullOrEmpty(path))
    46. {
    47. MsgBoxHelper.MsgErrorShow("请选择备份文件存放的位置!");
    48. return;
    49. }
    50. if (MsgBoxHelper.MsgBoxConfirm("备份数据", "您确定要备份数据库吗?") == DialogResult.Yes)
    51. {
    52. SysBLL sysBLL = new SysBLL();
    53. bool bl = sysBLL.BackupData(path);
    54. if (bl)
    55. {
    56. MsgBoxHelper.MsgBoxShow("备份数据", "系统数据备份完毕!");
    57. }
    58. else
    59. {
    60. MsgBoxHelper.MsgErrorShow("数据备份失败!");
    61. return;
    62. }
    63. }
    64. }
    65. }
    66. }

  • 相关阅读:
    web站点的欢迎页面
    springboot项目的打包发布部署,jar和war的区别
    SAP FICO产品成本组件分析表开发说明书(包括开发源代码、测试样例及FS)
    playwright在vscode+jupyter中出现NotImplementedError问题
    STM32串口重定向/实现不定长数据接收
    UML的组成
    山东省高新技术企业认定四项得分标准解析
    Camtasia2024破解版电脑屏幕录制剪辑软件
    MES系统是如何采集Modbus设备数据的呢?
    知名药企集中签约阿里健康,数字化健康服务成进博会热议话题
  • 原文地址:https://blog.csdn.net/wgb0409/article/details/132606956