本文介绍了我如何使用C#进行MSSQL 2008备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我正在尝试备份sql数据库...使用C#..我尝试了不同的代码,但无法做到..
谁能帮忙...

hi
i am trying to take a backup of sql database ...using c# ..i have tried different code but cant do it ..
can any one help ...

推荐答案

using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
            FileInfo file = new FileInfo("C:\\myscript.sql");
            string script = file.OpenText().ReadToEnd();
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            Server server = new Server(new ServerConnection(conn));
            server.ConnectionContext.ExecuteNonQuery(script);
        }
    }
}
SQL Query : -

BACKUP DATABASE [MyDatabase] TO  DISK = 'C:\....\MyDatabase.bak' WITH  INIT ,  NOUNLOAD ,  NAME = N'MyDatabase backup',  NOSKIP ,  STATS = 10,  NOFORMAT


这篇关于我如何使用C#进行MSSQL 2008备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 09:23