数据库备份错误 [英] database backup error
问题描述
我使用vs 2010创建了一个.mdf数据库文件。我可以检索并将数据插入数据库,但是当我想要备份时,错误已被处理。该数据库未在Management Studio中附加。
我的代码:
I was created a .mdf database file with vs 2010. I can retreive and insert data into database but when i want to backup error was handled. that database not attach in Management Studio.
my Code :
SqlConnection connect;
connect = new SqlConnection(DAL.AccessLayerClass._connectionStr);
connect.Open();
SqlCommand command;
command = new SqlCommand(@"backup database AGMDB to disk ='d:\svBackUp1.bak' with init,stats=10",connect);
command.ExecuteNonQuery();
connect.Close();
MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
我的连接字符串是:
my connection string is:
string _connectionStr = "Data Source=.\\SQLEXPRESS; AttachDbFilename=" + System.Windows.Forms.Application.StartupPath + "\\Database\\AGMDB.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True;";
和错误发生的原因是:
找不到条目在数据库'AGMDB'的sysdatabases中。找不到具有该名称的条目。确保输入的名称正确。
BACKUP DATABASE正在异常终止。
如何解决这个问题????
and the error occured is :
Could not locate entry in sysdatabases for database 'AGMDB'. No entry found with that name. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.
how can i solve this problem ????
推荐答案
连接字符串之类的数据源非常简单= .\\SQLEXPRESS; AttachDbFilename =+ System.Windows.Forms.Application.StartupPath +\\Database \\ AGMDB.mdf;综合安全=真;连接超时= 30;用户实例=真;
您的数据库不是AGMDB.mdf的名称,但它的名称将是路径+AGMDB.mdf因此解决方案非常简单你的代码
command = new SqlCommand(@backup database AGMDB to disk ='d:\ svBackUp1.bak'with init,stats = 10,connect);
替换
AGMDB与System.Windows.Forms.Application.StartupPath +\\ AGMDB.mdf...... Gud luck
Its very simple when you connection string like Data Source=.\\SQLEXPRESS; AttachDbFilename=" + System.Windows.Forms.Application.StartupPath + "\\Database\\AGMDB.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True;
your databse is not attch as name of AGMDB.mdf but its name will be path + "AGMDB.mdf" So solution is very simple in your code
command = new SqlCommand(@"backup database AGMDB to disk ='d:\svBackUp1.bak' with init,stats=10",connect);
replace
AGMDB with System.Windows.Forms.Application.StartupPath + "\\AGMDB.mdf"...Gud luck
String DbPath = System.Windows.Forms.Application.StartupPath+"\\AGMDB.mdf";
command = new SqlCommand(@"backup database "+ DbPath +" to disk ='d:\svBackUp1.bak' with init,stats=10",connect);
或使用可以使用
or use can use
command = new SqlCommand("backup DataBase "+DbPath +" to
disk = 'd:\svBackUp1.bak' with noformat, noinit, name = 'AGMDB',
skip,norewind,nounload,stats=10")
SqlConnection cn = new SqlConnection(@Data Source = .\SQLEXPRESS; AttachDbFilename = D:\ jaydip \ WindowsFormsApplication1 \ WindowsFormsApplication1 \Database1.mdf; Integrated Security = True; User Instance = True);
cn.Open();
MessageBox.Show(System.Windows.Forms.Application.StartupPath);
SqlCommand cmd = new SqlCommand(@backup database [+ System.Windows.Forms.Application .StartupPath +\\Database1.mdf到disk ='d:\\svBackUp1.bak'与init,stats = 10,cn);
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show(数据库备份成功。) ;
//此解决方案100%有效......
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\jaydip\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True;User Instance=True");
cn.Open();
MessageBox.Show(System.Windows.Forms.Application.StartupPath);
SqlCommand cmd = new SqlCommand(@"backup database [" + System.Windows.Forms.Application.StartupPath + "\\Database1.mdf] to disk ='d:\\svBackUp1.bak' with init,stats=10", cn);
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Database Backup Successfull.");
//this solution 100% works...
这篇关于数据库备份错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!