备份数据库的代码&错误进入执行非查询 [英] Code for backup database, & error comes in execute non query

查看:71
本文介绍了备份数据库的代码&错误进入执行非查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为备份编写代码,这是我在项目中使用的代码,它们是executenonquery中的错误,请检查代码并告诉我如何解决此问题

并且出现此错误

I''m making the code for Backup, this is my code which iam using in my project, their is an error in executenonquery , check the code and tell me how can i slove this problem

and this error comes

Could not locate entry in sysdatabases for database 'abcd'. No entry found with that name. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.



   string dbname = "abcd";
    SqlConnection sqlcon = new SqlConnection();
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

protected void btn_Click(object sender, EventArgs e)
    {
        //Mentioned Connection string make sure that user id and password sufficient previlages
        sqlcon.ConnectionString= @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\abcd.mdf ;Integrated Security=True;User Instance=True";



        //Enter destination directory where backup file stored
        string destdir = "D:\\Backup";

        //Check that directory already there otherwise create 
        if (!System.IO.Directory.Exists(destdir))
        {
            System.IO.Directory.CreateDirectory("D:\\Backup");
        }
       // try
        {
            //Open connection
            sqlcon.Open();
            //query to take backup database
           

           sqlcmd = new SqlCommand("backup database " + dbname + " to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
            sqlcmd.ExecuteNonQuery();
            //Close connection
            sqlcon.Close();
            Response.Write("Backup database successfully");
        }
      // catch (Exception ex)
        {
            Response.Write("Error During backup database!");
        }
    }

推荐答案

请参阅此文章:http://weblogs.asp.net/morteza/archive/2009/10/08/how-to-backup- and-restore-sql-express-2005-attachdbfilename-mode.aspx [
See this article: http://weblogs.asp.net/morteza/archive/2009/10/08/how-to-backup-and-restore-sql-express-2005-attachdbfilename-mode.aspx[^]


plz请使用cmd. executequerry代替cmd.executenon Querry
plz use cmd.executequerry in place of cmd.executenon querry


这篇关于备份数据库的代码&错误进入执行非查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆