Difference between revisions of "Connection Strings"

From Hostek.com Wiki
Jump to: navigation, search
(Connection string examples.)
Line 1: Line 1:
 
__FORCETOC__
 
__FORCETOC__
  
 +
Connection string examples.
  
==Access Database Connection String Examples==
+
==ASP.Net==
  
===Using DSN-less method===
+
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====
 
<pre>Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\home\mysite.com\db\myfile.mdb;"</pre>
 
<pre>Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\home\mysite.com\db\myfile.mdb;"</pre>
  
===Using DSN===
+
====Using DSN====
 
<pre>
 
<pre>
 
Conn.Open "MyDSN"
 
Conn.Open "MyDSN"
Line 16: Line 80:
  
  
==MySQL Connection String (ASP)==
+
===MySQL Connection String (ASP)===
 
<pre>
 
<pre>
 
Dim conDB
 
Dim conDB
Line 29: Line 93:
  
  
==MSSQL Connection String (ASP)==
+
===MSSQL Connection String (ASP)===
 
<pre>
 
<pre>
 
ConnStr="DRIVER={SQL
 
ConnStr="DRIVER={SQL

Revision as of 22:20, 25 January 2013


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.