Difference between revisions of "Connection Strings"

From Hostek.com Wiki
Jump to: navigation, search
(MSSQL Connection String (ASP))
 
(4 intermediate revisions by 2 users not shown)
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 81:
  
  
==MySQL Connection String (ASP)==
+
===MySQL Connection String (ASP)===
 +
For newer versions of MySQL, use the below code.
 
<pre>
 
<pre>
 
Dim conDB
 
Dim conDB
Line 23: Line 89:
 
Set conDB = CreateObject("ADODB.Connection")
 
Set conDB = CreateObject("ADODB.Connection")
 
'--You may need to use OPTION=3 instead of 16834
 
'--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"
+
conDB.Open "DRIVER=MySQL ODBC 5.1 Driver;SERVER=mysql10.hostek.com;OPTION=16834;USER=db_un;Password=db_pw;DATABASE=db_name"
 
'--Set the recordset
 
'--Set the recordset
 
Set rs = CreateObject("ADODB.Recordset")
 
Set rs = CreateObject("ADODB.Recordset")
 
</pre>
 
</pre>
  
 +
For older MySQL versions, you will want to use the below code.
 +
<pre>
 +
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")
 +
</pre>
  
==MSSQL Connection String (ASP)==
+
===MSSQL Connection String (ASP)===
 
<pre>
 
<pre>
ConnStr="DRIVER={SQL
+
ConnStr="DRIVER={SQLServer};SERVER=sql1.hostek.com;UID=db_username;PWD=db_password;DATABASE=db_name"
Server};SERVER=sql1.hostek.com;UID=db_username;PWD=db_password;DATABASE=db_name"
+
 
Conn.open ConnStr
 
Conn.open ConnStr
 
</pre>
 
</pre>

Latest revision as of 07:38, 15 November 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)

For newer versions of MySQL, use the below code.

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 5.1 Driver;SERVER=mysql10.hostek.com;OPTION=16834;USER=db_un;Password=db_pw;DATABASE=db_name"
'--Set the recordset
Set rs = CreateObject("ADODB.Recordset")

For older MySQL versions, you will want to use the below code.

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={SQLServer};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.