Guest User

Untitled

a guest
Mar 12th, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. dim dbType, dbHost, dbName, dbUser, dbPass, outputFile, email, subj, body, smtp, smtpPort, sqlstr
  2.  
  3. '''''''''''''''''
  4. ' Configuration '
  5. '''''''''''''''''
  6. dbType = "oracle" ' Valid values: "oracle", "sqlserver", "mysql"
  7. dbHost = "dbhost" ' Hostname of the database server
  8. dbName = "dbname" ' Name of the database/SID
  9. dbUser = "username" ' Name of the user
  10. dbPass = "password" ' Password of the above-named user
  11. outputFile = "c:output.csv" ' Path and file name of the output CSV file
  12. email = "email@me.here" ' Enter email here should you wish to email the CSV file (as attachment); if no email, leave it as empty string ""
  13. subj = "Email Subject" ' The subject of your email; required only if you send the CSV over email
  14. body = "Put a message here!" ' The body of your email; required only if you send the CSV over email
  15. smtp = "mail.server.com" ' Name of your SMTP server; required only if you send the CSV over email
  16. smtpPort = 25 ' SMTP port used by your server, usually 25; required only if you send the CSV over email
  17. sqlStr = "select user from dual" ' SQL statement you wish to execute
  18. '''''''''''''''''''''
  19. ' End Configuration '
  20. '''''''''''''''''''''
  21.  
  22.  
  23.  
  24. dim fso, conn
  25.  
  26. 'Create filesystem object
  27. set fso = CreateObject("Scripting.FileSystemObject")
  28.  
  29. 'Database connection info
  30. set Conn = CreateObject("ADODB.connection")
  31. Conn.ConnectionTimeout = 30
  32. Conn.CommandTimeout = 30
  33. if dbType = "oracle" then
  34. conn.open("Provider=MSDAORA.1;User ID=" & dbUser & ";Password=" & dbPass & ";Data Source=" & dbName & ";Persist Security Info=False")
  35. elseif dbType = "sqlserver" then
  36. conn.open("Driver={SQL Server};Server=" & dbHost & ";Database=" & dbName & ";Uid=" & dbUser & ";Pwd=" & dbPass & ";")
  37. elseif dbType = "mysql" then
  38. conn.open("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=" & dbHost & ";PORT=3306;DATABASE=" & dbName & "; UID=" & dbUser & "; PASSWORD=" & dbPass & "; OPTION=3")
  39. end if
  40.  
  41. ' Subprocedure to generate data. Two parameters:
  42. ' 1. fPath=where to create the file
  43. ' 2. sqlstr=the database query
  44. sub MakeDataFile(fPath, sqlstr)
  45. dim a, showList, intcount
  46. set a = fso.createtextfile(fPath)
  47.  
  48. set showList = conn.execute(sqlstr)
  49. for intcount = 0 to showList.fields.count -1
  50. if intcount <> showList.fields.count-1 then
  51. a.write """" & showList.fields(intcount).name & ""","
  52. else
  53. a.write """" & showList.fields(intcount).name & """"
  54. end if
  55. next
  56. a.writeline ""
  57.  
  58. do while not showList.eof
  59. for intcount = 0 to showList.fields.count - 1
  60. if intcount <> showList.fields.count - 1 then
  61. a.write """" & showList.fields(intcount).value & ""","
  62. else
  63. a.write """" & showList.fields(intcount).value & """"
  64. end if
  65. next
  66. a.writeline ""
  67. showList.movenext
  68. loop
  69. showList.close
  70. set showList = nothing
  71.  
  72. set a = nothing
  73. end sub
  74.  
  75. ' Call the subprocedure
  76. call MakeDataFile(outputFile,sqlstr)
  77.  
  78. ' Close
  79. set fso = nothing
  80. conn.close
  81. set conn = nothing
  82.  
  83. if email <> "" then
  84. dim objMessage
  85. Set objMessage = CreateObject("CDO.Message")
  86. objMessage.Subject = "Test Email from vbs"
  87. objMessage.From = email
  88. objMessage.To = email
  89. objMessage.TextBody = "Please see attached file."
  90. objMessage.AddAttachment outputFile
  91.  
  92. objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  93. objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtp
  94. objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = smtpPort
  95.  
  96. objMessage.Configuration.Fields.Update
  97.  
  98. objMessage.Send
  99. end if
Add Comment
Please, Sign In to add comment