Hajitsu
Hajitsu

Reputation: 784

How to backup from .mdf database that created by VS

I was created a .mdf database file with vs 2010. I can retrive and insert data into database bbut 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:

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

and that error was occurs:

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 error? Thanks

Upvotes: 3

Views: 3023

Answers (1)

Amen Ayach
Amen Ayach

Reputation: 4348

You need [] around DBFileName, try this:

SqlConnection connect;
connect = new SqlConnection(DAL.AccessLayerClass._connectionStr);
connect.Open();
SqlCommand command;
command = new SqlCommand(@"backup database [" + System.Windows.Forms.Application.StartupPath + "\\Database\\AGMDB.mdf] 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);

Reference : How to Backup and Restore SQL Express 2005 (AttachDbFilename mode)

Upvotes: 4

Related Questions