WINCC V7 创建SQL数据库 7.4OK, 7.3失败 //缺少对象

已锁定

MikeLyu

  • 帖子

    104
  • 精华

    0
  • 被关注

    6

论坛等级:侠客

注册时间:2011-11-18

黄金 黄金 如何晋级?

WINCC V7 创建SQL数据库 7.4OK, 7.3失败 //缺少对象

1680

6

2018-09-21 10:44:47

背景:想要通过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

WINCC V7 创建SQL数据库 7.4OK, 7.3失败 //缺少对象 已锁定
编辑推荐: 关闭

请填写推广理由:

本版热门话题

SIMATIC WinCC / Panel

共有32775条技术帖

相关推荐

热门标签

相关帖子推荐

guzhang

恭喜,你发布的帖子

评为精华帖!

快扫描右侧二维码晒一晒吧!

再发帖或跟帖交流2条,就能晋升VIP啦!开启更多专属权限!

  • 分享

  • 只看
    楼主

top
X 图片
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。