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 / image.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-04-03  |  15.5 KB  |  480 lines

  1. VERSION 2.00
  2. Begin Form PrimaryWindow 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Image"
  6.    ControlBox      =   0   'False
  7.    ForeColor       =   &H00000000&
  8.    Height          =   7035
  9.    Icon            =   IMAGE.FRX:0000
  10.    Left            =   615
  11.    LinkMode        =   1  'Source
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    ScaleHeight     =   6345
  15.    ScaleWidth      =   6990
  16.    Top             =   75
  17.    Width           =   7110
  18.    Begin PictureBox Image_Control 
  19.       Height          =   3495
  20.       Left            =   120
  21.       ScaleHeight     =   3465
  22.       ScaleWidth      =   6705
  23.       TabIndex        =   12
  24.       Top             =   2760
  25.       Width           =   6735
  26.    End
  27.    Begin VBSQL VBSQL1 
  28.       Caption         =   "SQL Err/Msg"
  29.       Height          =   272
  30.       Left            =   4800
  31.       Top             =   2505
  32.       Visible         =   0   'False
  33.       Width           =   2055
  34.    End
  35.    Begin ListBox Titles_List 
  36.       Height          =   810
  37.       Left            =   135
  38.       TabIndex        =   8
  39.       Top             =   1440
  40.       Width           =   2055
  41.    End
  42.    Begin DirListBox Dir_Tree_Combo 
  43.       Height          =   1275
  44.       Left            =   2520
  45.       TabIndex        =   4
  46.       Top             =   1170
  47.       Width           =   2055
  48.    End
  49.    Begin FileListBox Image_File_List 
  50.       Height          =   1590
  51.       Left            =   4800
  52.       Pattern         =   "*.bmp"
  53.       TabIndex        =   5
  54.       Top             =   855
  55.       Width           =   2055
  56.    End
  57.    Begin DriveListBox Drive_Combo 
  58.       Height          =   357
  59.       Left            =   2528
  60.       TabIndex        =   3
  61.       Top             =   833
  62.       Width           =   2048
  63.    End
  64.    Begin ListBox Database_List 
  65.       Height          =   420
  66.       Left            =   135
  67.       TabIndex        =   10
  68.       Top             =   360
  69.       Width           =   2055
  70.    End
  71.    Begin TextBox Title_Edit 
  72.       Height          =   323
  73.       Left            =   3855
  74.       TabIndex        =   1
  75.       Top             =   75
  76.       Width           =   3000
  77.    End
  78.    Begin Label Label5 
  79.       BackColor       =   &H00C0C0C0&
  80.       Caption         =   "Image in database:"
  81.       Height          =   225
  82.       Left            =   120
  83.       TabIndex        =   11
  84.       Top             =   2520
  85.       Width           =   2055
  86.    End
  87.    Begin Label Label4 
  88.       BackColor       =   &H00C0C0C0&
  89.       Caption         =   "Titles in database:"
  90.       Height          =   225
  91.       Left            =   120
  92.       TabIndex        =   9
  93.       Top             =   1200
  94.       Width           =   2055
  95.    End
  96.    Begin Label Image_File_Label 
  97.       BackColor       =   &H00C0C0C0&
  98.       Caption         =   "(none)"
  99.       Height          =   255
  100.       Left            =   3000
  101.       TabIndex        =   7
  102.       Top             =   500
  103.       Width           =   3840
  104.    End
  105.    Begin Label Label3 
  106.       BackColor       =   &H00C0C0C0&
  107.       Caption         =   "File:"
  108.       Height          =   255
  109.       Left            =   2520
  110.       TabIndex        =   6
  111.       Top             =   500
  112.       Width           =   375
  113.    End
  114.    Begin Label Label2 
  115.       BackColor       =   &H00C0C0C0&
  116.       Caption         =   "Image file title:"
  117.       ForeColor       =   &H00000000&
  118.       Height          =   225
  119.       Left            =   2520
  120.       TabIndex        =   2
  121.       Top             =   120
  122.       Width           =   1335
  123.    End
  124.    Begin Label Label1 
  125.       BackColor       =   &H00C0C0C0&
  126.       Caption         =   "Databases:"
  127.       Height          =   210
  128.       Left            =   120
  129.       TabIndex        =   0
  130.       Top             =   120
  131.       Width           =   2085
  132.    End
  133.    Begin Menu Menu_File 
  134.       Caption         =   "&File"
  135.       Begin Menu Logon_Selection 
  136.          Caption         =   "&Logon"
  137.       End
  138.       Begin Menu Log_Off_Selection 
  139.          Caption         =   "Log &Off"
  140.       End
  141.       Begin Menu Exit_Selection 
  142.          Caption         =   "&Exit"
  143.       End
  144.    End
  145.    Begin Menu Options_Menu 
  146.       Caption         =   "&Options"
  147.       Begin Menu View_Selection 
  148.          Caption         =   "&View Image"
  149.       End
  150.       Begin Menu Insert_Selection 
  151.          Caption         =   "&Insert Image"
  152.       End
  153.       Begin Menu Delete_Selection 
  154.          Caption         =   "&Delete Image"
  155.       End
  156.    End
  157.    Begin Menu About_Menu 
  158.       Caption         =   "&About"
  159.    End
  160. Sub About_Menu_Click ()
  161.     About_Form.Show 1
  162. End Sub
  163. Function CheckForImageTable () As Integer
  164. Rem Check to see if sample table exits
  165. Rem If it's not there, then see if they want to create it
  166.     Results% = ExecuteSQLCommand("Select count(*) from sysobjects where name = 'image_table'")
  167.     Do While (SqlResults%(SqlConn%) <> NOMORERESULTS)
  168.     Do While (SqlNextRow(SqlConn%) <> NOMOREROWS)
  169.         Table_Found$ = Sqldata(SqlConn%, 1)
  170.     Loop
  171.     Loop
  172.     If Val(Table_Found$) = 0 Then
  173.     Results% = MsgBox("Text table not found in " + Database$ + " database." + Chr$(13) + Chr$(10) + "Do you wish to create the table?", 52)
  174.     If Results% = 7 Then
  175.         CheckForImageTable = FAIL
  176.         Exit Function
  177.     Else
  178.         Cmd$ = "create table image_table (title varchar(30) not null, image_col image null)"
  179.         Results% = SqlCmd(SqlConn%, Cmd$)
  180.         Results% = SqlExec(SqlConn%)
  181.         Do While (SqlResults%(SqlConn%) <> NOMORERESULTS)
  182.         Do While (SqlNextRow(SqlConn%) <> NOMOREROWS)
  183.         Loop
  184.         Loop
  185.         CheckForImageTable = SUCCEED
  186.     End If
  187.     Else
  188.     CheckForImageTable = SUCCEED
  189.     End If
  190. End Function
  191. Sub ClearImageTitles ()
  192.     Rem Clear all text titles out of list box
  193.     Do While Titles_List.ListCount
  194.     Titles_List.RemoveItem 0
  195.     Loop
  196. End Sub
  197. Sub Database_List_Click ()
  198. Rem This procedure gets the chosen database.
  199. Rem Checks to see if the image table is there
  200. Rem If it's not, then clear the images field, and disable other fields
  201. Rem If it is, then get the list of images, display the list and enable other fields.
  202.     DatabaseSelection$ = Database_List.Text
  203.     Results% = SqlUse(SqlConn%, DatabaseSelection$)
  204.     Results% = CheckForImageTable()
  205.     If Results% = SUCCEED Then
  206.     PrimaryWindow.MousePointer = 11
  207.     RetrieveImageTitles
  208.     View_Selection.Enabled = True
  209.     Insert_Selection.Enabled = True
  210.     Delete_Selection.Enabled = True
  211.     PrimaryWindow.MousePointer = 0
  212.     Else
  213.     ClearImageTitles
  214.     View_Selection.Enabled = False
  215.     Insert_Selection.Enabled = False
  216.     Delete_Selection.Enabled = False
  217.     End If
  218. Rem Change the primary window title to show the database name
  219. Rem Clear the image control
  220.     DatabaseName$ = SQLName(SqlConn%)
  221.     ChangePrimaryWindowCaption
  222.     Image_Control.Picture = LoadPicture()
  223. End Sub
  224. Sub Delete_Selection_Click ()
  225.     Image_Title$ = Titles_List.Text
  226.     If Image_Title$ = "" Then
  227.     Beep
  228.     MsgBox "You must first select a title."
  229.     Else
  230.     Response% = MsgBox("Delete " + Image_Title$ + "?", 49)
  231.     If Response% = 1 Then
  232.         PrimaryWindow.MousePointer = 11
  233.         Results% = ExecuteSQLCommand("Delete from image_table where title = '" + Image_Title$ + "'")
  234.         Results% = SqlResults%(SqlConn%)
  235.         Do While (SqlNextRow(SqlConn%) <> NOMOREROWS)
  236.         Loop
  237.         Image_Control.Picture = LoadPicture()
  238.         RetrieveImageTitles
  239.         PrimaryWindow.MousePointer = 0
  240.     Else
  241.         MsgBox "Delete aborted."
  242.     End If
  243.     End If
  244. End Sub
  245. Sub Dir_Tree_Combo_Change ()
  246.     Image_File_Label.Caption = "(none)"
  247.     Image_File_List.Path = Dir_Tree_Combo.Path
  248. End Sub
  249. Sub Drive_Combo_Change ()
  250.     Image_File_Label.Caption = "(none)"
  251.     Dir_Tree_Combo.Path = Drive_Combo.Drive
  252. End Sub
  253. Sub Exit_Selection_Click ()
  254.     ExitApplication
  255.     End
  256. End Sub
  257. Sub Form_Load ()
  258.    Temp_Image_File = "c:\image.tmp"
  259.     PrimaryWindowTitle = "Image Example"
  260.     ChangePrimaryWindowCaption
  261.     InitializeApplication
  262.     MsgBox DBLIB_VERSION$
  263.     Logon_Selection.Enabled = True
  264.     Log_Off_Selection.Enabled = False
  265.     Exit_Selection.Enabled = True
  266.     View_Selection.Enabled = False
  267.     Insert_Selection.Enabled = False
  268.     Delete_Selection.Enabled = False
  269. End Sub
  270. Sub Image_File_List_Click ()
  271.     If Right$(Dir_Tree_Combo.Path, 1) = "\" Then
  272.     Image_File_Label.Caption = Dir_Tree_Combo.Path + Image_File_List.FileName
  273.     Else
  274.     Image_File_Label.Caption = Dir_Tree_Combo.Path + "\" + Image_File_List.FileName
  275.     End If
  276. End Sub
  277. Sub Insert_Selection_Click ()
  278. Rem This routine will insert the image from the file into the database
  279.     Image_File$ = Image_File_Label.Caption
  280.     Title$ = Title_Edit.Text
  281.     Title$ = PrepareString(Title$)
  282.     If Image_File$ = "(none)" Then
  283.     Beep
  284.     MsgBox "Please select a bitmap to insert."
  285.     ElseIf Title$ = "" Then
  286.     Beep
  287.     MsgBox "Please enter a title for the bitmap you wish to insert."
  288.     Else
  289.     PrimaryWindow.MousePointer = 11
  290.     InsertImage Title$, Image_File$
  291.     Title_Edit.Text = ""
  292.     Image_File_List.ListIndex = -1
  293.     Image_File_Label.Caption = ""
  294.     RetrieveImageTitles
  295.     PrimaryWindow.MousePointer = 0
  296.     End If
  297. End Sub
  298. Sub InsertImage (Title As String, Bitmap_File As String)
  299. Rem This routine inserts an image into the SQL Server
  300. Rem Insert new row with title and image token data
  301.     Results% = ExecuteSQLCommand("Insert into image_table values ('" + Title + "',0x80)")
  302.     Do While SqlResults(SqlConn%) <> NOMORERESULTS%
  303.     Do While SqlNextRow(SqlConn%) <> NOMOREROWS%
  304.     Loop
  305.     Loop
  306. Rem Get identifier for image column in current row
  307.     Results% = ExecuteSQLCommand("select image_col from image_table where title = '" + Title + "'")
  308.     Do While SqlResults(SqlConn%) <> NOMORERESULTS%
  309.     Do While SqlNextRow(SqlConn%) <> NOMOREROWS%
  310.         SqlPointer$ = SqlTxPtr(SqlConn%, 1)
  311.         SqlTimestamp$ = SqlTxTimeStamp(SqlConn%, 1)
  312.     Loop
  313.     Loop
  314. Rem Open bitmap file to load into SQL Server table
  315.     Open Bitmap_File For Binary As #1
  316.     FileLength& = LOF(1)
  317. Rem Begin inserting bitmap into image column in DatePartLimit& size chunks
  318.     Table$ = "image_table.image_col"
  319.     DataPartLimit& = 4096
  320.     DataPartSize& = 0
  321.     If SqlWriteText(SqlConn%, Table$, SqlPointer$, SQLTXPLEN%, SqlTimestamp$, 1, FileLength&, "") <> FAIL% Then
  322.     If SqlOk(SqlConn%) <> FAIL% Then
  323.         Results% = SqlResults(SqlConn%)
  324.         Done% = False
  325.         Do While Not Done%
  326.         DataPart$ = Input$(DataPartLimit&, 1)
  327.         DataPartSize& = DataPartSize& + Len(DataPart$)
  328.         Results% = SqlMoreText%(SqlConn%, Len(DataPart$), DataPart$)
  329.         If DataPartSize& = FileLength& Then
  330.             Done% = True
  331.         End If
  332.         Loop
  333.         If SqlOk(SqlConn%) <> FAIL% Then
  334.         If SqlResults(SqlConn%) <> FAIL% Then
  335.             MsgBox "Image inserted."
  336.         End If
  337.         End If
  338.     End If
  339.     End If
  340. Close 1
  341. End Sub
  342. Sub LoadImage (Title As String)
  343. Rem This routine reads an image from the SQL Server
  344. Rem Get length of bitmap image in image column
  345.     Results% = ExecuteSQLCommand("select datalength(image_col) from image_table where title = '" + Title + "'")
  346.     Do While SqlResults(SqlConn%) <> NOMORERESULTS
  347.     Do While SqlNextRow(SqlConn%) <> NOMOREROWS
  348.         ImageLen& = Val(Sqldata(SqlConn%, 1))
  349.     Loop
  350.     Loop
  351.     Offset& = 0
  352. Rem Set LoadSizeLimit to 8K
  353.     LoadSizeLimit& = 4096
  354.     If LoadSizeLimit& > ImageLen& Then
  355.     LoadSizeLimit& = ImageLen&
  356.     End If
  357.     LoadSize& = LoadSizeLimit&
  358. Rem Set size of image returned to LoadSizeLimit&
  359.     Results% = ExecuteSQLCommand("set textsize " + Str$(LoadSizeLimit&))
  360.     Do While SqlResults(SqlConn%) <> NOMORERESULTS
  361.     Do While SqlNextRow(SqlConn%) <> NOMOREROWS
  362.     Loop
  363.     Loop
  364. Rem Begin reading image column in LoadSizeLimit& size chunks
  365.     Cmd$ = "Declare @val varbinary(30)"
  366.     Results% = SqlCmd(SqlConn%, Cmd$)
  367.     Cmd$ = "Select @val = textptr(image_col) from image_table where title = '" + Title + "'"
  368.     Results% = SqlCmd(SqlConn%, Cmd$)
  369.     Table$ = "image_table.image_col"
  370.     Done% = False
  371.     Do While Not Done%
  372.     Cmd$ = "READTEXT " + Table$ + " @val " + Str$(Offset&) + " " + Str$(LoadSize&)
  373.     Results% = SqlCmd(SqlConn%, Cmd$)
  374.     If Offset& + LoadSize& = ImageLen& Then
  375.         Done% = True
  376.     Else
  377.         Offset& = Offset& + LoadSizeLimit&
  378.         If Offset& + LoadSizeLimit& > ImageLen& Then
  379.         LoadSize& = ImageLen& - Offset&
  380.         End If
  381.     End If
  382.     Loop
  383. Rem Retrieve image data in result rows and write to temporary bitmap file
  384.     If SqlExec(SqlConn%) <> FAIL% Then
  385.     Open Temp_Image_File For Binary As #1
  386.     filepos& = 1
  387.     Do While SqlResults(SqlConn%) <> NOMORERESULTS
  388.         Do While SqlNextRow(SqlConn%) <> NOMOREROWS
  389.         in$ = Sqldata(SqlConn%, 1)
  390.         Put 1, filepos&, in$
  391.         filepos& = filepos& + Len(in$)
  392.         Loop
  393.     Loop
  394.     Close 1
  395.     End If
  396. End Sub
  397. Sub Log_Off_Selection_Click ()
  398.     Logoff
  399.     Logon_Selection.Enabled = True
  400.     Log_Off_Selection.Enabled = False
  401.     Exit_Selection.Enabled = True
  402.     View_Selection.Enabled = False
  403.     Insert_Selection.Enabled = False
  404.     Delete_Selection.Enabled = False
  405. End Sub
  406. Sub Logon_Selection_Click ()
  407.     Login.Show 1
  408.     PrimaryWindow.MousePointer = 11
  409.     If CheckServerConnection() = 1 Then
  410.     Results% = GetDatabases(Database_List)
  411.     ChangePrimaryWindowCaption
  412.     Logon_Selection.Enabled = False
  413.     Log_Off_Selection.Enabled = True
  414.     End If
  415.     PrimaryWindow.MousePointer = 0
  416. End Sub
  417. Function PrepareString (String_In As String) As String
  418.     String_Out$ = ""
  419.     For I% = 1 To Len(String_In)
  420.     If Mid$(String_In, I%, 1) = Chr$(39) Then
  421.         String_Out$ = String_Out$ + Chr$(39) + Chr$(39)
  422.     Else
  423.         String_Out$ = String_Out$ + Mid$(String_In, I%, 1)
  424.     End If
  425.     Next
  426.     PrepareString = String_Out$
  427. End Function
  428. Sub RetrieveImageTitles ()
  429.     ClearImageTitles
  430.     Rem Retrieve text titles from SQL Server into list box
  431.     Results% = ExecuteSQLCommand("Select title from image_table")
  432.     Do While (SqlResults%(SqlConn%) <> NOMORERESULTS)
  433.     Do While (SqlNextRow(SqlConn%) <> NOMOREROWS)
  434.         Titles_List.AddItem Sqldata(SqlConn%, 1)
  435.     Loop
  436.     Loop
  437. End Sub
  438. Sub Title_Edit_KeyPress (KeyAscii As Integer)
  439.     If Len(Title_Edit.Text) = 30 Then
  440.     If KeyAscii <> 8 Then
  441.         KeyAscii = 0
  442.     End If
  443.     End If
  444. End Sub
  445. Sub Titles_List_Click ()
  446. Rem Clear the picture
  447.     Image_Control.Picture = LoadPicture()
  448. End Sub
  449. Sub Titles_List_DblClick ()
  450. Rem Get the image image selected from the server
  451. Rem Display the image
  452.     Image_Title$ = Titles_List.Text
  453.     PrimaryWindow.MousePointer = 11
  454.     LoadImage Image_Title$
  455.     Image_Control.Picture = LoadPicture(Temp_Image_File)
  456.     PrimaryWindow.MousePointer = 0
  457. End Sub
  458. Sub VBSQL1_Error (SqlConn As Integer, Severity As Integer, ErrorNum As Integer, ErrorStr As String, RetCode As Integer)
  459. ' Call the required VBSQL error-handling function
  460. ' OSErr and OSErrStr not used in VBSQL for Windows, but DOS interprets
  461. ' anything other than -1 as an OS error
  462.     OsErr% = -1
  463.     RetCode% = UserSqlErrorHandler%(SqlConn, Severity%, ErrorNum%, OsErr%, ErrorStr$, OsErrStr$)
  464. End Sub
  465. Sub VBSQL1_Message (SqlConn As Integer, Message As Long, State As Integer, Severity As Integer, MsgStr As String)
  466.     UserSqlMsgHandler SqlConn, Message&, State%, Severity%, MsgStr$
  467. End Sub
  468. Sub View_Selection_Click ()
  469.     Image_Title$ = Titles_List.Text
  470.     If Image_Title$ = "" Then
  471.     Beep
  472.     MsgBox "You must first select a title."
  473.     Else
  474.     PrimaryWindow.MousePointer = 11
  475.     LoadImage Image_Title$
  476.     Image_Control.Picture = LoadPicture(Temp_Image_File)
  477.     PrimaryWindow.MousePointer = 0
  478.     End If
  479. End Sub
  480.