• 大小: 519KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: C#
  • 标签: SQL  

资源简介

SQL Server数据库备份与恢复(C#代码示例)为你提示了简洁、易懂的代码示例

资源截图

代码片段和文件信息

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;

namespace Test_BackUp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //备份
        private void button1_Click(object sender EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = “(*.mdf)|*.mdf“;
            saveFileDialog.FilterIndex = 0;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                SQLBACK(“KK\\SQLEXPRESS“ “nuanqi“ “123“ “BackUpTest“ filePath);
                //SQLBACK(“KK\\SQLEXPRESS“ “nuanqi“ “123“ “BackUpTest“ @“E:\BackUpTest.mdf“);
            }
        }

        /// < summary>
        /// SQL数据库备份
        /// < /summary>
        /// < param name=“ServerIP“>SQL服务器IP或(Localhost)< /param>
        /// < param name=“LoginUserName“>数据库登录名< /param>
        /// < param name=“LoginPass“>数据库登录密码< /param>
        /// < param name=“DBName“>数据库名< /param>
        /// < param name=“BackPath“>备份到的路径< /param>
        public static void SQLBACK(string ServerIP string LoginUserName string LoginPass string DBName string BackPath)
        {
            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(ServerIP LoginUserName LoginPass);
                oBackup.Database = DBName;
                //oBackup.Files = BackPath;
                oBackup.Files = @““ + BackPath + ““;
                oBackup.BackupSetName = DBName;
                oBackup.BackupSetDescription = “数据库备份“;
                oBackup.Initialize = true;
                oBackup.SQLBackup(oSQLServer);
                System.Windows.Forms.MessageBox.Show(“备份成功!“);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                oSQLServer.DisConnect();
            }
        }

        //恢复
        private void button2_Click(object sender EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = “(*.mdf)|*.mdf“;
            openFileDialog.FilterIndex = 0;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                SQLDbRestore(“KK\\SQLEXPRESS“ “nuanqi“ “123“ “BackUpTest“ @“E:\BackUpTest.mdf“);
        }

        /// < summary>
        /// SQL恢复数据库
        /// < /summary>
        /// < param name=“ServerIP“>SQL服务器IP或(Localhost)< /param>
        /// < param name=“LoginUserName“>数据库登录名< /par

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     950272  2010-09-29 16:04  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\bin\Debug\Interop.SQLDMO.dll

     文件       9728  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\bin\Debug\Test_BackUp.exe

     文件      26112  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\bin\Debug\Test_BackUp.pdb

     文件      14328  2010-09-29 17:21  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\bin\Debug\Test_BackUp.vshost.exe

     文件     950272  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\bin\Release\Interop.SQLDMO.dll

     文件       9728  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\bin\Release\Test_BackUp.exe

     文件      22016  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\bin\Release\Test_BackUp.pdb

     文件       4466  2010-09-29 17:21  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\Form1.cs

     文件       2647  2010-09-29 16:12  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\Form1.Designer.cs

     文件       5814  2010-09-29 16:12  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\Form1.resx

     文件          0  2010-09-29 17:21  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\build.force

     文件     950272  2010-09-29 16:04  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\Interop.SQLDMO.dll

     文件       1748  2010-09-29 17:21  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\Test_BackUp.csproj.FileListAbsolute.txt

     文件        847  2010-09-29 16:12  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\Test_BackUp.csproj.GenerateResource.Cache

     文件        579  2010-09-29 16:04  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\Test_BackUp.csproj.ResolveComReference.cache

     文件       9728  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\Test_BackUp.exe

     文件        180  2010-09-29 16:12  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\Test_BackUp.Form1.resources

     文件      26112  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\Test_BackUp.pdb

     文件        180  2010-09-29 16:05  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Debug\Test_BackUp.Properties.Resources.resources

     文件     950272  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Release\Interop.SQLDMO.dll

     文件       1446  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Release\Test_BackUp.csproj.FileListAbsolute.txt

     文件        847  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Release\Test_BackUp.csproj.GenerateResource.Cache

     文件        579  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Release\Test_BackUp.csproj.ResolveComReference.cache

     文件       9728  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Release\Test_BackUp.exe

     文件        180  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Release\Test_BackUp.Form1.resources

     文件      22016  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Release\Test_BackUp.pdb

     文件        180  2010-09-29 16:46  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\obj\Release\Test_BackUp.Properties.Resources.resources

     文件        492  2010-09-29 15:57  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\Program.cs

     文件       1354  2010-09-29 15:57  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\Properties\AssemblyInfo.cs

     文件       2872  2010-09-29 15:57  SQL Server数据库备份与恢复\Test_BackUp\Test_BackUp\Properties\Resources.Designer.cs

............此处省略22个文件信息

评论

共有 条评论