c#数据库备份中关键字“to”附近的语法不正确

时间:2015-12-25 22:57:39

标签: c#-3.0

string sql = "BACKUP DATABASE" + comboBox1.SelectedText.ToString() + 
             " to disk = '" + textBox1.Text.ToString() + "'\'" + 
             comboBox1.SelectedText+"'.bak";

1 个答案:

答案 0 :(得分:0)

 try 
   {
    if (!string.IsNullOrWhiteSpace(textBox1.Text))
        {
          SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Anjum-Dar\Documents\Visual Studio 2012\managerecord\managerecord\recordemanage.mdf;Integrated Security=True");
          con.Open();
          string sql = "BACKUP DATABASE" + comboBox1.SelectedText.ToString() + " to disk = '" + textBox1.Text.ToString() + "'\'" + comboBox1.SelectedText + "'.bak";
          SqlCommand cmd = new SqlCommand(sql, con);
          cmd.ExecuteNonQuery();

          con.Close();
          MessageBox.Show("Successfully ");
       }
     } 
      catch (Exception ex) { 
       MessageBox.Show(ex.Message);
     }