我曾经使用SQL-DMO在VBS中运行脚本来创建新数据库。这是为SQL Server 2000完成的,但是这不再适用于SQL Server 2008 R2。
脚本是这样的:
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(database)”)时,我得到错误号-2147219701 ...它不包含任何描述。我不确定它的来源。 我知道微软放弃了DMO的事情,但是由于SMO不支持VBScript,我有点选择。
有什么想法吗?
答案 0 :(得分: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