我不知道问什么老是错误,这是从别人那拷过来的。不知道是不是因为数据库中间有空格啊,那这怎么解决。一到 cmd那句那就跳到错误了。
 SqlConnection conn = new SqlConnection("Data Source=localhost;User ID=sa;PWD=;DataBase=sale MIS");
            //设置进度条
            toolStripProgressBar1.Maximum = 100;//设置最大长度值
            toolStripProgressBar1.Value = 0;//设置当前值
            toolStripProgressBar1.Step = 10;//设置没次增长多少
            for (int i = 0; i < 10; i++)//循环
            {
                System.Threading.Thread.Sleep(1000);//暂停1秒
                toolStripProgressBar1.Value += toolStripProgressBar1.Step;//让进度条增加一次
            }            try
            {
                conn.Open();
                string a = System.DateTime.Now.ToShortDateString();
                string b = "\\备份" + a + ".bak";
                string path = folderBrowserDialog1.SelectedPath + b;
                string sql = "backup DataBase sale MIS to disk='" + path + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("数据备份成功!", "成功提示",MessageBoxButtons.OK,MessageBoxIcon.Information);            }
            catch
            {
                MessageBox.Show("数据备份失败!", "错误提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            }
            finally
            {
                conn.Close();
            }

解决方案 »

  1.   

     SqlConnection conn = new SqlConnection("Data Source=localhost;User ID=sa;PWD=;DataBase=sale MIS");从别的地方拷过来的?你这个数据库是否正确,还有一种可能是你要备份的这个路径有多级,并且多级文件夹不存在,检查文件夹是否存在不存在先创建文件夹
      

  2.   

    问题不要混在一起,先拆分试试
    sale MIS是数据库名?那得用中括号括起。建立好目录后测试。
      string sql = @"backup DataBase [sale MIS] to disk='c:\temp\temp.bak'";
      SqlCommand cmd = new SqlCommand(sql, conn);最好先到数据库查询分析器里调试通过再用代码调用。