Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to take backup of sql server 2005 database in c#.net without SMO
Posted

 
Share this answer
 
Comments
veenusethi 2-Aug-13 4:30am    
I want Without SMO I don't Know What is SMO and i could not find SMO dll in my pc Sol _Please Help me Database backup of sql server 2005 on button click in c#
I guess without SMO you can't do it.
And for SMO, you need to to have the SQL Server Management Studio.

SMO -> SQL Server Management Objects.
For one example, refer - Create a Visual C# SMO Project in Visual Studio .NET.
Here is a procedure is use for back up in C#.Hope it helps
C#
public  void PerformBackUp(string BackUpLocation, string BackUpFileName, string DatabaseName, string ServerName)
       {
           DatabaseName = "[" + DatabaseName + "]";
           string fileUNQ = DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() +"_"+ DateTime.Now.Hour.ToString()+ DateTime.Now .Minute .ToString () + "_" + DateTime .Now .Second .ToString () ;
           BackUpFileName = BackUpFileName + fileUNQ + ".bak";
           string SQLBackUp = @"BACKUP DATABASE " + DatabaseName + " TO DISK = N'" + BackUpLocation + @"\" + BackUpFileName + @"'";
           // Declare sqlconn,sqlcmd, show progresss execute non query
           string svr = "Server=" + ServerName + ";Database=master;Integrated Security=True";
           SqlConnection cnBk = new SqlConnection(svr);
           SqlCommand cmdBkUp = new SqlCommand(SQLBackUp, cnBk);

           try
           {
               cnBk.Open();
               cmdBkUp.ExecuteNonQuery();
               string msg = "Database " + DatabaseName + " successfully backed up to " + BackUpLocation + @"\" + BackUpFileName
                   + "\n Back Up Date : " + DateTime.Now.ToString();
               MessageBox .Show(msg,Application .ProductName , MessageBoxButtons .OK, MessageBoxIcon .Information );
               lblProgress.Visible = false;
               progressBar1.Visible = false;
           }
           catch (Exception ex)
           {
               MessageBox.Show("Database " + DatabaseName + "back up error.\n" + ex.Message, "Back Up Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
           }

           finally
           {
               if (cnBk.State == ConnectionState.Open)
               {

                   cnBk .Close();
               }
           }
       }
 
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