Click here to Skip to main content
15,909,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all

I want to backup of my database server with c sharp. I saw a method in google. and using this method now its create a error "failed to connect to server VS ".
C#
  private void btnBackup_Click(object sender, EventArgs e)
        {
            BackupDatabase("RPSJDB","Sa","manish","VS","c:\\RPSJ.mdf");
//            Back();
        }

        public void BackupDatabase(String databaseName, String userName,String password, String serverName, String destinationPath)
        {
            Backup sqlBackup = new Backup();
            sqlBackup.Action = BackupActionType.Database;
            sqlBackup.BackupSetDescription = "ArchiveDataBase:" + DateTime.Now.ToShortDateString();
            sqlBackup.BackupSetName = "Archive";
            sqlBackup.Database = "RPSJDB";
            BackupDeviceItem deviceItem = new BackupDeviceItem("c:\\RPSJDB\\mk.mdf", DeviceType.File);
            ServerConnection connection = new ServerConnection("VS", "Sa", "manish");
            Server sqlServer = new Server(connection);
            Database db = sqlServer.Databases["RPSJDB"];
            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);
        }
Posted
Updated 28-Oct-13 19:50pm
v2
Comments
Madhu Nair 29-Oct-13 2:08am    
Using the same connection string, are you able to connect to the server using SQL Management Studio?
Manish Arya 29-Oct-13 2:12am    
string connstr = @"Server=192.168.10.2\MSSQL;Initial Catalog=RPSJDB; Uid=Sa; Pwd=manish; Max Pool Size=100";

yes I m connected with sql management studio. I can insert,delete everything.

1 solution

try changing the below line from -
C#
ServerConnection connection = new ServerConnection("VS", "Sa", "manish");


To -

ServerConnection connection = new ServerConnection("192.168.10.2\MSSQL", "Sa", "manish");

OR

Refer This
 
Share this answer
 
v2
Comments
Manish Arya 29-Oct-13 2:33am    
same problem

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