Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public void GetBackup(string strDBName)
{
    if (string.IsNullOrEmpty(strDBName))
    {
        MessageBox.Show("Server Name & Database can not be Blank");
        return;
    }
    else
    {                
        string destinationPath = ConfigurationManager.AppSettings.Get("BackupDestinationPath");
        

BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
            
          
             ServerConnection connection = new ServerConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString);               
           
            Server sqlServer = new Server(connection);                
            Database db = sqlServer.Databases[strDBName];

The above line Database db = sqlServer.Databases[strDBName]; is causing the below mentioned error.

Error : "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not> found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"}

Message : Failed to connect to server Data
Source=PC210090\\SQLEXPRESS;Initial Catalog=testdb2;Persist Security
Info=True;User ID=sa;password=password-1"}
Posted
Updated 15-Oct-15 20:40pm
v2
Comments
Tomas Takac 16-Oct-15 2:41am    
Did you try to connect to the database using management studio?
Sujith Karivelil 16-Oct-15 2:49am    
Its more easy to specify the database in connection string

This error message informs you that it is not possible to connect to SQL Server.You need to troubleshoot your DB connection.

The possible reasons and their elimination is described in below link:
7 things to check to resolve “A network-related or instance-specific error occurred while establishing a connection to SQL Server[^]


--Amy
 
Share this answer
 
I think more easiest way is to specify the database name within the connection string. So your connection string will be looks like:

HTML
<add key="connectionstring" value="Server=myserver;Initial Catalog=myDatabase_name;Persist Security Info=True;MultipleActiveResultSets=True;User ID=****;Password=****;Max Pool Size=90;" />
 
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