明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

ASP創(chuàng)建SQL Server數(shù)據(jù)庫的2種方法

[摘要]<% ************ 使用ADODB.Connect對象創(chuàng)建 ************************Dim oConnDim sDatabaseNamesDatabaseName = "CodeCreateDB"Set oConn = Server.C...

<%
 ************ 使用ADODB.Connect對象創(chuàng)建 ************************
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

數(shù)據(jù)庫名字
sDatabaseName = "CodeCreateDBTest"
數(shù)據(jù)文件保存路徑
sDatapath = "D:\"

創(chuàng)建Sql Server對象并進行鏈接,(local)處為Server名字
Set oSQLServer = Server.CreateObject("SQLDMO.SQLServer")
  oSQLServer.LoginSecure = True        使用集成驗證integrated security
  oSQLServer.Connect "(local)"
oSQLServer.Connect "(local)", "sa", ""  使用標準驗證 standard security

 創(chuàng)建Database對象
Set oDatabase =  Server.CreateObject("SQLDMO.Database")
oDatabase.Name = sDatabaseName

 創(chuàng)建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

 創(chuàng)建日志文件對象
Set oLogFile =  Server.CreateObject("SQLDMO.LogFile")
With oLogFile
    .Name = sDatabaseName & "_log"
    .PhysicalName = sDatapath & "\" & sDatabaseName & "_log.ldf"
End With

把DB文件對象和日志文件對象添加到DataBase數(shù)據(jù)庫對象
oDatabase.FileGroups("PRIMARY").DBFiles.Add oDBFileData
oDatabase.TransactionLog.LogFiles.Add oLogFile

 把數(shù)據(jù)庫添加到Sql server(create the database)
oSQLServer.Databases.Add oDatabase

 關閉連接
oSQLServer.Close

 釋放對象
Set oLogFile = Nothing
Set oDBFileData = Nothing
Set oDatabase = Nothing
Set oSQLServer = Nothing
%>