日期:2014-05-18  浏览次数:20840 次

我写的数据备份,为啥说我没权限,求解释
try
  {
  string recharge = System.Configuration.ConfigurationManager.AppSettings["recharge"];
  string strg = Application.StartupPath.ToString();//应用程序路径(dll文件路径)
  strg = strg.Substring(0, strg.LastIndexOf("\\"));//上一级
  strg = strg.Substring(0, strg.LastIndexOf("\\"));//上一级
  strg += recharge;//DataBF文件夹
  string sqltxt = @"BACKUP DATABASE master TO Disk='" + strg + "\\" + ".dat" + "'";//备份数据库的sql语句
  SqlConnection conn = db.getConnection();//创建数据库连接
  // conn.Open();//打开数据库连接
  SqlCommand cmd = new SqlCommand(sqltxt, conn);//创建SqlCommand 对象用于执行sql语句
  cmd.ExecuteNonQuery();//执行sql语句
  conn.Close();//sql语句执行完毕,关闭数据库连接
  }
  catch (Exception ex)
  {
  MessageBox.Show("cuowu "+ex.Message);
  db.closeConnection();
  }
我报的错误时cuowu 在数据库‘master’中拒绝了BACKUP DATABASE 权限

------解决方案--------------------
看看连接字符串中的用户是否有BACKUP DATABASE 权限,
用sa用户看看是否报错
------解决方案--------------------
我也碰到过这种情况,解决方法如下(主要是加上use master):
 
C# code

 protected void BtnBack_Click(object sender, EventArgs e)
        {
            
            string saveAway = "D:\\S2.bak";
            string cmdText = @"use master;backup database TrainSearch to disk='" + saveAway + "'";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = cmdText;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();           
            Common.ShowMessage.Show(Page, "yes", "备份成功!");
            con.Close();
            BackUpTime bt = new BackUpTime();
            string time = DateTime.Now.ToString();
            if (bt.EditBackTime(1, time))
            {

            }
            else
            { 
            }
            
        }