Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 '-'.
Posted
Updated 12-Apr-14 3:00am
v2

Slightly more complex than you think! See here: Backing up an SQL Database in C#[^]
 
Share this answer
 
Comments
Maciej Los 13-Dec-14 5:30am    
Self-reference - i like it ;)
OriginalGriff 13-Dec-14 5:42am    
That was last April!
Are you re-running "The Best of OriginalGriff" or something? :laugh:
Maciej Los 13-Dec-14 5:47am    
Someone made changes to this thread, so it brought back to the top of "active questions".

Cheers,
Maciej
Try to put square brackets around the database name:
SQL
BACKUP DATABASE [Ast-db] TO DISK
 
Share this answer
 
C#
 string path="D:/filename.bak";
SqlCommand cmd=new SqlCommand("backup database dbnameto disk='"+path+"'",con);
 int a=cmd.ExecuteNonQuery();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900