设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 320|回复: 1

Exlent error handling for vbs and database accessing

[复制链接]
发表于 2013-5-24 09:00:41 | 显示全部楼层 |阅读模式
本帖最后由 Test 于 2013-5-24 09:01 编辑

uses the resume next and emails out the name of the file that has an error. But at the moment if 1 of the 10 rows in the file are fine it will load that 1 row and still send the email.
  1. Function Main()
  2.         Const adOpenForwardOnly = 0
  3.         Const adLockReadOnly = 1
  4.         Const adCmdText = &H0001

  5.         dim strSqlConnection
  6.         dim dbSqlConnect
  7.         dim strSQL
  8.         dim objSqlCmd
  9.         dim dbXlConnect
  10.         dim strXlSQL
  11.         dim rstXlResults
  12.         dim arrXlData
  13.         dim objFSO
  14.         dim objStartFolder
  15.         dim objFolder
  16.         dim objFile


  17.         objStartFolder = "F:\Metastorm BPM\MDS\Output"
  18.         strXlSQL = "SELECT * FROM [Sheet1$A3:T65000]"

  19.         Set strSqlConnection = CreateObject("ADODB.Connection")

  20.         strSqlConnection.Provider = "sqloledb"
  21.         strSqlConnection.Properties("Data Source").Value = "gf"
  22.         strSqlConnection.Properties("Initial Catalog").Value = "e-df"
  23.         strSqlConnection.Properties("User ID").Value = "df"
  24.         strSqlConnection.Properties("Password").Value = "dfs"
  25.         strSqlConnection.Properties("Integrated Security").Value = "dsf"

  26.         Set objFSO = CreateObject("Scripting.FileSystemObject")

  27.         If objFSO.FolderExists(objStartFolder) Then
  28.                 On Error Resume Next               
  29.                 Set objFolder = objFSO.GetFolder(objStartFolder)
  30.                
  31.                 For each objFile In objFolder.Files
  32.                         If objFSO.GetExtensionName(objFile) = "xls" Then
  33.                                 Set dbXlConnect = CreateObject("ADODB.Connection")
  34.                                 dbXlConnect.Open = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & objFile.Path & ";Extended Properties=""Excel 8.0;HDR=Yes;"";" ' Excel Object connection properties
  35.                                 
  36.                                 'Check if the excel file was opened
  37.                                 If err.number <> 0 Then
  38.                                         SendMail "Openning Excel file = " & objFile.Path, err.Description
  39.                                         DTSTaskExecResult_Failure
  40.                                         Exit Function
  41.                                 End If
  42.                                 
  43.                                 Set rstXlResults = CreateObject("ADODB.Recordset")
  44.                                 rstXlResults.Open strXlSQL, dbXlConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
  45.                                 'Check if recordset was able to be created from the ex file
  46.                                 If err.number <> 0 Then
  47.                                         SendMail "Error creating recordset", err.Description
  48.                                         DTSTaskExecResult_Failure
  49.                                         Exit Function
  50.                                 End If

  51.                                 If Not (rstXlResults.bof and rstXlResults.eof) then
  52.                                         arrXlData = rstXlResults.GetRows
  53.                                         rstXlResults.Close
  54.                                         'Check if there was a problem creating an array from the recordset row
  55.                                         If err.number <> 0 Then
  56.                                                 SendMail "Unable to create array from recordset row", err.Description
  57.                                                 DTSTaskExecResult_Failure
  58.                                                 Exit Function
  59.                                         End If
  60.                                        
  61.                                         Set rstXlResults = Nothing

  62.                                         dbXlConnect.Close
  63.                                         Set dbXlConnect = Nothing

  64.                                         if isarray(arrXlData) then

  65.                                                 Set dbSqlConnect = CreateObject("ADODB.Connection")
  66.                                                 dbSqlConnect.Open strSqlConnection
  67.                                                 'Check if there was a problem opening the connection
  68.                                                 If err.number <> 0 Then
  69.                                                         SendMail "Unable to open the dbSqlConnect connection", err.Description
  70.                                                         DTSTaskExecResult_Failure
  71.                                                         Exit Function
  72.                                                 End If

  73.                                                 for x=0 to ubound(arrXlData,2) ' Loop data and insert into db
  74.                                                         ' Destination table and array
  75.                                                         strSQL = "INSERT INTO dbo.MDS_TEMP_Staging VALUES ('" & arrXlData(0,x) & "','" & arrXlData(1,x) & "','" & arrXlData(2,x) & "', '" & arrXlData(3,x) & "','" & arrXlData(4,x) & "', '" & arrXlData(5,x) &
  76.                                                         "','" & arrXlData(6,x) & "' , '" & arrXlData(7,x) & "','" & arrXlData(8,x) & "','" & arrXlData(9,x) & "', '" & arrXlData(10,x) & "','" & arrXlData(11,x) & "', '" & arrXlData(12,x) & "','" & arrXlData(13,x) & "' ,'" &
  77.                                                         arrXlData(14,x) & "','" & arrXlData(15,x) & "','" & arrXlData(16,x) & "', '" & arrXlData(17,x) & "', '" & arrXlData(18,x) & "', '" & arrXlData(19,x) & "')"

  78.                                                         'Set objSqlCmd = CreateObject("ADODB.Command")
  79.                                                         'objSqlCmd.ActiveConnection = dbSqlConnect
  80.                                                         'objSqlCmd.CommandType = adCmdText
  81.                                                         'objSqlCmd.CommandText = strSQL

  82.                                                         'objSqlCmd.Execute
  83.                                                         'Set objSqlCmd = Nothing
  84.                                                         dbSqlConnect.Execute strSQL
  85.                                                         'Check if there was a problem doing the insert
  86.                                                         If err.number <> 0 Then
  87.                                                                 SendMail "Insert error", err.Description
  88.                                                                 DTSTaskExecResult_Failure
  89.                                                                 Exit Function
  90.                                                         End If
  91.                                                 next

  92.                                                 set dbSqlConnect = nothing
  93.                                                 set arrXlData = nothing
  94.                                         end if
  95.                                 End If
  96.                         End If
  97.                 Next
  98.         End If

  99.         set objFSO = Nothing

  100.         Main = DTSTaskExecResult_Success

  101. End Function

  102. Sub SendMail(ByVal eSource, ByVal eDescription)
  103. Dim iMsg, strBody

  104.         'Use COM to create Message and Configuration Objects
  105.         Set iMsg = CreateObject("CDO.Message")
  106.         
  107.         strBody = "Error Source: " & eSource & "
  108. " & "Error Description: " & eDescription

  109.         ' Apply the settings to the message.
  110.         With iMsg
  111.                 .To = "dg.df@df-df.co.uk"
  112.                 'If Not IsNull(strCC) Then .CC = strCC
  113.                 .From = "df@ff-df.co.uk"
  114.                 .Subject = "MONTHLY load has failed" & objFile & " "
  115.                 .HTMLBody = strBody

  116.                 .Send
  117.         End With

  118.         ' Clean up variables.
  119.         Set iMsg = Nothing
  120. End Sub
复制代码
 楼主| 发表于 2013-5-24 09:10:15 | 显示全部楼层
本帖最后由 Test 于 2013-5-24 09:12 编辑

If you do not want loading any data from a file where there is an issue with 1 or more rows of data in that file.
e.g. for the code above, if row 3 has an issue, it will load 1,2 send the error email and then continue loading the rest of the rows. How about if I want it to fail to load all together if there is an error before any data is loaded.

Add a transaction to the connection object would give out this need.

Also, moved all the insert code into its own procedure so you should be able to continue on with the next file if the current file fails.
  1. Const adOpenForwardOnly = 0
  2. Const adLockReadOnly = 1
  3. Const adCmdText = &H0001

  4. Function Main()
  5.         dim strSqlConnection         
  6.         dim objFSO
  7.         dim objStartFolder
  8.         dim objFolder
  9.         dim objFile
  10.         
  11.         objStartFolder = "F:\Metastorm BPM\MDS\Output"
  12.         

  13.         Set strSqlConnection = CreateObject("ADODB.Connection")

  14.         strSqlConnection.Provider = "sqloledb"
  15.         strSqlConnection.Properties("Data Source").Value = "gf"
  16.         strSqlConnection.Properties("Initial Catalog").Value = "e-df"
  17.         strSqlConnection.Properties("User ID").Value = "df"
  18.         strSqlConnection.Properties("Password").Value = "dfs"
  19.         strSqlConnection.Properties("Integrated Security").Value = "dsf"

  20.         Set objFSO = CreateObject("Scripting.FileSystemObject")

  21.         If objFSO.FolderExists(objStartFolder) Then
  22.                 On Error Resume Next               
  23.                 Set objFolder = objFSO.GetFolder(objStartFolder)
  24.                
  25.                 For each objFile In objFolder.Files
  26.                         If objFSO.GetExtensionName(objFile) = "xls" Then
  27.                                 InsertData objFile, strSqlConnection
  28.                         End If
  29.                 Next
  30.         End If

  31.         set objFSO = Nothing

  32.         Main = DTSTaskExecResult_Success

  33. End Function

  34. Sub InsertData(oFile, oConn)
  35. Dim dbXlConnect
  36. Dim rstXlResults
  37. Dim strXlSQL
  38. Dim arrXlData
  39. Dim dbSqlConnect
  40. Dim strSQL

  41.         strXlSQL = "SELECT * FROM [Sheet1$A3:T65000]"

  42.         On Error Resume Next
  43.         
  44.         Set dbXlConnect = CreateObject("ADODB.Connection")
  45.         dbXlConnect.Open = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & oFile.Path & ";Extended Properties=""Excel 8.0;HDR=Yes;"";" ' Excel Object connection properties
  46.         
  47.         'Check if the excel file was opened
  48.         If err.number <> 0 Then
  49.                 SendMail "Openning Excel file = " & oFile.Path, err.Description
  50.                 Exit Sub
  51.         End If
  52.         
  53.         Set rstXlResults = CreateObject("ADODB.Recordset")
  54.         rstXlResults.Open strXlSQL, dbXlConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
  55.         
  56.         'Check if recordset was able to be created from the ex file
  57.         If err.number <> 0 Then
  58.                 SendMail "Error creating recordset", err.Description
  59.                 Exit Sub
  60.         End If

  61.         If Not (rstXlResults.bof and rstXlResults.eof) then
  62.                 arrXlData = rstXlResults.GetRows
  63.                 rstXlResults.Close
  64.                 'Check if there was a problem creating an array from the recordset row
  65.                 If err.number <> 0 Then
  66.                         SendMail "Unable to create array from recordset row", err.Description
  67.                         Exit Sub
  68.                 End If
  69.                
  70.                 Set rstXlResults = Nothing

  71.                 dbXlConnect.Close
  72.                 Set dbXlConnect = Nothing

  73.                 if isarray(arrXlData) then

  74.                         Set dbSqlConnect = CreateObject("ADODB.Connection")
  75.                         dbSqlConnect.Open oConn
  76.                         'Check if there was a problem opening the connection
  77.                         If err.number <> 0 Then
  78.                                 SendMail "Unable to open the dbSqlConnect connection", err.Description
  79.                                 Exit Sub
  80.                         End If
  81.                         
  82.                         dbSqlConnect.BeginTrans
  83.                         
  84.                         for x=0 to ubound(arrXlData,2) ' Loop data and insert into db
  85.                                 ' Destination table and array
  86.                                 strSQL = "INSERT INTO dbo.MDS_TEMP_Staging VALUES ('" & arrXlData(0,x) & "','" & arrXlData(1,x) & "','" & arrXlData(2,x) & "', '" & arrXlData(3,x) & "','" & arrXlData(4,x) & "', '" & arrXlData(5,x) &
  87.                                 "','" & arrXlData(6,x) & "' , '" & arrXlData(7,x) & "','" & arrXlData(8,x) & "','" & arrXlData(9,x) & "', '" & arrXlData(10,x) & "','" & arrXlData(11,x) & "', '" & arrXlData(12,x) & "','" & arrXlData(13,x) & "' ,'" &
  88.                                 arrXlData(14,x) & "','" & arrXlData(15,x) & "','" & arrXlData(16,x) & "', '" & arrXlData(17,x) & "', '" & arrXlData(18,x) & "', '" & arrXlData(19,x) & "')"

  89.                                 dbSqlConnect.Execute strSQL
  90.                                 'Check if there was a problem doing the insert
  91.                                 If err.number <> 0 Then
  92.                                         dbSqlConnect.RollbackTrans
  93.                                         SendMail "Insert error" & oFile.Path, err.Description
  94.                                         Exit Sub
  95.                                 End If
  96.                         next
  97.                         
  98.                         dbSqlConnect.CommitTrans
  99.                         
  100.                         set dbSqlConnect = nothing
  101.                         set arrXlData = nothing
  102.                 end if
  103.         End If
  104. End Sub

  105. Sub SendMail(ByVal eSource, ByVal eDescription)
  106. Dim iMsg, strBody

  107.         'Use COM to create Message and Configuration Objects
  108.         Set iMsg = CreateObject("CDO.Message")
  109.         
  110.         strBody = "Error Source: " & eSource & "
  111. " & "Error Description: " & eDescription

  112.         ' Apply the settings to the message.
  113.         With iMsg
  114.                 .To = "dg.df@df-df.co.uk"
  115.                 'If Not IsNull(strCC) Then .CC = strCC
  116.                 .From = "df@ff-df.co.uk"
  117.                 .Subject = "MONTHLY load has failed" & objFile & " "
  118.                 .HTMLBody = strBody

  119.                 .Send
  120.         End With

  121.         ' Clean up variables.
  122.         Set iMsg = Nothing
  123. End Sub
复制代码
Good programmer MarkT
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-26 04:49 , Processed in 0.017002 second(s), 17 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表