背景:想要通过VBS 创建一个SQL数据库;(同一个VBS脚本程序!)
在项目文件夹下创建一个 zzzz.mdf zzzz.ldf文件;
在WIN10:WINCC7.4中成功没有问题,版本Update6;
在WIN7: WINCC7.3中 错误,SQL提示 “没有对象”; 7.3版本我已经装到了Update17了!!
附上我的脚本:
Dim blnRecInitOk
Function InitSQLDB(Byval immer)
Dim objSQLServer 'As New SQLDMO.SQLServer
Dim objDatabase 'As New SQLDMO.Database
Dim objDBFile 'As New SQLDMO.DBFile
Dim objLogFile 'As New SQLDMO.LogFile
Dim objFileSystem
Dim strDatabaseName
Dim strDBFilePath
Dim szRName
Dim szRComment
Dim szDSN
Dim ret
Const Mydebug = 1
Dim objCommand
Dim strConnection
Dim objConnection
Dim objFSO
Dim RFile
Dim TempFile
Dim objNet
Dim strCacls
Dim objShell
If ( blnRecInitOk = True) And (immer = False )Then '
Exit Function
End If
strDBFilePath = "D:\V9_WINCC\64057096_Ningbo\"'HMIRuntime.Tags("ProjectPath").Read
If Mydebug = 1 Then HMIRuntime.Trace vbCrLf & "InitRecipe.bmo: " & strDBFilePath & vbCrLf
If Len(strDBFilePath) < 2 Then
Exit Function '
End If
szDSN = "ST-PGM5-CN01"''HMIRuntime.Tags("@ServerName").Read
If Len(szDSN) < 2 Then
Exit Function '
End If
blnRecInitOk = True '
''创建的SQL数据库 名字是 zzzzz
strDatabaseName = "zzzzz"
On Error Resume Next
Set objSQLServer=CreateObject("SQLDMO.SQLServer")
objSQLServer.LoginSecure = True
objSQLServer.Connect szDSN &"\wincc"
On Error Resume Next
' Set database names
Set objDatabase=CreateObject("SQLDMO.Database")
objDatabase.Name = strDatabaseName
On Error Resume Next
'
Set objFileSystem=CreateObject("Scripting.FileSystemObject")
If Not objFileSystem.FileExists (strDBFilePath & strDatabaseName & ".mdf") Then
objSQLServer.DetachDB strDatabaseName
If Mydebug = 1 Then HMIRuntime.Trace "DetachDatabase: " & strDatabaseName
End If
Set objFileSystem = Nothing
Err.Clear
' check if attached database is in the desired path
If objSQLServer.Databases(strDatabaseName).PrimaryFilePath = strDBFilePath Then
'Already connected to the database in the correct project
If Mydebug = 1 Then HMIRuntime.Trace "CreateDatabase: " & strDatabaseName & " already connected!"
Else
'Connected to the database in the wrong path - detach database
If Mydebug = 1 Then Trace "Detatch wrong Database: " & strDatabaseName & " connected To wrong path, database will be detached!"
'detach database
objSQLServer.DetachDB strDatabaseName
End If
' database is not attached
'clear error
Err.Clear
On Error Resume Next
' Database file already existing?
Set objFileSystem=CreateObject("Scripting.FileSystemObject")
If Mydebug = 1 Then MsgBOX "CreateDatabase: file exists ?: " & strDBFilePath & strDatabaseName & ".mdf"
If Not objFileSystem.FileExists (strDBFilePath & strDatabaseName & ".mdf") Then
Set objNet = CreateObject("WScript.Network")
strCacls = "cmd /c " &Chr(34) & "echo y|cacls " & strDBFilePath & strDatabaseName &_
".mdf" &" /g " &objNet.UserName &":F" & Chr(34)
If mydebug = 1 Then MsgBOX "mdf doesn't exist,and cacls cmd is ::" &strCacls
Set objShell = CreateObject("Wscript.Shell")
objShell.Run strCacls , 2, True 'chr(34)="" wg. Blank in program files
strCacls = "cmd /c " &Chr(34) & "echo y|cacls " & strDBFilePath & strDatabaseName &_
".ldf" &" /g " &objNet.UserName &":F" & Chr(34)
If mydebug = 1 Then Msgbox strCacls
Set objShell = CreateObject("Wscript.Shell")
objShell.Run strCacls , 2, true 'chr(34)="" wg. Blank in program files
Set objShell = Nothing
Set objNet = Nothing
End If
If Mydebug Then Msgbox "Now we will create mdf file in the Folder"
Err.Clear
' Create new database file
If Mydebug = 1 Then Msgbox "Init : " & strDBFilePath & strDatabaseName & ".mdf does not exist - will be created" & vbCrLf
' Set properties of the database file
Set objDBFile=CreateObject("SQLDMO.DBFile")
objDBFile.Name = strDatabaseName & ".mdf"
objDBFile.PhysicalName = strDBFilePath & strDatabaseName & ".mdf"
' Add file to database group
objDatabase.FileGroups("PRIMARY").DBFiles.Add objDBFile
If Err.Number <> 0 Then '' 【就是这里在V7.3中提示 没有对象,而V7.4却没问题】
Msgbox "Initrecipe DB_create recipe database _Error: "& Hex(Err.Number)& vbCrLf &"Descr: "& Err.Description & vbCrLf
ret = 1
Else
Msgbox "mdf OK"
End If
' Set properties of the logfile
Set objLogFile=CreateObject("SQLDMO.LogFile")
objLogFile.Name = strDatabaseName & ".ldf"
objLogFile.PhysicalName = strDBFilePath & strDatabaseName & ".ldf"
objDatabase.TransactionLog.LogFiles.Add objLogFile
If Err.Number <> 0 Then
If Mydebug Then Msgbox "Initrecipe DB_add log _Error: "& Hex(Err.Number)& vbCrLf &"Descr: "& Err.Description & vbCrLf
ret = 1
Else
Msgbox "ldf ok"
End If
' Add new database
objSQLServer.Databases.Add objDatabase
If Err.Number <> 0 Then
If Mydebug Then Msgbox "Initrecipe DB_add database _Error: "& Hex(Err.Number)& vbCrLf &"Descr: "& Err.Description & vbCrLf
ret = 1
End If
' attach database
Set objCommand = CreateObject("ADODB.Command")
With objCommand
.CommandType = 4 'adCmdStoredProc
.ActiveConnection = objConnection
.CommandText = "sp_attach_db"
.Parameters.Refresh
.Parameters("@dbname") = strDatabaseName
.Parameters("@filename1") = strDBFilePath & strDatabaseName & ".mdf"
.Parameters("@filename2") = strDBFilePath & strDatabaseName & ".ldf"
.Execute
End with
Set objFileSystem = Nothing
Set objLogFile = Nothing
Set objDBFile = Nothing
Set objDatabase = Nothing
Set objCommand = Nothing
objSQLServer.Disconnect
Set objSQLServer = Nothing
InitSQLDB = 0
End Function
======================
有没有大神指导下? 同样的脚本,却不同的结果!如果可以我可以提供项目,QQ 527670827