Database Connection Strings for ASP.NET (web.config)
C# and .NETsql-serverconnection-stringsasp-netoraclemysql
Site Admin · 3/6/2020
Site Admin · 3/6/2020
ASP.NET applications keep their database connection strings in the section of . Below are examples for three databases. Replace the server, database, user and password with your own, and use the that matches the driver you have installed.
The long form describes the listener explicitly:
The short form (host, port and service name) does the same:
and are Oracle's classic demo account and password.
With a SQL Server login (user name and password):
The same thing with the shorter keywords , , and :
With Windows authentication, which needs no password in the file ( is the instance name):
<connectionStrings>web.configproviderNamescotttigerServerDatabaseuidpwdSQLEXPRESSIntegrated Security=trueweb.configweb.configappsettings.jsonsaroot<connectionStrings>
<add name="OracleConnectionString"
connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVER=127.0.0.1)(SID=ORCLSID)));User Id=scott;Password=tiger;"
providerName="Oracle.DataAccess.Client" />
</connectionStrings><add name="OracleConnectionString"
connectionString="Data Source=127.0.0.1:1521/orcl;User ID=scott;Password=tiger;"
providerName="Oracle.DataAccess.Client" /><add name="MSSQLConnectionString"
connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;Password=YourPassword;"
providerName="System.Data.SqlClient" /><add name="MSSQLConnectionString"
connectionString="Server=127.0.0.1;Database=Northwind;uid=sa;pwd=YourPassword;"
providerName="System.Data.SqlClient" /><add name="MSSQLConnectionString"
connectionString="Data Source=YOUR-PC\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=true"
providerName="System.Data.SqlClient" /><add name="MySqlConnectionString"
connectionString="Server=127.0.0.1;Port=3306;Database=dbname;Uid=root;Pwd=YourPassword;"
providerName="MySql.Data.MySqlClient" />// needs a reference to System.Configuration
string connectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["MSSQLConnectionString"].ConnectionString;
using (var connection = new System.Data.SqlClient.SqlConnection(connectionString))
{
connection.Open();
// run your commands here
}
Comments
Loading comments…