Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
bool bBackUpStatus = true;
            Cursor.Current = Cursors.WaitCursor;
            if (Directory.Exists(@"D:\SQLBackup"))
            {
                if (File.Exists(@"D:\SQLBackup\wcBackUp1.bak"))
                {
                    if (MessageBox.Show(@"Do you want to replace it?", "Back", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        File.Delete(@"D:\SQLBackup\wcBackUp1.bak");
                    }
                    else
                        bBackUpStatus = false;
                }
            }
            else
                Directory.CreateDirectory(@"D:\SQLBackup");
            if (bBackUpStatus)
            {
                //Connect to DB
                SqlConnection connect;
                string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bin\Debug\Garment.mdf;Integrated Security=True;Asynchronous Processing= True ;User Instance=True";
                connect = new SqlConnection(con);
                connect.Open();
                //-----------------

                //Execute SQL---------------
                SqlCommand command;
                command = new SqlCommand(@"backup database Garment to disk ='E:Garment.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);
            }
Posted

Check that the Garment.mdf database exists in location |DataDirectory|\bin\Debug\.
 
Share this answer
 
Comments
Er Aslam Khan 9-Aug-14 5:24am    
yeah i already checked Garment.mdf database file is exists
jaket-cp 11-Aug-14 11:56am    
Test the code to make sure it can find the Garment.mdf file.
To test you could put a MessageBox in place of //Connect to DB
and use File.Exists to make sure the file can be found.
Something like:
if (File.Exists(@"|DataDirectory|\bin\Debug\Garment.mdf"))
{
//MessageBox here with The File Does Exist message
}
Maybe the database is not being attached as Garment even though the file name is Garment.mdf. Modify your connection string to :'

C#
string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bin\Debug\Garment.mdf;Integrated Security=True;Asynchronous Processing= True ;User Instance=True";
 
Share this answer
 
Maybe the database is not being attached as Garment even though the file name is Garment.mdf. Modify your connection string to :'

C#
string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bin\Debug\Garment.mdf;Database=Garment;Integrated Security=True;Asynchronous Processing= True ;User Instance=True";


Take note of the Database keyword in the connection string.
 
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