我不知道问什么老是错误,这是从别人那拷过来的。不知道是不是因为数据库中间有空格啊,那这怎么解决。一到 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();
}
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();
}
sale MIS是数据库名?那得用中括号括起。建立好目录后测试。
string sql = @"backup DataBase [sale MIS] to disk='c:\temp\temp.bak'";
SqlCommand cmd = new SqlCommand(sql, conn);最好先到数据库查询分析器里调试通过再用代码调用。