0

I am facing some issues with connectivity . Question-

1)Is it possible that Web application access the databse but not console application?

2)When I am connecting my console app to our databse on server-"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible ",this error is coming though when I test the connection using setting tab in VS2010 for console application,it says Connection succeed,but as soon as i write Con.open,I get the error mentioned.

3)Also,If i am not able to connect to my db with new application,how come previous build web application on production are working fine.?

NOTE-There has been massive change in Server-Sql instances and all servers were upgraded.

Do I make sense?

Code I am using-

SqlConnection con;
            SqlDataReader reader;
            try
            {
                int id;
                con = new SqlConnection("Data Source=DAN\\C1;Initial Catalog=tablenames;User ID=user;Password=123");
                con.Open();//Error comes here

Exact same connection string works for webapplication but not for Console.Is it have something to do with sql server upgarde and changes.

Any help or guidance?

vish1990
  • 272
  • 3
  • 16
  • Make sure your console application uses the same connection string as the web application – Mainak Jul 08 '16 at 10:54
  • it does have same string :| – vish1990 Jul 08 '16 at 10:58
  • then the problem could be that your pc isn't connected to the network on which the db server is . try logging in to the db from sqlserver management studio. if you get the same error as above then you are not connected to the required network. – Mainak Jul 08 '16 at 11:07
  • actually all comes to the question-Is it possible that i get Test connection succeded in VS2010 (under Setting tab) but gives error while opening a connection -con.open() .Also,i can loggin to db with SQl Server management studio – vish1990 Jul 08 '16 at 11:09
  • take a look at this link http://stackoverflow.com/questions/1391503/a-network-related-or-instance-specific-error-occurred-while-establishing-a-conne – Mainak Jul 08 '16 at 11:16
  • I will cehck that in couple of hours(need admin access),but then,still if new application can not connect,how come old application are working fine that are already deployed – vish1990 Jul 08 '16 at 11:50

3 Answers3

0

okay well from what i can see you may want to check that you have imported and used all the required namespaces involved in an SQL connection , you will need to import the following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

After ensuring all the correct namespaces have been imported then you should check your connection string maybe there is a typing error or something there , remember if you make a mistake on the connection string the connection will be refused , and im not sure if this code will work but here goes try this method of connecting :

 SqlConnection con;
                SqlDataReader reader;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
con = new SqlConnection(connetionString);
                try
                {
                    int id;
                    con.Open();
                    Console.WriteLine("Connection Open ! ");
                    cnn.Close(); 
                } 
                catch (Exception ex) { Console.WriteLine("Can not open connection ! "); }

If the above code gives you problems you can try this other method of doing a SQL connection in a C# console project as seen in the link below:

http://www.c-sharpcorner.com/UploadFile/5089e0/how-to-create-single-connection-string-in-console-applicatio/

Daniel Goncalves
  • 164
  • 1
  • 11
  • Tried the link too.Connection succed message do come but when con.open() comes for execution,I get above mentioned error – vish1990 Jul 08 '16 at 12:24
  • mmmmm are you sure you have all the required references installed in your library because sometimes its a matter of connectors – Daniel Goncalves Jul 08 '16 at 13:19
0

First check in the Server Explorer whether you are connected to the Server or database.If u connected to the server, server name will be visible under data connection, right click on it go to properties there u can find connection string value. copy paste that value in your sql connection code. Try this it may solve your connection string problem.

  • No,i checked.the credential works fine and I can get connection string too.I believe the problem is at SQL Server end – vish1990 Jul 08 '16 at 12:53
0

Thanks everyone.But it was some security issue.Once I deploy my code on server,its working.Remote connectivity was somehow restricted in the sql server.

vish1990
  • 272
  • 3
  • 16