Scripting > mrScriptBasic overview > mrScriptBasic examples > 8: Creating a SQL Server database
 
8: Creating a SQL Server database
This example shows how to create a SQL Server database from mrScriptBasic. To run this example, you need either SQL Server or the SQL Server Desktop Engine (MSDE) to be installed.
The example uses ADO to connect to the SQL Server referred to as localhost and execute an SQL query to create a database called MyNewDatabase. The database is created only if a database with the same name does not already exist on the server.
Dim ServerName, DatabaseName, SQLStatement
Dim ConnectionString, adoConnection

ServerName = "localhost"
DatabaseName = "MyNewDatabase"

ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source="+ServerName
SQLStatement = "IF NOT EXISTS (Select '*' From master..sysdatabases Where Name = '"+DatabaseName+"') BEGIN CREATE DATABASE "+DatabaseName+" END"

' Use ADO to open a connection to the server and execute the SQL query
Set adoConnection = CreateObject("ADODB.Connection")

adoConnection.Open(ConnectionString)
adoConnection.Execute(SQLStatement)
adoConnection.Close()
See also
mrScriptBasic examples