home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / vbsql / vbsqlgen.bas < prev    next >
Encoding:
BASIC Source File  |  1996-04-03  |  12.5 KB  |  488 lines

  1. '$INCLUDE: 'VBQUERY.BI'
  2. '$INCLUDE: 'VBDSQL.BI'
  3.  
  4. Sub ChangePrimaryWindowCaption ()
  5.     PrimaryWindow.Caption = PrimaryWindowTitle + " - " + Servername$ + "/" + DatabaseName$
  6. End Sub
  7.  
  8. Function CheckServerConnection () As Integer
  9.     If SqlConn <> 0 Then
  10.         CheckServerConnection = 1
  11.     Else
  12.         CheckServerConnection = 0
  13.     End If
  14. End Function
  15.  
  16. Sub DoubleQuotes (InString As String)
  17.  
  18. Rem
  19. Rem This will replace all double quotes with ""
  20. Rem and all single quotes with ''
  21. Rem
  22.  
  23. DOUBLEQUOTE$ = Chr$(34) + Chr$(34)
  24. SINGLEQUOTE$ = Chr$(39) + Chr$(39)
  25. Static mychar As String * 1
  26.  
  27. Rem
  28. Rem Get the length of the string coming in
  29. Rem Set the length of TmpString to length of string coming in + 100 new chars
  30. Rem
  31.  
  32. y% = Len(InString)
  33. TmpString$ = Space$(y% + 100)
  34. i% = 1
  35.  
  36. For x% = 1 To y%
  37.  
  38.     mychar$ = Mid$(InString, x%, 1)
  39.     If mychar$ = Chr$(34) Then
  40.     Mid$(TmpString$, i%, 2) = DOUBLEQUOTE$
  41.     i% = i% + 1
  42.     Else
  43.     If mychar$ = Chr$(39) Then
  44.         Mid$(TmpString$, i%, 2) = SINGLEQUOTE$
  45.         i% = i% + 1
  46.     Else
  47.         mychar$ = Mid$(InString, x%, 1)
  48.         Mid$(TmpString$, i%) = mychar$
  49.     End If
  50.     End If
  51.     i% = i% + 1
  52. Next x%
  53.  
  54. InString$ = RTrim$(TmpString$)
  55.  
  56. End Sub
  57.  
  58. Function ExecuteSQLCommand (cmd As String) As Integer
  59.  
  60. Rem
  61. Rem This routine executes a command(s) and returns whether the
  62. Rem execute succeeded or failed.
  63. Rem
  64.  
  65. SQLStatus% = SUCCEED
  66. ExecuteSQLCommand = SUCCEED
  67. If SqlCmd(SqlConn, cmd$) = FAIL% Then
  68.     SQLStatus% = FAIL
  69.     ExecuteSQLCommand = FAIL
  70. End If
  71. If SqlExec(SqlConn) = FAIL% Then
  72.     SQLStatus% = FAIL
  73.     ExecuteSQLCommand = FAIL
  74. End If
  75. End Function
  76.  
  77. Sub FixTextLineFeeds (InString As String)
  78.  
  79. Rem
  80. Rem This will replace all LF characters in the InString with CRLF
  81. Rem
  82.  
  83. CRLF$ = Chr$(13) + Chr$(10)
  84. LF$ = Chr$(10)
  85.  
  86. Rem
  87. Rem Get the length of the string coming in
  88. Rem Set the length of TmpString to length of string coming in + 100 new chars
  89. Rem
  90.  
  91. y% = Len(InString)
  92. TmpString$ = Space$(y% + 100)
  93. i% = 1
  94.  
  95. For x% = 1 To y%
  96.  
  97.     mychar$ = Mid$(InString, x%, 1)
  98.     If mychar$ = LF$ Then
  99.     Mid$(TmpString$, i%, 1) = CRLF$
  100.     i% = i% + 1
  101.     Else
  102.     mychar$ = Mid$(InString, x%, 1)
  103.     Mid$(TmpString$, i%) = mychar$
  104.     End If
  105.     i% = i% + 1
  106. Next x%
  107.  
  108. InString$ = RTrim$(TmpString$)
  109. End Sub
  110.  
  111. Function GetDatabases (Database_Control As Control) As Integer
  112.  
  113. Rem
  114. Rem This routine gets the name of all the databases on the SQL Server.
  115. Rem Fill each element in the combobox or list box which is passed into this procedure
  116. Rem execute the command.  Get each database name and fill the combobox.
  117. Rem
  118.  
  119.     If ExecuteSQLCommand("Select name from master..sysdatabases") = FAIL% Then
  120.     GetDatabases = FAIL
  121.     Exit Function
  122.     Else
  123.     If SqlResults(SqlConn) = FAIL% Then Exit Function
  124.     While SqlNextRow(SqlConn) <> NOMOREROWS%
  125.         Database_Control.AddItem SqlData(SqlConn, 1)
  126.     Wend
  127.     End If
  128.  
  129. Rem If this is a combobox we are filling, then display the first database in the list to start with
  130.  
  131.     If TypeOf Database_Control Is ComboBox Then
  132.     Database_Control.Text = Database_Control.List(0)
  133.     End If
  134.  
  135.     GetDatabases = SUCCEED
  136. End Function
  137.  
  138. Function LoginToServer () As Integer
  139.  
  140. LoginToServer = SUCCEED
  141.  
  142. Rem
  143. Rem Check to see if the connection is live, if so, then close it
  144. Rem Set the max time to login to 30 seconds
  145. Rem Open the new connection
  146. Rem Change the caption of the application to reflect the server name and the database
  147. Rem Set the max time we will wait for a SQL Server response
  148. Rem
  149.  
  150. If SqlConn <> 0 Then SqlClose (SqlConn)
  151. Status% = SqlSetLoginTime%(LoginTimeout%)
  152. SqlConn = SqlOpenConnection(Servername$, LoginID$, password$, ProgramName$, ProgramName$)
  153. If SqlConn <> 0 Then
  154.     DatabaseName$ = SqlName(SqlConn)
  155.     ChangePrimaryWindowCaption
  156.     Result% = SqlSetTime%(QueryTimeout%)
  157. Else
  158.     DatabaseName$ = ""
  159.     Servername$ = ""
  160.     LoginToServer = FAIL
  161. End If
  162.  
  163. End Function
  164.  
  165. Sub Logoff ()
  166.     If SqlConn <> 0 Then
  167.     SqlClose (SqlConn)
  168.     Servername$ = "[No server]"
  169.     DatabaseName$ = "[no database]"
  170.     ChangePrimaryWindowCaption
  171.     End If
  172. End Sub
  173.  
  174. Function MakeRuleList (Rawtext As String) As String
  175.  
  176. Rem
  177. Rem This functions takes a rule of type "IN" from sp_helptext and makes it a
  178. Rem comma delimited list for easy use in list boxes
  179. Rem
  180.  
  181. start% = InStr(1, Rawtext$, "'")
  182. MakeRuleList = Mid$(Rawtext$, start%, Len(Rawtext$) - 2)
  183. End Function
  184.  
  185. Sub ParseRule (Rulename() As String)
  186.  
  187. Rem
  188. Rem This routine takes the comma delimeted rules,which came from the
  189. Rem MakeRuleList procedure, removes the quotes and stores the values
  190. Rem in an array.  This is good for use in combo and list boxes.
  191. Rem
  192.  
  193. in$ = Rulename$(0)
  194.  
  195. start% = 1
  196. For i% = 0 To 100
  197.     endpos% = InStr(start% + 1, in$, "'")
  198.     Rulename(i%) = Mid$(in$, start% + 1, (endpos% - start%) - 1)
  199.     start% = InStr(endpos% + 1, in$, "'")
  200.     If start% = 0 Then Exit For
  201. Next i%
  202.  
  203. End Sub
  204.  
  205. Function Process_SQL_query (cmd As String, OutputData() As String) As Long
  206.  
  207. Rem
  208. Rem This routine will process query rows and output the total number
  209. Rem of rows which reflects the number of items in the output array.
  210. Rem
  211. Rem Define array for column lengths, column positions, and column types
  212. Rem Define structures for getting a compute column's information and getting
  213. Rem a regular column's information
  214. Rem
  215.  
  216. Rem Declare a local error handler for string overflows
  217. On Error GoTo CancelQuery
  218.  
  219. Static ColValue$
  220. Static collengths() As Long
  221. ReDim Preserve collengths(255) As Long
  222. Static colpositions() As Integer
  223. ReDim Preserve colpositions(255) As Integer
  224. Static Coltypes() As Integer
  225. ReDim Preserve Coltypes(50) As Integer
  226.  
  227. Process_SQL_query = 0
  228.  
  229. Rem
  230. Rem Define the new line character and the tab key
  231. Rem Get the command from the QUERY_FIELD.
  232. Rem Fill the command buffer. If fail, then exit the subroutine.
  233. Rem Execute the command
  234. Rem
  235.  
  236. NL$ = Chr$(13) + Chr$(10)
  237. COLSEP$ = " "
  238.  
  239. If cmd$ <> "" Then
  240.     If ExecuteSQLCommand(cmd$) = FAIL% Then Exit Function
  241. End If
  242.  
  243. outputrowcnt% = 0
  244.  
  245. Rem
  246. Rem Get each set of results
  247. Rem Get the number of compute columns, order by columns, and select columns
  248. Rem Get the exact position of each column (for lining up compute columns)
  249. Rem
  250.  
  251. Do Until ResultProcess% = NOMORERESULTS%
  252.     ResultProcess% = SqlResults(SqlConn)
  253.     If ResultProcess% = NOMORERESULTS% Or ResultProcess% = FAIL Then Exit Do
  254.  
  255.     numcol% = SqlNumCols%(SqlConn)
  256.     If numcol% > 0 Then
  257.     numorder% = SqlNumOrders%(SqlConn)
  258.     colline$ = ""
  259.     coluline$ = ""
  260.  
  261. Rem
  262. Rem Get the column name and length for each column
  263. Rem Format and output the column headings (max 256 chars wide).
  264. Rem
  265.  
  266.     For x% = 1 To numcol%
  267.     colname$ = SqlColName(SqlConn, x%)
  268.     Coltypes(x%) = SqlColType(SqlConn, x%)
  269.     collengths(x%) = SqlColLen(SqlConn, x%)
  270.     
  271.     ' templen holds length of column data.  truncate text and image
  272.     tmplen% = collengths(x%)
  273.     If tmplen% > 255 Then tmplen% = 255
  274.     
  275.     actuallen& = Len(colname$)
  276.     
  277.     If x% = 1 Then
  278.        colpositions(x%) = 1
  279.     Else
  280.         colpositions(x%) = Len(colline$) + Len(COLSEP$)
  281.     End If
  282.      
  283.     If actuallen& < tmplen% Then
  284.         colline$ = colline$ + colname$ + Space$((tmplen% - actuallen&) + 1) + COLSEP$
  285.         coluline$ = coluline$ + String$(Len(colname$), "_") + Space$((tmplen% - actuallen&) + 1) + COLSEP$
  286.     Else
  287.         colline$ = colline$ + colname$ + COLSEP$
  288.         coluline$ = coluline$ + String$(Len(colname$), "_") + COLSEP$
  289.     End If
  290.     
  291.     Next x%
  292.  
  293.     
  294.     OutputData(outputrowcnt%) = colline$
  295.     outputrowcnt% = outputrowcnt% + 1
  296.     OutputData(outputrowcnt%) = coluline$
  297.     outputrowcnt% = outputrowcnt% + 1
  298.     OutputData(outputrowcnt%) = " "
  299.     outputrowcnt% = outputrowcnt% + 1
  300.  
  301.     End If   'end of numcol% > 0 test
  302.  
  303.  
  304. Rem
  305. Rem Get each row of data, and process according to type of row
  306. Rem Output each row into the list box
  307. Rem
  308.  
  309.     RowProcess% = 99
  310.     Do Until RowProcess% = NOMOREROWS%
  311.     DataStr$ = ""
  312.     Result% = SqlNextRow(SqlConn)
  313.     If Result% = NOMOREROWS% Or Result% = FAIL Then Exit Do
  314.  
  315. Rem
  316. Rem Process a COMPUTE Row  (Available in VB Win only).
  317. Rem In DOS, this function pops up a message box saying COMPUTE rows are not supported.
  318. Rem
  319.  
  320.     If Result% <> REGROW Then
  321.     Process_altrows Result%, OutputData(), outputrowcnt%, colpositions()
  322.     Else
  323.  
  324. Rem
  325. Rem Process a regular row.
  326. Rem Get the column value and length.
  327. Rem If it is a Text column, then change the LF to CRLF if they exist
  328. Rem Align columns even with the column headings.
  329. Rem
  330.     For x% = 1 To numcol%
  331.         ColValue$ = SqlData(SqlConn, x%)
  332.         actuallen& = Len(ColValue$)
  333.         If actuallen& > 255 Then
  334.         ColValue$ = Left$(ColValue$, 255)
  335.         actuallen& = 255
  336.         End If
  337.               
  338.         If Coltypes(x%) = SQLTEXT% Then
  339.         FixTextLineFeeds ColValue$
  340.         End If
  341.  
  342.         If x% <> numcol% Then
  343.         DataStr$ = DataStr$ + ColValue$ + Space$(colpositions(x% + 1) - colpositions(x%) - actuallen&)
  344.         Else
  345.         DataStr$ = DataStr$ + ColValue$
  346.         End If
  347.  
  348.         ColValue$ = ""
  349.     Next x%
  350.  
  351.     OutputData(outputrowcnt%) = DataStr$
  352.     End If
  353.     outputrowcnt% = outputrowcnt% + 1
  354.  
  355.     Loop        'End of row loop
  356.  
  357. Rem
  358. Rem Output the number of rows affected by the query (if applicable)
  359. Rem Output the sort order (if applicable)
  360. Rem
  361.  
  362.     rowcnt& = SqlCount(SqlConn)
  363.     If SqlIsCount(SqlConn) Then
  364.     DataStr$ = "(" + Str$(rowcnt&) + " rows affected)"
  365.     OutputData(outputrowcnt%) = " "
  366.     OutputData(outputrowcnt% + 1) = DataStr$
  367.     outputrowcnt% = outputrowcnt% + 2
  368.     End If
  369.  
  370.     If numorder% > 0 Then
  371.     OutputData(outputrowcnt%) = " "
  372.     DataStr$ = "Sort Order: "
  373.     For y% = 1 To numorder%
  374.         ordercol$ = SqlColName(SqlConn, SqlOrderCol(SqlConn, y%))
  375.         DataStr$ = DataStr$ + " " + ordercol$
  376.     Next y%
  377.     OutputData(outputrowcnt% + 1) = DataStr$
  378.     outputrowcnt% = outputrowcnt% + 2
  379.     End If
  380.  
  381. Loop        'End of result loop
  382.  
  383. Rem
  384. Rem Check for return parameters and return status from stored procedures at the end
  385. Rem of every result set.  Available in VBWin only.
  386. Rem
  387.  
  388. Process_rpc_returns OutputData(), outputrowcnt%
  389. Process_SQL_query = outputrowcnt%
  390.  
  391. Exit Function
  392.  
  393. CancelQuery:
  394.     Result% = SqlCancel%(SqlConn)
  395.     Msg$ = "Error number " + Str$(Err) + ":  " + Error$ + NL$
  396.     Msg$ = Msg$ + "Query Cancelled" + NL$
  397.     MsgBox Msg$, MB_ICONEXCLAMATION, "Visual Basic Error"
  398.     Exit Function
  399.  
  400. End Function
  401.  
  402. Function UserSqlErrorHandler% (SqlConn As Integer, Severity As Integer, ErrorNum As Integer, OsErr As Integer, ErrorStr As String, OsErrStr As String)
  403. 'UserSqlErrorHander% - This function is REQUIRED for all VBDSQL applications.  It
  404. 'is called by the VB-DOS interface code for DB-LIBRARY whenever a
  405. 'DB-LIBRARY error occurs.  In VB-Win, it can be called from the error event handler.
  406.  
  407. 'This function can do anything EXCEPT call another
  408. 'DB-LIBRARY function (with the exception of SqlDead%, which you can
  409. 'call to determine if the connection is still intact).
  410. '
  411. 'You can return 1 of 3 values:
  412. ' INTEXIT     - exit the program
  413. ' INTCANCEL   - cancel the operation
  414. ' INTCONTINUE - continue the operation (can only continue on timeout read
  415. '                    errors, which usually occur if a table that is locked
  416. '                    is updated or read)
  417. '
  418.  
  419. Rem
  420. Rem Only display message if it's not a notification that there's a server error
  421. Rem
  422.  
  423.     If ErrorNum% <> SQLESMSG% Then
  424.     MsgBox ("DBLibrary Error: " + Str$(ErrorNum%) + " " + ErrorStr$)
  425.     End If
  426.  
  427.  
  428.  
  429.     'If an operating-system error occurred, print the error string.
  430.      If OsErr% <> -1 Then
  431.         MsgBox ("Operating-System Error: " + OsErrStr$)
  432.      End If
  433.  
  434.     'Exit if the error is fatal.
  435.     If Severity% = EXFATAL Then
  436.         UserSqlErrorHandler% = INTEXIT
  437.     Else
  438.         UserSqlErrorHandler% = INTCANCEL
  439.     End If
  440.  
  441.  
  442. End Function
  443.  
  444. Sub UserSqlMsgHandler (SqlConn As Integer, Message As Long, State As Integer, Severity As Integer, MsgStr As String)
  445. 'UserSqlMsgHandler - This procedure is REQUIRED for VBDSQL applicaitons.
  446. 'In VB-DOS, it is called by BASIC DB-LIBRARY whenever a connected server needs to
  447. 'issue a message to the client.  You can call it from the message handler event
  448. 'in VB-Win
  449.  
  450. NL$ = Chr$(13) + Chr$(10)
  451.  
  452. Rem
  453. Rem Only display the message if it's not a general msg or a change language message
  454. Rem
  455.  
  456.     If Message& <> 5701 And Message& <> 5703 Then
  457.     Msg$ = "SQL Server Error: " + Str$(Message&) + " " + MsgStr$ + NL$
  458.     Msg$ = Msg$ + "State=" + Str$(State%) + ", Severity=" + Str$(Severity)
  459.  
  460.     MsgBox Msg$
  461.     End If
  462.     
  463.  
  464. End Sub
  465.  
  466. Function WarningMessage (MsgStr As String) As Integer
  467.  
  468. Rem
  469. Rem This routine displays a warning message with a YES and NO button
  470. Rem and returns the result.
  471. Rem
  472.  
  473. Const MB_YESNO = 4
  474. Const MB_ICONEXLAMATION = 48
  475. Const IDYES = 6
  476. Const IDNO = 7
  477. Const DEFBUTTON2 = 256
  478.  
  479. DgDef% = MB_YESNO + MB_ICONEXCLAMATION + MB_DEFBUTTON2
  480. Response% = MsgBox(MsgStr$, DgDef%, "System Warning")
  481. If Response% = IDNO Then
  482.     WarningMessage = 0
  483. Else
  484.     WarningMessage = 1
  485. End If
  486. End Function
  487.  
  488.