Back up database Error
-
Hi all,I have a problem with back up command! I use this code but it doesnt work:
string constr = @"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\App_Data\Database1.mdf;Integrated Security=True;User Instance=True;";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection ();
con.ConnectionString = constr;
con.Open();
System.Data.SqlClient.SqlCommand comm=newSystem.Data.SqlClient.SqlCommand();
comm.CommandText = @"backup database Database1 to disk ='c:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\DBbackup.bak' with init,stats=10";
comm.Connection = con;
comm.ExecuteNonQuery();
con.Close();and when I run it, I get this error: Database 'Database1' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally. but I use the database1 and I connect it successfully! what is the problem?
-
Hi all,I have a problem with back up command! I use this code but it doesnt work:
string constr = @"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\App_Data\Database1.mdf;Integrated Security=True;User Instance=True;";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection ();
con.ConnectionString = constr;
con.Open();
System.Data.SqlClient.SqlCommand comm=newSystem.Data.SqlClient.SqlCommand();
comm.CommandText = @"backup database Database1 to disk ='c:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\DBbackup.bak' with init,stats=10";
comm.Connection = con;
comm.ExecuteNonQuery();
con.Close();and when I run it, I get this error: Database 'Database1' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally. but I use the database1 and I connect it successfully! what is the problem?
Uhm you are naming your backup file with the .aspx extension, pretty sure that is going to bite you later, normal extension is .bak
Never underestimate the power of human stupidity RAH
-
Uhm you are naming your backup file with the .aspx extension, pretty sure that is going to bite you later, normal extension is .bak
Never underestimate the power of human stupidity RAH
ohhh,it was a typing mistake , I still have this error ! and another note is that the DBBackup.bak file doesn't exsit for the first time and I wanted to use the directory.createdirectory,file.exist or directory.exist method but I dont know what name space(s) should I use! any suggession for my tow questions???? plz help me!
-
Hi all,I have a problem with back up command! I use this code but it doesnt work:
string constr = @"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\App_Data\Database1.mdf;Integrated Security=True;User Instance=True;";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection ();
con.ConnectionString = constr;
con.Open();
System.Data.SqlClient.SqlCommand comm=newSystem.Data.SqlClient.SqlCommand();
comm.CommandText = @"backup database Database1 to disk ='c:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\DBbackup.bak' with init,stats=10";
comm.Connection = con;
comm.ExecuteNonQuery();
con.Close();and when I run it, I get this error: Database 'Database1' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally. but I use the database1 and I connect it successfully! what is the problem?
sara-setare wrote:
but I use the database1 and I connect it successfully!
Are the connectionstrings the same? Sql Server databases are usually attached to that server, with the server assigning a database-alias to that attached file. You are attaching a file "on the fly", without specifying an alias. I doubt that Sql Server will use the filename as the database-alias. Add a "Database=database1;" part to your connectionstring, similar to the example given below;
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;I'd recommend attaching it permanently to the server to avoid collisions in alias-names when attaching.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
sara-setare wrote:
but I use the database1 and I connect it successfully!
Are the connectionstrings the same? Sql Server databases are usually attached to that server, with the server assigning a database-alias to that attached file. You are attaching a file "on the fly", without specifying an alias. I doubt that Sql Server will use the filename as the database-alias. Add a "Database=database1;" part to your connectionstring, similar to the example given below;
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;I'd recommend attaching it permanently to the server to avoid collisions in alias-names when attaching.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
Thank you so much,it works! but maybe a primary question!!: how can I attach my database permanently to the server?
-
Thank you so much,it works! but maybe a primary question!!: how can I attach my database permanently to the server?