0

Is there a way of inputting username, password & data source name from user instead of hardcoding them?

Right now I have hardcoded in app.config like this

<connectionStrings>
<add name="SQLConnectionString" connectionString="Data Source=db01\instance01;Initial Catalog=testdb; MultipleActiveResultSets=true;User ID=testuser;Password=readonly; Connection Timeout=60;" providerName="System.Data.SqlClient"/>
</connectionStrings>

When I run the console app, I want it to print how to enter the parameters and then accept parameters. How can I do this?

Thanks Rashmi

2
  • 1
    You should look into string.Format() function, at least. What have you tried, btw? Commented Mar 3, 2014 at 16:19
  • Use the SqlConnectionStringBuilder class. Commented Mar 3, 2014 at 16:21

1 Answer 1

1

You can simply create a SqlConnection and set the connection string as first parameter.

string username = Console.ReadLine();

SqlConnection conn = new SqlConnection("User ID=" + username);

Additionally you can take a look at the SqlConnectionStringBuilder that does more for the work for you.

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

3 Comments

sorry, got pulled into another project. Haven't had the time to work on this. Will definitely post my results.
got it to work. Console.WriteLine("Enter DataSource"); stringBuilder.DataSource = Console.ReadLine(); Console.WriteLine("Enter userid"); & so on Console.WriteLine("Generated Connection String = " + stringBuilder.ConnectionString); How can I show "**" when user types in password?
You can't. You can hide input from the user though. See msdn.microsoft.com/en-us/library/x3h8xffw(v=vs.110).aspx

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.