0

I'm successfully able to connect to my database with the following method, however I have a connection string stored in my web.config that I obviously would rather use than this way where the credentials are exposed in code. However I haven't found a good way to do this. WebMatrix gives me a fatal connection error of 18 and I don't know how else I can do this off the top of my head.

What I currently have

Any ideas?

1 Answer 1

2

You can read the connection string from Web.config with something like this:

System.Configuration.ConfigurationManager.ConnectionStrings["someName"].ConnectionString;

which returns a string, just like myBuilder.ConnectionString does. So you can use it directly in place of what you have:

string connString = ConfigurationManager.ConnectionStrings["someName"].ConnectionString;
MySqlConnection myconn = new MySqlConnection(connString);
Sign up to request clarification or add additional context in comments.

7 Comments

hey david. I tried that and I'm getting "Unable to connect to any of the specified MySQL hosts.' SocketException: No such host is known" on myconn.Open();
This is how it looks in the config. <connectionStrings> <add name="jj_booking_system" connectionString="server=rds-booking-system.andtherestoftheendpoint.com,3306;database=jj_booking_system;uid=saydin;pwd=somepassword" providerName="System.Data.SqlClient" /> </connectionStrings>
@sushi7777: When you debug, have you confirmed that it's finding the right connection string from the config? If the connection string is correct and it can't connect then, well, it can't connect.
server=rds-booking-system.andtherestoftheendpoint.com,3306;database=jj_booking_system;uid=saydin;pwd=somepassword is literally what is in the connString variable. Bear in mind the above screenshot works fine...
@sushi7777: In your original working version, what was the runtime value for myBuilder.ConnectionString? Was it identical to the new runtime value?
|

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.