Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I write code for back up but I debug code don't find database and have error
please help me



C#
// add ref
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.Wmi;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlServer.Management.Smo.Broker;
using Microsoft.SqlServer.Management.Smo.Mail;
using Microsoft.SqlServer.Management.Smo.RegisteredServers;
using Microsoft.SqlServer.Management.Trace;
// Connection
SqlConnection sq = new SqlConnection(ClassAccessDatac.con);

// behind code btn
     private void btnBackup_Click(object sender, EventArgs e)
        {
            string MethodeName = "btnBackup_Click";
            try
           {
                 string strFileName = string.Empty;
                
                 saveFileDialog1.DefaultExt = "mdf";
                 saveFileDialog1.FileName = "BackupFile" + un1.OnUniq();
                 saveFileDialog1.Filter = @"SQL Backup files  *.*|All files(*.*) ";
                 saveFileDialog1.FilterIndex = 1;
                 saveFileDialog1.OverwritePrompt = true;
                 saveFileDialog1.Title = "Backup SQL File";
                 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                 {
                     strFileName = saveFileDialog1.FileName;
                 BackupDatabase("BozorgMehr", "sa", "09124863440", "SERVER001", strFileName);
                   Backup(strFileName);
                 }
             }  
            catch (Exception ex)
             {

              
               Keys.RegError(MethodeName, ex.ToString(), sq);
            }

        
        }
        public Microsoft.SqlServer.Management.Smo.Database db = new Database();

        public void BackupDatabase(String databaseName, String userName,
       String password, String serverName, String destinationPath)
        {
            try{

            Backup sqlBackup = new Backup();

            sqlBackup.Action = BackupActionType.Database;
            sqlBackup.BackupSetDescription = "ArchiveDataBase:" +
                             DateTime.Now.ToShortDateString();
            sqlBackup.BackupSetName = "Archive";

            sqlBackup.Database = databaseName;

            BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
            ServerConnection connection = new ServerConnection(serverName, userName, password);
            Server sqlServer = new Server(connection);

            Database db = sqlServer.Databases[databaseName];

            sqlBackup.Initialize = true;
            sqlBackup.Checksum = true;
            sqlBackup.ContinueAfterError = true;

            sqlBackup.Devices.Add(deviceItem);
            sqlBackup.Incremental = false;

            sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
            sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

            sqlBackup.FormatMedia = false;

            sqlBackup.SqlBackup(sqlServer);

            }
                 catch (Exception ex)
                {

            
                   Keys.RegError(MethodeName, ex.ToString(), sq);
                }
        }
Posted
Comments
Thanks7872 12-Dec-14 6:10am    
And do you think that we can read your mind?
vajihe.mirzaei 14-Dec-14 5:08am    
I need back up SQL server2012 programmatically in network (Ethernet).
I had written above code but when program invoke database, can't find .
my path is true, I don't know.
help...please...
vajihe.mirzaei 12-Dec-14 6:46am    
Sorry I was too tired and unfocus.
I'm a network. The program runs on the client. They should back up their database. Program should be back up to full.error instant Object in C # code.
my database have 40 table 50 procedure.
network : Ethernet , 10 client,connection Radio
.....sorry

1 solution

You did not provide any details about your issue... Nevertheless, there is stil the chance to help you.

Here is a complete sample of backuping database by using SMO: SQL Server 2008 - Backup and Restore Databases using SMO[^]
Backup and Restore SQL Server databases programmatically with SMO[^]
 
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