1

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.

1
  • 1
    Well if it works where is the problem ? Commented May 29, 2015 at 14:27

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @AlexK. included that link in answer.
I'm using OleDb because I want to handle different Database types. I'm just using SQL DB in my question as an example. And I want to use IP addresses in data source so that I can connect to the DB when it's on another network. Imagine the SQL DB resides on a server somewhere in US and my client want to connect to it from his PC in Europe . What's the connection string to do such a thing ? that's the question.
No, that's not a recommended as well as best way. If you really want to handle different DB types then try using Abstract DB factory pattern but use the exact DB provider,
Almost every popular database engine on the market today (SQL Server, Oracle, MySql, Sqlite, PostgreSql etc...) have ADO.Net connectors. I still don't get why you'd want to use OleDbConnection. In addition to all that, the ADO.Net providers provide you with a ConnectionBuilder class that would handle all this connection string stuff for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.