A connectionstring is sometimes a bit obscure. Do I need ODBC or OLE DB? When you need one and Google for it, you often find old ones that either don’t work at all anymore, or are not optimal. This article provides you with a couple of ASP.NET to SQL Server connectionstring examples, and as a bonus for ASP to MySQL too.
In this article I show you various connectionstrings to connect to an SQL Server or MySQL (MariaDB) database from either ASP.NET or classic ASP. Yes, really, classic ASP :) I recommend you always use SSL/TLS encryption for your connection where possible.
I put this up here, because I often forget which connectionstring to use when, when a client asks a question or encounters connection problems… Throughout the examples, replace ‘sql.example.com’, ‘mysql.example.com’, ‘db-user’, ‘P4ssword’ and ‘examplecom’ with your own information.
ASP.NET connectionstring for Microsoft SQL Server, using System.Data.SqlClient Namespace
// asp.net connectionstring for sql server
string strConnection = "Data Source=sql.example.com;" +
"User ID=db-user;" +
"Password=P4ssword;" +
"Initial Catalog=examplecom;";
SqlConnection objConnection = new SqlConnection(strConn);
objConnection.Open()
Code language: C# (cs)
Enable TLS/SSL in System.Data.SqlClient – encrypted SQL connection
Ideally all connections should be encrypted (using TLS/SSL), so that data transfers between a SQL Server instance and a client application are secure. To enable the use of TLS/SSL in your System.Data.SqlClient connection to encrypt and secure the connection, add Encrypt=True and TrustServerCertificate=True like:
Encrypt=True; TrustServerCertificate=True;
Code language: HTML, XML (xml)
Microsoft’s documentation states:
Beginning in .NET Framework 4.5, when
SqlConnection.ConnectionString PropertyTrustServerCertificate
is set to false andEncrypt
set to true, the server name (or IP address) in a SQL Server SSL certificate must exactly match the server name (or IP address) specified in the connectionstring. Otherwise, the connection attempt will fail.
ASP.NET Connector/NET to MySQL
A fully-managed ADO.NET driver for MySQL. Also see my MySql.Data.MySqlClient test script for reference and more information.
string strConn = "server=mysql.example.com;" +
"user=db-user;database=examplecom;" +
"port=3306;password=P4ssword;
SslMode=REQUIRED;";
MySqlConnection conn = new MySqlConnection(strConn);
Code language: C# (cs)
ASP.NET Connector/ODBC to MySQL
Connector/ODBC is a standardized database driver for Windows, Linux, Mac OS X, and Unix platforms. Also see my MySQL Connector/ODBC test script for reference and more information.
string strConn = "DRIVER={MySQL ODBC 8.0 Unicode Driver};" +
"Provider=MSDASQL;" +
"SERVER=mysql.example.com;" +
"DATABASE=examplecom;" +
"UID=db-user;" +
"PASSWORD=P4ssword;" +
"SslMode=REQUIRED;";
OdbcConnection MyConnection = new OdbcConnection(strConn);
MyConnection.Open();
Code language: C# (cs)
Classic ASP connection to SQL Server using ODBC
Dim strConn
' Microsoft SQL Server ODBC Driver
Set strConn = Server.CreateObject("ADODB.Connection")
Connstr = Connstr & "DRIVER={ODBC Driver 18 for SQL Server};"
Connstr = Connstr & "DATABASE=examplecom;"
Connstr = Connstr & "SERVER=sql.example.com;"
' open the connection here using: Connstr, "username", "password"
SqlConn.Open Connstr, "db-user", "P4ssword"
Code language: C# (cs)
Choose your DRIVER=
string carefully. Use “{ODBC Driver 18 for SQL Server}” if ODBC Driver for SQL Server 18 is installed, otherwise use “{ODBC Driver 17 for SQL Server}”, “{SQL Server}”, or ask your hosting company for instructions.
By not adding “db-user” and “P4ssword” to Connstr
, you can safely print the connection string for debugging purposes using: Response.Write(Connstr)
.
SQL connectionstring with encryption in ASP
Use the connectionstring properties Encrypt and trustServerCertificate (if needed) to allow applications to use Transport Layer Security (TLS) encryption. For example:
Set strConn = Server.CreateObject("ADODB.Connection")
Connstr = Connstr & "DRIVER={ODBC Driver 18 for SQL Server};"
Connstr = Connstr & "DATABASE=examplecom;"
Connstr = Connstr & "SERVER=sql.example.com;"
Connstr = Connstr & "Encrypt=Yes;"
Connstr = Connstr & TrustServerCertificate=Yes;"
Code language: VBScript (vbscript)
You may find more information in Microsofts article Using Encryption Without Validation.
Microsoft OLE DB Provider voor SQL Server (MSOLEDBSQL)
Dim strConn
Set strConn = Server.CreateObject("ADODB.Connection")
Connstr = "Provider=MSOLEDBSQL;"
Connstr = Connstr & "DATABASE=examplecom;"
Connstr = Connstr & "SERVER=sql.example.com;"
' open the connection here using: Connstr, "db-user", "P4ssword"
SqlConn.Open Connstr, "db-user", "P4ssword"
Code language: VBScript (vbscript)
Deprecated: Microsoft OLE DB Provider for SQL Server (SQLOLEDB)
Only for archival purposes. Do not use in production!
Dim oldSqlConn
Set oldSqlConn = Server.CreateObject("ADODB.Connection")
Connstr = "Provider=SQLOLEDB;"
Connstr = Connstr & "DATABASE=examplecom;"
Connstr = Connstr & "SERVER=sql.example.com;"
' open the connection here using: Connstr, "username", "password"
oldSqlConn.Open Connstr, "db-user", "P4ssword"
Code language: VBScript (vbscript)
ASP connectionstring to MySQL databases using Connector/ODBC
Dim strConn
Set sCstrConnonn = Server.CreateObject("ADODB.Connection")
strConn.Open "Provider=MSDASQL;" & _
"DRIVER={MySQL ODBC 8.0 Unicode Driver};" &_
"Server=mysql.example.com;" & _
"Database=examplecom;" & _
"UID=db-user;" & _
"PWD=P4ssword;" &_
"SSLMODE=REQUIRED;"
Code language: VBScript (vbscript)
UTF-8 with MySQL and classic ASP
Internally, VBScript runs with UCS2 character encoding, not UTF-8. UCS2 stands for 2-byte Universal Character Set and is a character encoding standard in which characters are represented by a fixed-length 16 bits (2 bytes).
If you want to fully support UTF-8 in ASP, for example with MySQL utf8mb4, then you have to define UCS2 as your connection’s charset:
charset=ucs2;
This makes the ASP connectionstring for UTF-8 support:
strConn.Open "Provider=MSDASQL;" & _
"DRIVER={MySQL ODBC 8.0 Unicode Driver};" &_
"Server=mysql.example.com;" & _
"Database=examplecom;" & _
"UID=db-user;" & _
"PWD=P4ssword;" &_
"SSLMODE=REQUIRED;" &_
"charset=ucs2;"
Code language: VBScript (vbscript)
To save UTF-8 encoded values (é, ë, á, ö, etc) in your MySQL database table, you must set ASP’s codepage to 65001 too:
<%@language="VBScript" codepage="65001"%>
Code language: HTML, XML (xml)
Conclusion
It is always handy to have multiple connectionstring examples ready to use, whether it’s for ASP.NET or classic ASP. This makes developing web applications easier. Use highest available software versions and always encrypt connections.
Show Your Support

If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ☕($10 USD or €10 EUR for example). And please share the love and help others make use of this website. Thank you very much! <3 ❤️
Great addition Greg, thanks!
Just in case someone stumbles across this blog post and is struggling with login to a database. If your username or password contains some special characters, such as £, then you also need to make sure you have the codepage line in your file as well. That is true for MySQL and MsSQL