home *** CD-ROM | disk | FTP | other *** search
/ Software Collection (I) / TOOLS.iso / b18 / 1.img / EXAMPLE3.BA@ / EXAMPLE3.bin
Encoding:
Text File  |  1992-09-15  |  6.0 KB  |  192 lines

  1.  
  2. 'EXAMPLE3.BAS -- This example creates a new database, then inserts
  3. 'data from a data file into a new table containing several datatypes.
  4. 'The program then updates the table using browse-mode techniques.
  5.  
  6. 'To run this example, you must have the file DATAFILE.TXT in the
  7. 'current directory.  You must also have CREATE DATABASE permission in
  8. 'your login database and 2mb of free space on the default drive.
  9.  
  10. 'Copyright (c) 1990 by Microsoft Corp.  All rights reserved.
  11.  
  12.  
  13. '$INCLUDE: 'sql.bi'
  14.  
  15. MemLeft& = SETMEM(-16384)   'Reserve 16K of heap.
  16. STACK 8192                  'Reserve 8K of stack space.
  17.  
  18. 'Initialize BASIC DB-LIBRARY.
  19. IF SqlInit$ = "" THEN
  20.     PRINT "Can't start BASIC DB-LIBRARY."
  21.     END
  22. END IF
  23.  
  24. 'Get a login record and fill it in with the necessary information.
  25. Login& = SqlLogin&
  26. Result% = SqlSetLUser%(Login&, "loginid")
  27. Result% = SqlSetLPwd%(Login&, "passwd")
  28. Result% = SqlSetLApp%(Login&, "example")
  29.  
  30. 'Get two connections for communicating with SQL Server; one to query
  31. 'the database, one to simultaneously update the database.
  32. SqlConn& = SqlOpen&(Login&, "server")
  33. UpdSqlConn& = SqlOpen&(Login&, "server")
  34. IF SqlConn& = 0 OR UpdSqlConn& = 0 THEN END
  35.  
  36. PRINT : PRINT "Creating the 'test' database."
  37. Result% = SqlCmd%(SqlConn&, "CREATE DATABASE test")
  38. Result% = SqlExec%(SqlConn&)
  39. Result% = SqlResults%(SqlConn&)
  40. Result% = SqlUse%(SqlConn&, "test")
  41. Result% = SqlUse%(UpdSqlConn&, "test")
  42.  
  43. PRINT : PRINT "Creating the 'alltypes' table."
  44. cmd$ = "CREATE TABLE alltypes"
  45. cmd$ = cmd$ + " ( age tinyint, userid smallint, royalty int,"
  46. cmd$ = cmd$ + "   name char(25), title_id varbinary(20),"
  47. cmd$ = cmd$ + "   citizen bit, account float, title varchar(20),"
  48. cmd$ = cmd$ + "   manager char(25), timestamp )"
  49. cmd$ = cmd$ + " CREATE UNIQUE INDEX myindex ON alltypes(userid)"
  50. Result% = SqlCmd%(SqlConn&, cmd$)
  51. Result% = SqlExec%(SqlConn&)
  52. DO UNTIL SqlResults%(SqlConn&) = NO.MORE.RESULTS
  53. LOOP
  54.  
  55. 'Open data file for sequential input.
  56. OPEN "datafile.txt" FOR INPUT AS #1
  57.  
  58. PRINT : PRINT "Inserting rows into the 'alltypes' table."
  59. DO UNTIL EOF(1)
  60.     LINE INPUT #1, Values$
  61.     cmd$ = " INSERT INTO alltypes VALUES(" + Values$ + ", null)"
  62.     Result% = SqlCmd%(SqlConn&, cmd$)
  63. LOOP
  64. Result% = SqlExec%(SqlConn&)
  65.  
  66. 'Process the results of each INSERT statement.
  67. Result% = SqlResults%(SqlConn&)
  68. DO UNTIL Result% = NO.MORE.RESULTS
  69.     IF Result% = FAIL THEN PRINT "One of the INSERT statements failed."
  70.     Result% = SqlResults%(SqlConn&)
  71. LOOP
  72.  
  73. 'Print the contents of the new table.
  74. PRINT : PRINT "Selecting rows from the 'alltypes' table:"
  75. Result% = SqlCmd%(SqlConn&, "SELECT age, name FROM alltypes")
  76. Result% = SqlExec%(SqlConn&)
  77. Result% = SqlResults%(SqlConn&)
  78. Result% = SqlPrRow%(SqlConn&)
  79.  
  80. PRINT : PRINT "Updating rows (incrementing age of each person)."
  81. Result% = SqlCmd%(SqlConn&, "SELECT * FROM alltypes FOR BROWSE")
  82. Result% = SqlExec%(SqlConn&)
  83. Result% = SqlResults%(SqlConn&)
  84. DO UNTIL Result% = NO.MORE.RESULTS
  85.     IF Result% = SUCCEED THEN
  86.         DO UNTIL SqlNextRow%(SqlConn&) = NO.MORE.ROWS
  87.  
  88.             'Increment the age of each person in the table, using the
  89.             'WHERE clause returned by SqlQual$ to ensure that we are
  90.             'not overwriting another user's changes.
  91.             Age% = VAL(SqlData$(SqlConn&, 1))
  92.             Qual$ = SqlQual$(SqlConn&, -1, "alltypes")
  93.             cmd$ = "UPDATE alltypes SET age = " + STR$(Age% + 1)
  94.             cmd$ = cmd$ + " " + Qual$
  95.             Result% = SqlCmd%(UpdSqlConn&, cmd$)
  96.             Result% = SqlExec%(UpdSqlConn&)
  97.             Result% = SqlResults%(UpdSqlConn&)
  98.  
  99.         LOOP
  100.     END IF
  101.     Result% = SqlResults%(SqlConn&)
  102. LOOP
  103.  
  104. PRINT : PRINT "Selecting rows from the updated 'alltypes' table:"
  105. Result% = SqlCmd%(SqlConn&, "SELECT age, name FROM alltypes")
  106. Result% = SqlExec%(SqlConn&)
  107. Result% = SqlResults%(SqlConn&)
  108. Result% = SqlPrRow%(SqlConn&)
  109.  
  110. PRINT : PRINT "Removing the 'test' database."
  111. Result% = SqlUse%(SqlConn&, "master")
  112. Result% = SqlUse%(UpdSqlConn&, "master")
  113. Result% = SqlCmd%(SqlConn&, "DROP DATABASE test")
  114. Result% = SqlExec%(SqlConn&)
  115. Result% = SqlResults%(SqlConn&)
  116.  
  117. 'Close connection and exit program.
  118. CALL SqlExit
  119.  
  120. END
  121.  
  122.  
  123. 'Sample Output
  124. '
  125. 'Msg No.: 5701  Severity:  2  Level:  0
  126. 'Message: Changed database context from 'master' to 'master'.
  127. '
  128. 'Msg No.: 5701  Severity:  2  Level:  0
  129. 'Message: Changed database context from 'master' to 'master'.
  130. '
  131. 'Creating the 'test' database.
  132. '
  133. 'Msg No.: 1805  Severity:  2  Level:  0
  134. 'Message: CREATE DATABASE: allocating 1024 pages on disk 'server'
  135. '
  136. 'Msg No.: 5701  Severity:  1  Level:  0
  137. 'Message: Changed database context from 'master' to 'test'.
  138. '
  139. 'Msg No.: 5701  Severity:  1  Level:  0
  140. 'Message: Changed database context from 'master' to 'test'.
  141. '
  142. 'Creating the 'alltypes' table.
  143. '
  144. 'Inserting rows into the 'alltypes' table.
  145. '
  146. 'Selecting rows from the 'alltypes' table:
  147. '  23 Edgar Allen Poe
  148. '  75 Charles Dickens
  149. '  99 Sherlock Holmes
  150. '  35 Bob Crachit
  151. '  45 Archie Moore
  152. '
  153. 'Updating rows (incrementing age of each person).
  154. '
  155. 'Selecting rows from the updated 'alltypes' table:
  156. '  24 Edgar Allen Poe
  157. '  76 Charles Dickens
  158. ' 100 Sherlock Holmes
  159. '  36 Bob Crachit
  160. '  46 Archie Moore
  161. '
  162. 'Removing the 'test' database.
  163. '
  164. 'Msg No.: 5701  Severity:  1  Level:  0
  165. 'Message: Changed database context from 'test' to 'master'.
  166. '
  167. 'Msg No.: 5701  Severity:  1  Level:  0
  168. 'Message: Changed database context from 'test' to 'master'.
  169.  
  170. 'User-defined handlers.  BASIC DB-LIBRARY will call these procedures if
  171. 'there is an error or a message from the server.
  172. '
  173. FUNCTION UserSqlErrorHandler% (SqlConn&, State%, DbErr%, OsErr%, DbErrStr$, OsErrStr$)
  174.     PRINT
  175.     IF SqlDead%(SqlConn&) THEN
  176.         PRINT "Connection to SQL Server failed."
  177.         UserSqlErrorHandler% = INTEXIT
  178.         EXIT FUNCTION
  179.     END IF
  180.     PRINT "State: "; State%; " Error No: "; DbErr%; " OS Error: "; OsErr%
  181.     PRINT "DB-LIBRARY Error: "; DbErrStr$
  182.     IF OsErr% <> DBNOERR THEN PRINT "Operating-System Error: "; OsErrStr$
  183.     UserSqlErrorHandler% = INTCANCEL
  184. END FUNCTION
  185.  
  186. SUB UserSqlMsgHandler (SqlConn&, MsgNo&, Severity%, Level%, Msg$)
  187.     PRINT
  188.     PRINT "Msg No.:"; MsgNo&; " Severity: "; Severity%; " Level: "; Level%
  189.     PRINT "Message: "; Msg$
  190. END SUB
  191.  
  192.