Mahmoud Ghandour
Mahmoud Ghandour

Reputation: 63

Connection String via IP Address For SQL Server

I want to create connection string so that I can connect to SQL database remotely in my C# code using OleDb as a provider and the server IP address.

Note 1 : I'm using OleDb because I want to handle different database types. SQL is just an example. Note 2 : The database resides on another machine (machine on another network)

I did all the setup to connect from another machine (firewall,Enable TCP/IP..etc), and now I can connect remotely using Microsoft SQL Server Management 2014 by specifying (server name : My Computer Name-PC\Instance name) and using SQL Authentication then entering the username and password and press connect and then it goes well, I connect successfully.

BUT I TRIED A LOT OF COMBINATIONS TO BUILD THE CONNECTION STRING IN MY C# CODE AND NONE OF THEM WORKS EXCEPT THIS :

OleDbConnection conn = new OleDbConnection(@"provider = sqloledb; data source = MyCompName-PC\sqlexpress; Initial Catalog = DataBase1 ; user id = MyUsername ; password = MyPassword ;");

Otherwise if I try to use my server public IP address instead of MyCompName in Data Source it keeps giving me error : server not found or access denied.

I search in connectionstrings.com but problem is still there.

Upvotes: 1

Views: 3405

Answers (1)

Rahul
Rahul

Reputation: 77896

From your post it looks like you are trying to connect to a SQL Server database then why are you using OleDbConnection? instead of using SQL Server connection provider.

OleDbConnection connection provider is used for connecting to MS Access database.

You should be using a SqlConnection class like

SqlConnection conn = new SqlConnection("data source = MyCompName-PC\sqlexpress; Initial Catalog = DataBase1 ; user id = MyUsername ; password = MyPassword ;")

See SQLConnection Reference for more information as commented by @AlexK.

Upvotes: 2

Related Questions