当前位置:编程学习 > C#/ASP.NET >>

c#数据库备份失败


这是选择路径备份出错的弹框

这是默认路径备份的出错弹框,请问这问题怎么解决?
以下是click函数的代码:
string connectionString = "server=.\\SQLEXPRESS;database=sx;User id =sa;pwd=SQL2005;";
            if (textBox2.Text == "" & radioButton2.Checked == true)
            {
                MessageBox.Show("请选择备份数据库文件的路径。");
                return;
            }
            try
            { 
                string strg = Application.StartupPath.ToString();
                strg = strg.Substring(0, strg.LastIndexOf("\\"));
                strg = strg.Substring(0, strg.LastIndexOf("\\"));        
                 strg = @"\Data";
                 string sqltxt = @"BACKUP DATABASE sx TO Disk='" + strg + "\\" + textBox2.Text + ".bak" + "'";
                SqlConnection con = new SqlConnection(connectionString);//
                con.Open();
                SqlCommand cmd = new SqlCommand(sqltxt, con);
                cmd.ExecuteNonQuery();
                 con.Close(); 
                //MessageBox.Show("数据备份成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
               --------------------编程问答-------------------- 弄了一天了,都找不到问题所在 --------------------编程问答-------------------- textBox2.Text 应该只填文件名,不要路径和后缀名
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/ --------------------编程问答--------------------
引用 2 楼 feiyun0112 的回复:
textBox2.Text 应该只填文件名,不要路径和后缀名
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/

string sqltxt = @"BACKUP DATABASE sx TO Disk='" + strg + "\\" + textBox2.Text + ".bak" + "'";请问版主大人这句话该怎么改? --------------------编程问答--------------------

    private string dbName
    {
        get
        {
            string sql = "Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = @@spid)";
            return dosoft.DAL.DbHelperSQL.GetSingle(sql).ToString();
        }
    }




 string dbN = dbName;
string path = Server.MapPath("./DataBackUp/" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".bak");
string cmdstr = "backup database " + dbN + " to disk = '" + path + "' ";


一直都是这样写..你先测试下 看看可以么..

如果不可以 可能是你路径的问题.因为你那路径里面有空格啥的 导致的失败. --------------------编程问答-------------------- 貌似是你如果用命令备份,如果路径不存在,就会出现异常,数据库貌似不会直接创建文件夹
你看下你备份的路径是不是存在,就是文件夹! --------------------编程问答--------------------
存在dosoft和server没有定义,这个是不是少了引用? --------------------编程问答--------------------
引用 5 楼 kangkang08 的回复:
貌似是你如果用命令备份,如果路径不存在,就会出现异常,数据库貌似不会直接创建文件夹
你看下你备份的路径是不是存在,就是文件夹!
路径是直接用folderBrowserDialog1控件来确定的,选择路径备份的应该不会有问题的,默认备份的是放在"E://" --------------------编程问答-------------------- 那把sqltxt那个变量打出来看看 --------------------编程问答-------------------- 然后复制到查询分析器里面去执行试试
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,