Answered by:
How to do database connection with this code

Question
-
User-775831949 posted
I have an external datasource and I have found this code.
I have put it in .cs file of my Web Form aspx below.
So what is next step to get this to become connectionstring on my aspx?public static string GetRDSConnectionString() { var appConfig = ConfigurationManager.AppSettings; string dbname = appConfig["RDS_DB_NAME"]; if (string.IsNullOrEmpty(dbname)) return null; string username = appConfig["RDS_USERNAME"]; string password = appConfig["RDS_PASSWORD"]; string hostname = appConfig["RDS_HOSTNAME"]; string port = appConfig["RDS_PORT"]; return "Data Source=" + hostname + ";Initial Catalog=" + dbname + ";User ID=" + username + ";Password=" + password + ";"; } }
Usually, I use this in .cs to do ADO connection. How should I modify the last line to use the data connectionstring created above ?
Thanksprotected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string constr = ConfigurationManager.ConnectionStrings["MyASPConnectionString"].ConnectionString;
Monday, January 22, 2018 2:08 PM
Answers
-
User475983607 posted
As with many of your posts, it helps if you explain the subject, what you are trying to do and why rather than posting code and making us guess.
The posted code snippet is reading values from the web.config appSetting node; RDS_DB_NAME, RDS_USERNAME, RDS_PASSWORD, RDS_HOSTNAME, and RDS_PORT. The code uses these appSetting values to build a connection string. If you already have a working connection string then you do not need this code snippet. If you are trying to connect to a new data store, contact support for the service you are using. They can tell you what values you need to store in order to connect to their service.
The following link explains the web.config appSetting nodes and how to read values from the node.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 22, 2018 2:20 PM -
User753101303 posted
Hi,
You'll use :
string constr = yourPageClass.GetRDSConnectionString();
Not sure placing this code inside a form is your best bet. Or this external source is used only from this particular page ?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 22, 2018 2:20 PM -
User-707554951 posted
Hi hkbeer
Put following code in the connectionStrings section in web.config
<connectionStrings> <add name="MyASPConnectionString" connectionString="Data Source=hostname;Initial Catalog=dbname;UserID=username;Password=password;" /> </connectionStrings>
Output:
Note: Replace hostname, dbname, username, password with real value you get in web.config
Best regards
Cathy
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 23, 2018 8:04 AM
All replies
-
User475983607 posted
As with many of your posts, it helps if you explain the subject, what you are trying to do and why rather than posting code and making us guess.
The posted code snippet is reading values from the web.config appSetting node; RDS_DB_NAME, RDS_USERNAME, RDS_PASSWORD, RDS_HOSTNAME, and RDS_PORT. The code uses these appSetting values to build a connection string. If you already have a working connection string then you do not need this code snippet. If you are trying to connect to a new data store, contact support for the service you are using. They can tell you what values you need to store in order to connect to their service.
The following link explains the web.config appSetting nodes and how to read values from the node.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 22, 2018 2:20 PM -
User753101303 posted
Hi,
You'll use :
string constr = yourPageClass.GetRDSConnectionString();
Not sure placing this code inside a form is your best bet. Or this external source is used only from this particular page ?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 22, 2018 2:20 PM -
User-707554951 posted
Hi hkbeer
Put following code in the connectionStrings section in web.config
<connectionStrings> <add name="MyASPConnectionString" connectionString="Data Source=hostname;Initial Catalog=dbname;UserID=username;Password=password;" /> </connectionStrings>
Output:
Note: Replace hostname, dbname, username, password with real value you get in web.config
Best regards
Cathy
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 23, 2018 8:04 AM