0

我曾經使用SQL-DMO在VBS中運行腳本來創建新數據庫。這是爲SQL Server 2000完成的,但是這不再適用於SQL Server 2008 R2。使用VBS創建SQL Server 2008 R2數據庫時出錯(SQL DMO)

腳本是這樣的:

Const C_SQL_UID = "sa" 
Const C_SQL_PWD = "sa" 
Const C_SQL_SERVER = "dbserver" 
Const C_SQL_TMPDBNAME = "TMP_DB" 
Const C_SQLDIR = "d:\data" 

Dim SqlServer: Set SqlServer = CreateObject("SQLDMO.SqlServer") 
SqlServer.Connect C_SQL_SERVER, C_SQL_UID, C_SQL_PWD 

Dim database: Set database = CreateObject("SQLDMO.Database") 
Dim dbDataFile: Set dbDataFile = CreateObject("SQLDMO.DBFile") 
Dim dbLogFile: Set dbLogFile = CreateObject("SQLDMO.LogFile") 

database.Name = C_SQL_TMPDBNAME 

dbDataFile.Name = C_SQL_TMPDBNAME 
dbDataFile.PhysicalName = C_SQLDIR & "\" & C_SQL_TMPDBNAME & "_Data.mdf" 
dbDataFile.PrimaryFile = True 
dbDataFile.Size = 2 
dbDataFile.FileGrowthType = 0 'SQLDMOGrowth_MB 
dbDataFile.FileGrowth = 1 

database.FileGroups.Item("PRIMARY").DBFiles.Add(dbDataFile) 

dbLogFile.Name = C_SQL_TMPDBNAME & "Log" 
dbLogFile.PhysicalName = C_SQLDIR & "\" & C_SQL_TMPDBNAME & "_Log.ldf" 
dbLogFile.Size = 2 

database.TransactionLog.LogFiles.Add(dbLogFile) 

On Error Resume Next 
SqlServer.Databases.Add(database) 
If Err Then 
    wscript.echo Now & " Error creating DB (" & Err.Number & ") " & Err.Description 
End If 
On Error GoTo 0 

當執行最後的指令(「SqlServer.Databases.Add(數據庫)」),我得到的錯誤號-2147219701 ...不包含任何描述。我不確定它可以從哪裏來。 我知道微軟放棄了DMO的事情,但由於SMO不支持VBScript,所以我沒有選擇。

任何想法?

回答

0

噢,最後我剛剛使用了一個很好的舊ADODB連接,並在T-SQL中提出了所有請求。 對於此腳本,它很簡單:

Dim conn 
Set conn = CreateObject("ADODB.Connection") 
conn.CommandTimeout = 9999 
conn.Provider = "SQLOLEDB" 
conn.ConnectionTimeout = 10 
conn.Open "server=" & C_SQL_SERVER & ";uid=" & C_SQL_UID & ";pwd=" & C_SQL_PWD & ";database=Master" 

sql = "CREATE DATABASE " & C_DB_NAME & " " 
sql = sql & "ON (NAME = '" & C_DB_NAME & "', FILENAME = '" & C_SQLDIR & "\" & C_DB_NAME & "_Data.mdf', SIZE = 5MB, MAXSIZE = UNLIMITED, FILEGROWTH = 5MB) " 
sql = sql & "LOG ON(NAME = '" & C_DB_NAME & "Log', FILENAME = '" & C_SQLDIR & "\" & C_DB_NAME & "_Log.ldf', SIZE = 5MB, MAXSIZE = UNLIMITED, FILEGROWTH = 5MB)" 
conn.Execute sql 
相關問題