<%
************ 使用ADODB.Connect對象建立 ************************
Dim oConn
Dim sDatabaseName
sDatabaseName = "CodeCreateDB"
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "Provider=SQLOLEDB;Data Source=(local);User ID=sa;Password=;"
oConn.Execute "CREATE DATABASE " & sDatabaseName
%>
方法二:
<%@ Language=VBScript %>
<%
******************* 使用 SQLDMO Object 物件程式庫 *******************
Dim oSQLServer
Dim oDatabase
Dim oDBFileData
Dim oLogFile
Dim sDatabaseName
Dim sDatapath
資料庫名字
sDatabaseName = "CodeCreateDBTest"
資料檔案儲存路徑
sDatapath = "D:/"
建立Sql Server對象並進行連結,(local)處為Server名字
Set oSQLServer = Server.CreateObject("SQLDMO.SQLServer")
oSQLServer.LoginSecure = True 使用整合驗證integrated security
oSQLServer.Connect "(local)"
oSQLServer.Connect "(local)", "sa", "" 使用標準驗證 standard security
建立Database對象
Set oDatabase = Server.CreateObject("SQLDMO.Database")
oDatabase.Name = sDatabaseName
建立db檔案對象
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
建立記錄檔對象
Set oLogFile = Server.CreateObject("SQLDMO.LogFile")
With oLogFile
.Name = sDatabaseName & "_log"
.PhysicalName = sDatapath & "/" & sDatabaseName & "_log.ldf"
End With
把DB檔案對象和記錄檔對象添加到DataBase資料庫物件
oDatabase.FileGroups("PRIMARY").DBFiles.Add oDBFileData
oDatabase.TransactionLog.LogFiles.Add oLogFile
把資料庫添加到Sql server(create the database)
oSQLServer.Databases.Add oDatabase
關閉串連
oSQLServer.Close
釋放對象
Set oLogFile = Nothing
Set oDBFileData = Nothing
Set oDatabase = Nothing
Set oSQLServer = Nothing
%>