Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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 :

C#
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:

C#
string _connectionStr = "Data Source=.\\SQLEXPRESS; AttachDbFilename=" + System.Windows.Forms.Application.StartupPath + "\\Database\\AGMDB.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True;";





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 ????
Posted
Updated 30-Oct-12 1:55am
v2

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
 
Share this answer
 
Comments
Harpreet_125 30-Oct-12 5:25am    
can u plz tell me how to write full query for that ???
C#
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
C#
command = new SqlCommand("backup DataBase "+DbPath +" to
disk = 'd:\svBackUp1.bak' with noformat, noinit, name = 'AGMDB',
skip,norewind,nounload,stats=10")
 
Share this answer
 
v2
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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900