Difference between revisions of "Connection Strings"

From Hostek.com Wiki
Jump to: navigation, search
(Connection string examples.)
(Basic connection string)
Line 16: Line 16:
 
  <configuration>
 
  <configuration>
 
   <connectionStrings>  
 
   <connectionStrings>  
     <add name="SQLServerConnection" connectionString="Server=myServerName;Database=myDatabaseName;User Id=myUsername;Password=myPassword;" />
+
     <add name="SQLServerConnection" connectionString="Server=myServerName;
 +
    Database=myDatabaseName;User Id=myUsername;Password=myPassword;" />
 
   </connectionStrings>
 
   </connectionStrings>
 
  </configuration>
 
  </configuration>
Line 25: Line 26:
 
How to create connection object in your code using the above connection string:
 
How to create connection object in your code using the above connection string:
 
  System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient(System.Configuration.ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString);
 
  System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient(System.Configuration.ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString);
 
  
 
====How to override default ASPNet Membership connection string====
 
====How to override default ASPNet Membership connection string====

Revision as of 17:52, 9 February 2014


Connection string examples.

ASP.Net

connection string examples for ASP.Net.

SQL Server (MSSQL)

SQL Server connection string examples.

Basic connection string

web.config:

<configuration>
  <connectionStrings> 
    <add name="SQLServerConnection" connectionString="Server=myServerName;
    Database=myDatabaseName;User Id=myUsername;Password=myPassword;" />
  </connectionStrings>
</configuration>

NOTE: Replace myServerName, myDatabaseName, myUsername, and myPassword with the correct details for your database.


How to create connection object in your code using the above connection string:

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient(System.Configuration.ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString);

How to override default ASPNet Membership connection string

web.config:

<configuration>
  <connectionStrings>
    <remove name="LocalSQLServer" />
    <add name="LocalSQLServer" connectionString="Server=myServerName;Database=myDatabaseName;User Id=myUsername;Password=myPassword;" />
  </connectionStrings>
</configuration>

NOTE: Replace myServerName, myDatabaseName, myUsername, and myPassword with the correct details for your database. For overriding the default ASPNet Membership connection string, it is important that the connection string name be left as "LocalSQLServer".

MySQL

MySQL connection string example:

NOTE: This example requires the following library from mysql.com to be referenced in your project (also ensure that 'CopyLocal' is set to 'True' on the reference):

http://dev.mysql.com/downloads/connector/net/

web.config:

<configuration>
  <connectionStrings> 
    <add name="MySQLConnection" connectionString="Datasource=myServerName;Database=myDatabaseName;uid=myUsername;pwd=myPassword;" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>
</configuration>

NOTE: Replace myServerName, myDatabaseName, myUsername, and myPassword with the correct details for your database.


How to create connection object in your code using the above connection string:

MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(
   System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString
);


Classic ASP

Connection string examples for classic ASP.

Access Database Connection String Examples

Using DSN-less method

Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\home\mysite.com\db\myfile.mdb;"

Using DSN

Conn.Open "MyDSN"

Set Conn = CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\home\mysite.com\db\myfile.mdb;"


MySQL Connection String (ASP)

Dim conDB
Dim rs
'--Set the connection
Set conDB = CreateObject("ADODB.Connection")
'--You may need to use OPTION=3 instead of 16834
conDB.Open "DRIVER=MySQL ODBC 3.51 Driver;SERVER=mysql10.hostek.com;OPTION=16834;USER=db_un;Password=db_pw;DATABASE=db_name"
'--Set the recordset
Set rs = CreateObject("ADODB.Recordset")


MSSQL Connection String (ASP)

ConnStr="DRIVER={SQL
Server};SERVER=sql1.hostek.com;UID=db_username;PWD=db_password;DATABASE=db_name"
Conn.open ConnStr

Replace the username, password, and database name with the information for your database.