VBscript to connect to sql server and run query

VBScript provides one of the best ways to connect to any kind of database. That is through the ADODB connection.

You just need to have the correct connection string with the right credentials. If you have these two then you can write code to connect to almost any kind of database in just 3 lines.

In order to connect to the database, we need to follow below steps:

  1. Create an object of ADODB, like below: Set con=createobject("adodb.connection")
  2. Create an object of recordset : Set rs=createobject("adodb.recordset")
  3. Open the Connection using the connection string con.open"Driver={SQL Server};server=MySqlServer;uid=MyUserName;pwd=MyPassword;database=pubs" 
  4. Run the query using the connection object. rs.open "select * from emp",con

This will store the data into the recordset object rs.

This page has all the code needed for connecting and reading data from popular database types using VBscript.

How to connect to SQL Database

How to connect to Oracle Database

How to connect to MySQL Database

How to connect to an Excel file as Database

How to connect to Sybase Database

How to connect to MS Acess Database

How to get data from Excel as Database