从C#中的Sql Server备份和还原,net [英] Backup And Restore From Sql Server in C#,net

查看:165
本文介绍了从C#中的Sql Server备份和还原,net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我想从SQL SERVER数据库备份,但我有错误:



Hi all

I wanna Backup from the SQL SERVER Database but I have the error :

<pre lang="c#">
private void BackUp(string strfilename)
{
 using (SqlConnection con = new SqlConnection(fis.connectstr))
           {
              
               string command = "BACKUP DATABASE Ast-db TO DISK='" + strfilename + "'";

               con.Open();
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = command;
                   cmd.Connection = con;
                   cmd.CommandType = CommandType.Text;
                   int r = cmd.ExecuteNonQuery();
                   if (r > 0)
                   {
                       MessageBox.Show("BackUp ok !!!!!!!!! ");
                   }
                   this.Cursor = Cursors.Default;
                   con.Close();
               }
}





但我有错误:' - '附近的语法不正确。



But I have Error : Incorrect syntax near '-'.

推荐答案

比你想象的要复杂得多!请参阅此处:使用C#备份SQL数据库 [< a href =http://www.codeproject.com/Tips/461494/Backing-up-an-SQL-Database-in-Csharptarget =_ blanktitle =New Window> ^ ]
Slightly more complex than you think! See here: Backing up an SQL Database in C#[^]


尝试在数据库名称周围加上方括号:

Try to put square brackets around the database name:
BACKUP DATABASE [Ast-db] TO DISK


 string path="D:/filename.bak";
SqlCommand cmd=new SqlCommand("backup database dbnameto disk='"+path+"'",con);
 int a=cmd.ExecuteNonQuery();


这篇关于从C#中的Sql Server备份和还原,net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆