Home > Software engineering >  Almighty god! Help genuflect is begged! Vb picture save in binary form to essentially a database
Almighty god! Help genuflect is begged! Vb picture save in binary form to essentially a database

Time:09-25

To be a function to upload pictures, I didn't do the vb, encounter problems not solve, please help ahhh, almighty god,,
Requirement is first put a can edit the picture control (because to connect tablet, tablet on written word become pictures showed on the picture editing control directly), then the image in the form of binary saved into the database, and then read it out to the interface displayed and I put a button now, click the upload button simulation function, after obtaining image path 'reads the images this times is not the object of reference to real error, the great god help me to see why, and what we can edit pictures plug-in is recommended, and grateful to be: : thank you very much error location: Dim strPath As String=" D: \ \ agent system CLIENTCARGOBOOKING \ Resources \ 88. JPG "

Dim strPhotoPath As String=System. Web. HttpContext. Current. Server MapPath (strPath) 'reads the images this






The following is a complete code page:
 Imports System. IO 
Imports System. Text
Imports System. The RunTime. Serialization. Formatters
Imports System. Data
Imports Cargo. Bus
Imports the ime
Imports system. Math

Public Class Form1

Private Sub Button1_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles for. Click
'image path
Dim strPath As String="D: \ \ agent system CLIENTCARGOBOOKING \ Resources \ 88 JPG"

Dim strPhotoPath As String=System. Web. HttpContext. Current. Server MapPath (strPath) 'read picture
Dim the fs As FileStream=New System. IO. FileStream (strPhotoPath, FileMode. Open, FileAccess. Read)
Dim br As BinaryReader=New BinaryReader (fs)
Dim photo () As Byte=br. ReadBytes (CType (fs) Length, Integer))
Br. The Close ()
Fs. The Close ()
Dim myConn As SqlClient. SqlConnection=New SqlClient. SqlConnection (" Data Source=. Initial Catalog=JM; User ID=sa; Password=123 ")
Dim strComm As String="INSERT INTO zy (personName, personPhotoPath personPhoto)"
StrComm=(strComm + (" VALUES (' wangwu ', '" + (strPath + "@ photoBinary)',")))

Dim myComm As SqlClient. SqlCommand=New SqlClient. SqlCommand (strComm myConn)
MyComm. The Parameters. The Add (" @ photoBinary ", SqlDbType. Binary, photo. Length)
MyComm. The Parameters (" @ photoBinary "). The Value=https://bbs.csdn.net/topics/photo
MyConn. The Open ()
MyComm. ExecuteNonQuery ()
MyConn. Close ()
'3: VB IMAGE save file in database field ()
End Sub

The End of the Class


Only 40 points, points behind in additional

CodePudding user response:

In VB ADO object has a Stream object is binary data operation, the stored images to the database, you first need to convert image data into binary data, can be stored in a SQL database image field, after read binary data, using the Stream object converts binary data image, displayed in the interface,
You of this program is VB.NET, although a little, but didn't do it binary data conversion, VB60 done many,

CodePudding user response:

Ado is above 2.5 version

CodePudding user response:

Give you an example of a VB60 storage and read the pictures, for your reference, because SQL voice whether VB60 or VB2008 should be the same:
1, declare a ADODB Stream object, is Dim in VB60 Mst As New ADODB. Stream
The pictures in 2, find, read the hard disk, storage paths, extension
 Private Sub Command1_Click () 'using the file open dialog box to find hard disk image files 
On Error GoTo Err
CommonDialog1. Filter="JPG file (*.jpg) | *. JPG | GIF files (*. GIF) | *. GIF (*. BMP) | | BMP file *. BMP (*. PNG) | | PNG file *. PNG"
CommonDialog1. ShowOpen
Text2. Text=CommonDialog1. FileName 'get the picture of hard disk path + name + extension
SSR=CommonDialog1. FileTitle 'get the picture file name + extension
SSR (SSR, 4)=Right 'for the extension of the images, delimited ". "
Command2. Enabled=True
The Exit Sub
Err:
MsgBox Err. The Description 'click on the cancel button will display an error, error content
End Sub

The pictures 3, the specific data to the database table, just connect to the database statements, you can according to your database connection can
 RS. Open the "select * from table name", cn, 3, 2 
RS. AddNew
RS. Fields (" name ")=Text1. The Text 'storage by setting their own pictures name
The Mst. Type=adTypeBinary
Mst. Open the Mst objects Open '
If Text2. Text & lt;> . "" Then the Mst LoadFromFile Text2. Text 'Mst object image content in Text2 into binary data
RS. The Fields (" file ")=the Mst. Read 'Image of binary data stored in the data table data type field, the field name here is [file]
RS. Fields (" extension ")=SSR 'image file extension stored in this field
RS. Update
The Mst. Close
RS. Close

4, read binary image data stored in the data table is
 Image1. Picture=LoadPicture (" ") 
RS. Open the "select * from data table name order By ID Desc", cn, 2, 2 'SQL statements can be according to your request to modify
The Mst. Type=adTypeBinary
The Mst. Open
The Mst. Write RS. Fields (" file ") 'Mst object to read database binary data
The Mst. SaveToFile App. Path & amp; "\ doc " & amp; RS. Fields (" name ") & amp; RS. Fields (" extension "), adSaveCreateOverWrite
'the Mst object will be converted image files stored in the doc directory, stored images are stored in the database file name + extension
Image1. Picture=LoadPicture (App) Path & amp; "\ doc " & amp; RS. Fields (" name ") & amp; RS. Fields (" extension "))
'images displayed in Image1 controls
The Mst. Close
RS. Close

Not very complicated, in this example, you should be able to achieve the desired requirements in VB2008,

The image is shown in VB60 data tables stored image capture,
  • Related