<%
************Create using ADODB.Connect object************************
Dim oConn
DimsDatabaseName
sDatabaseName = "CodeCreateDB"
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "Provider=SQLOLEDB;Data Source=(local);User ID=sa;Password=;"
oConn.Execute "CREATE DATABASE " & sDatabaseName
%>
Method 2:
<%@ Language=VBScript %>
<%
********************* Using SQLDMO Object object library**********************
Dim oSQLServer
Dim oDatabase
Dim oDBFileData
Dim oLogFile
DimsDatabaseName
DimsDatapath
database name
sDatabaseName = "CodeCreateDBTest"
Data file saving path
sDatapath = "D:"
creates a Sql Server object and links it, (local) is the Server name
Set oSQLServer = Server.CreateObject("SQLDMO.SQLServer")
oSQLServer.LoginSecure = True uses integrated security
oSQLServer.Connect "(local)"
oSQLServer.Connect "(local)", "sa", ""
Create a Database object
using standard authentication standard security
Set oDatabase = Server.CreateObject("SQLDMO.Database")
oDatabase.Name = sDatabaseName
creates db file object
Set oDBFileData = Server.CreateObject("SQLDMO.DBFile")
With oDBFileData
.Name = sDatabaseName & "_data"
.PhysicalName = sDatapath & "" & sDatabaseName & "_data.mdf"
.PrimaryFile = True
.FileGrowthType = SQLDMOGrowth_MB
.FileGrowth = 1
End With
creates log file object
Set oLogFile = Server.CreateObject("SQLDMO.LogFile")
With oLogFile
.Name = sDatabaseName & "_log"
.PhysicalName = sDatapath & "" & sDatabaseName & "_log.ldf"
End With
adds the DB file object and log file object to the DataBase database object
oDatabase.FileGroups("PRIMARY").DBFiles.Add oDBFileData
oDatabase.TransactionLog.LogFiles.Add oLogFile
adds the database to Sql server (create the database)
oSQLServer.Databases.Add oDatabaseClose
connection
oSQLServer.Closereleases
the object
Set oLogFile = Nothing
Set oDBFileData = Nothing
Set oDatabase = Nothing
Set oSQLServer = Nothing
%>