OracleConnection, web.config, ODP.NET, and ASP.NET / C#
wblacklJan 11 2008 — edited Jan 12 2008I had a hard time finding this information on the web, so I decided to post it here for someone else. I was finding it difficult to locate information on the proper method to store the database username and password in the web.config file, but reference it in my C# code while using ODP.NET. The problem was how to get the OracleConnection to use the connectionString in the web.config file. The trick is to use the ConfigurationManager.ConnectionStrings. See below. I hope this is helpful. It may not be the best way to do it, but it seems to work.
void Login_Click(Object sender, EventArgs E)
{
OracleConnection MyConn = new OracleConnection(ConfigurationManager.ConnectionStrings["oracle_db"].ConnectionString);
string strCommandText = "SELECT SYSDATE FROM DUAL;”
OracleCommand command = new OracleCommand(strCommandText);
command.Connection = MyConn;
MyConn.Open();
// application program code HERE //
MyConn.Close();
MyConn.Dispose();
}
<configuration>
<connectionStrings>
<add name="oracle_db"
connectionString="Data Source=tns_name; UserID=acct_name;Password=acct_passwd;" providerName="System.Data.OracleClient"/>
</connectionStrings>
</configuration>
Tns_name is the name you use to reference to oracle database in the tnsnames.ora file.
Acct_name is the database user login.
Acct_passwd is the database password.