0

hope someone can help me. I created a server with a database and added one table in it. Also, i created a client that can send data to server and notify when connecting, But i can't connect to the database (located in server) from client i'm using "sqlconnection" but from some reason i can't connect, how can i connect to server database from client using "sqlconnection"?

this is what i tried:

        SqlConnection cn = new SqlConnection(@"Data Source=10.0.0.2\MSSQLLocalDB,8;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=;Password=");
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        string query = "Select * from [Employees]";
        da = new SqlDataAdapter(query, cn);
        da.Fill(dt);
        dataGridView1.DataSource = dt;
Filburt
  • 17,626
  • 12
  • 64
  • 115
RAZ
  • 55
  • 8
  • Are you sure the connection string you are using is correct? If in doubt, [maybe try this](http://stackoverflow.com/a/10480011/205233) to check your connection string. – Filburt Nov 01 '15 at 12:11

1 Answers1

0

Would be very helpful to have the error that you are getting. I suppose it is something like this

TITLE: Connect to Server
------------------------------

Cannot connect to 10.0.0.2

------------------------------
ADDITIONAL INFORMATION:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&EvtSrc=MSSQLServer&EvtID=53&LinkId=20476

------------------------------

The network path was not found

------------------------------

Anyway, it seems to me that in the connection string you have provided

"Data Source=10.0.0.2\MSSQLLocalDB,8;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=;Password="

you specify a port number 8 on the connection to the data source. That seems a bit suspicious, as per default 1433 is the MSSQL port and even further any port with number 1024 or less should be system reserved and not be used by user driven applications.

Please if this doesn't answer your question, provide the error message you are getting.

LIvanov
  • 1,126
  • 12
  • 30
  • This is what i get: "SQL Server. - SQL Server (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it" – RAZ Nov 01 '15 at 13:00
  • I think maybe i loose something (I'm new at database server client). I couldn't find an example with source code that demonstarte a client connecting adatabase via server (my connection to server is with sockets tcp) and show the data from tabe into a datafridview in client... – RAZ Nov 01 '15 at 13:05
  • @RanAzrad that looks exactly like a port misuse. Omit the ",8" part from your connection string, if you are using the default MS SQL configuration OR find out the port you are using and change the DataSource value respectively. – LIvanov Nov 01 '15 at 15:16
  • Still didn't solve it. Can you show me an example of database stored in server that client connecting to server and run queries ? – RAZ Nov 01 '15 at 18:46
  • I bet you didn't do any reconfiguration on the MS SQL installation so I guess if you just use `"Data Source=10.0.0.2\MSSQLLocalDB;Initial Catalog=dbase;User ID=;Password="` you will be able to connect. Also try connecting from an MS SQL client such as MS SQL Management studio. – LIvanov Nov 01 '15 at 21:00
  • This is the error: provider: SQL Network Interfaces, error: 26 Maybe you can give me a reference for doing that? – RAZ Nov 02 '15 at 05:54
  • Also, where can i find my username and password for databse? On server the username and password fields are empty – RAZ Nov 02 '15 at 07:47
  • This is the connection i use: cn = new SqlConnection(@"Data Source=192.168.1.102,1433;Network Library=DBMSSOCN;Initial Catalog=myDatabase;User ID=myid;Password=mypass"); and i receive in cn.open() an exception – RAZ Nov 02 '15 at 08:02
  • 1
    As far as my intuition goes, you have a named MS SQL instance installed with Windows Authentication only (this is the default installation configuration) and since you don't know what your username and pass are, I guess you never set them up. Therefore you will need to access the server with Windows Authentication, which would require your client to use the Win Credentials you are currently logged in with. – LIvanov Nov 02 '15 at 10:34
  • 1
    At this point I suggest you do some research on how in theory things should work. Take a look at those articles as references [Named and Default instances](http://serverfault.com/questions/80767/what-are-named-and-default-instance), [Connect to SQL Server using windows Authentication](https://msdn.microsoft.com/en-us/library/ff647396.aspx) – LIvanov Nov 02 '15 at 10:34