2

I am going off of this tutorial: http://www.dotnetperls.com/sqlclient . Instead of adding a data source and a having visual studio compile my connecting string - I want to do it myself. The reason being is that the database will not always be the same and I want this application to be able to use different databases depending on which I point it to.

So how can I manually create the connection string? I am using SQL Server 2005.

3
  • pass the connectionstring to the constructor of SqlConnection... Commented Sep 22, 2011 at 14:18
  • Can you give an example of this. I apologize but I am somewhat new to C#. Commented Sep 22, 2011 at 14:19
  • The problem is also I am unaware of the format for the connection string. Commented Sep 22, 2011 at 14:19

3 Answers 3

2

Step 1: Go to connectionstrings.com and find the proper format for your database.

Step 2: Plug in the appropriate values to the connection string.

Step 3: Pass that string to the constructor of SqlConnection.

I would also suggest storing your connection string in your app.config/web.config file. You can then modify them easily if needed. The proper format can be found at MSDN - connectionStrings element. You then change your code to:

 SqlConnection sqlConn = new SqlConnection(
     ConfigurationManager.ConnectionStrings["ConnStringName"].ConnectionString);
Sign up to request clarification or add additional context in comments.

3 Comments

How do I select the table I want to read/write from if it is a remote database?
@pss1337 - That would be part of your SQL query.
Gotcha, I forgot it is just like regular SQL statements after I connect to the database. Does this look like it would work, or do I need to pass the connection string differently (test values for pwd,and uname, etc). pastebin.com/4Mqq0YZN
0

I don't see where the connection string is "compiled".

In the code

SqlConnection con = new SqlConnection(
    ConsoleApplication1.Properties.Settings.Default.masterConnectionString)

ConsoleApplication1.Properties.Settings.Default.masterConnectionString is a field and it can be replaced with any other appropriate string.

Comments

0

for SQL Server format of the connection string is

"Data Source = server_address; Initial Catalog = database_name; User ID = UserId; Password = **;"

save this connection string in a string variable and use with connection object.

either way you can add in web.config file.

<ConnectionString>
<add name = "name_of_connecctionString" ConnectionString = "Data Source = server_address;       Initial Catalog = database_name; User ID = UserId; Password = ****;" ProviderName = "system.Data.SqlClient"/>
</ConnectionString>

you can change the provider as needed by you.

then in code behind file access this particular connection string using configuration manager.

Comments

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.