home *** CD-ROM | disk | FTP | other *** search
-
- 'EXAMPLE3.BAS -- This example creates a new database, then inserts
- 'data from a data file into a new table containing several datatypes.
- 'The program then updates the table using browse-mode techniques.
-
- 'To run this example, you must have the file DATAFILE.TXT in the
- 'current directory. You must also have CREATE DATABASE permission in
- 'your login database and 2mb of free space on the default drive.
-
- 'Copyright (c) 1990 by Microsoft Corp. All rights reserved.
-
-
- '$INCLUDE: 'sql.bi'
-
- MemLeft& = SETMEM(-16384) 'Reserve 16K of heap.
- STACK 8192 'Reserve 8K of stack space.
-
- 'Initialize BASIC DB-LIBRARY.
- IF SqlInit$ = "" THEN
- PRINT "Can't start BASIC DB-LIBRARY."
- END
- END IF
-
- 'Get a login record and fill it in with the necessary information.
- Login& = SqlLogin&
- Result% = SqlSetLUser%(Login&, "loginid")
- Result% = SqlSetLPwd%(Login&, "passwd")
- Result% = SqlSetLApp%(Login&, "example")
-
- 'Get two connections for communicating with SQL Server; one to query
- 'the database, one to simultaneously update the database.
- SqlConn& = SqlOpen&(Login&, "server")
- UpdSqlConn& = SqlOpen&(Login&, "server")
- IF SqlConn& = 0 OR UpdSqlConn& = 0 THEN END
-
- PRINT : PRINT "Creating the 'test' database."
- Result% = SqlCmd%(SqlConn&, "CREATE DATABASE test")
- Result% = SqlExec%(SqlConn&)
- Result% = SqlResults%(SqlConn&)
- Result% = SqlUse%(SqlConn&, "test")
- Result% = SqlUse%(UpdSqlConn&, "test")
-
- PRINT : PRINT "Creating the 'alltypes' table."
- cmd$ = "CREATE TABLE alltypes"
- cmd$ = cmd$ + " ( age tinyint, userid smallint, royalty int,"
- cmd$ = cmd$ + " name char(25), title_id varbinary(20),"
- cmd$ = cmd$ + " citizen bit, account float, title varchar(20),"
- cmd$ = cmd$ + " manager char(25), timestamp )"
- cmd$ = cmd$ + " CREATE UNIQUE INDEX myindex ON alltypes(userid)"
- Result% = SqlCmd%(SqlConn&, cmd$)
- Result% = SqlExec%(SqlConn&)
- DO UNTIL SqlResults%(SqlConn&) = NO.MORE.RESULTS
- LOOP
-
- 'Open data file for sequential input.
- OPEN "datafile.txt" FOR INPUT AS #1
-
- PRINT : PRINT "Inserting rows into the 'alltypes' table."
- DO UNTIL EOF(1)
- LINE INPUT #1, Values$
- cmd$ = " INSERT INTO alltypes VALUES(" + Values$ + ", null)"
- Result% = SqlCmd%(SqlConn&, cmd$)
- LOOP
- Result% = SqlExec%(SqlConn&)
-
- 'Process the results of each INSERT statement.
- Result% = SqlResults%(SqlConn&)
- DO UNTIL Result% = NO.MORE.RESULTS
- IF Result% = FAIL THEN PRINT "One of the INSERT statements failed."
- Result% = SqlResults%(SqlConn&)
- LOOP
-
- 'Print the contents of the new table.
- PRINT : PRINT "Selecting rows from the 'alltypes' table:"
- Result% = SqlCmd%(SqlConn&, "SELECT age, name FROM alltypes")
- Result% = SqlExec%(SqlConn&)
- Result% = SqlResults%(SqlConn&)
- Result% = SqlPrRow%(SqlConn&)
-
- PRINT : PRINT "Updating rows (incrementing age of each person)."
- Result% = SqlCmd%(SqlConn&, "SELECT * FROM alltypes FOR BROWSE")
- Result% = SqlExec%(SqlConn&)
- Result% = SqlResults%(SqlConn&)
- DO UNTIL Result% = NO.MORE.RESULTS
- IF Result% = SUCCEED THEN
- DO UNTIL SqlNextRow%(SqlConn&) = NO.MORE.ROWS
-
- 'Increment the age of each person in the table, using the
- 'WHERE clause returned by SqlQual$ to ensure that we are
- 'not overwriting another user's changes.
- Age% = VAL(SqlData$(SqlConn&, 1))
- Qual$ = SqlQual$(SqlConn&, -1, "alltypes")
- cmd$ = "UPDATE alltypes SET age = " + STR$(Age% + 1)
- cmd$ = cmd$ + " " + Qual$
- Result% = SqlCmd%(UpdSqlConn&, cmd$)
- Result% = SqlExec%(UpdSqlConn&)
- Result% = SqlResults%(UpdSqlConn&)
-
- LOOP
- END IF
- Result% = SqlResults%(SqlConn&)
- LOOP
-
- PRINT : PRINT "Selecting rows from the updated 'alltypes' table:"
- Result% = SqlCmd%(SqlConn&, "SELECT age, name FROM alltypes")
- Result% = SqlExec%(SqlConn&)
- Result% = SqlResults%(SqlConn&)
- Result% = SqlPrRow%(SqlConn&)
-
- PRINT : PRINT "Removing the 'test' database."
- Result% = SqlUse%(SqlConn&, "master")
- Result% = SqlUse%(UpdSqlConn&, "master")
- Result% = SqlCmd%(SqlConn&, "DROP DATABASE test")
- Result% = SqlExec%(SqlConn&)
- Result% = SqlResults%(SqlConn&)
-
- 'Close connection and exit program.
- CALL SqlExit
-
- END
-
-
- 'Sample Output
- '
- 'Msg No.: 5701 Severity: 2 Level: 0
- 'Message: Changed database context from 'master' to 'master'.
- '
- 'Msg No.: 5701 Severity: 2 Level: 0
- 'Message: Changed database context from 'master' to 'master'.
- '
- 'Creating the 'test' database.
- '
- 'Msg No.: 1805 Severity: 2 Level: 0
- 'Message: CREATE DATABASE: allocating 1024 pages on disk 'server'
- '
- 'Msg No.: 5701 Severity: 1 Level: 0
- 'Message: Changed database context from 'master' to 'test'.
- '
- 'Msg No.: 5701 Severity: 1 Level: 0
- 'Message: Changed database context from 'master' to 'test'.
- '
- 'Creating the 'alltypes' table.
- '
- 'Inserting rows into the 'alltypes' table.
- '
- 'Selecting rows from the 'alltypes' table:
- ' 23 Edgar Allen Poe
- ' 75 Charles Dickens
- ' 99 Sherlock Holmes
- ' 35 Bob Crachit
- ' 45 Archie Moore
- '
- 'Updating rows (incrementing age of each person).
- '
- 'Selecting rows from the updated 'alltypes' table:
- ' 24 Edgar Allen Poe
- ' 76 Charles Dickens
- ' 100 Sherlock Holmes
- ' 36 Bob Crachit
- ' 46 Archie Moore
- '
- 'Removing the 'test' database.
- '
- 'Msg No.: 5701 Severity: 1 Level: 0
- 'Message: Changed database context from 'test' to 'master'.
- '
- 'Msg No.: 5701 Severity: 1 Level: 0
- 'Message: Changed database context from 'test' to 'master'.
-
- 'User-defined handlers. BASIC DB-LIBRARY will call these procedures if
- 'there is an error or a message from the server.
- '
- FUNCTION UserSqlErrorHandler% (SqlConn&, State%, DbErr%, OsErr%, DbErrStr$, OsErrStr$)
- PRINT
- IF SqlDead%(SqlConn&) THEN
- PRINT "Connection to SQL Server failed."
- UserSqlErrorHandler% = INTEXIT
- EXIT FUNCTION
- END IF
- PRINT "State: "; State%; " Error No: "; DbErr%; " OS Error: "; OsErr%
- PRINT "DB-LIBRARY Error: "; DbErrStr$
- IF OsErr% <> DBNOERR THEN PRINT "Operating-System Error: "; OsErrStr$
- UserSqlErrorHandler% = INTCANCEL
- END FUNCTION
-
- SUB UserSqlMsgHandler (SqlConn&, MsgNo&, Severity%, Level%, Msg$)
- PRINT
- PRINT "Msg No.:"; MsgNo&; " Severity: "; Severity%; " Level: "; Level%
- PRINT "Message: "; Msg$
- END SUB
-
-