VB.net save a picture to a SQL Server database (i)

Source: Internet
Author: User
server| Data | Database preparation knowledge: STREAM, ado.net



Microsoft's SQL Server database image, text and other fields are binary large objects. These objects are accessed slightly differently from other lightweight objects. For example, when we open a datasheet, common types of fields can be seen, but the image type of the field is not, can only be read programmatically. This piece of information is about how to use VB. NET to the SQL Server database to store pictures, how to remove picture browsing from the database. I also spent the boss's energy here, mainly to answer a classmate's questions. His inquisitive way of learning, I was anxious out of a cold sweat, ashamed ... )



This routine uses SQL Server's own Northwind database, where employees (staff) datasheet has a field "photo", which is used to store photos, there are 9 records, I spent half a day trying to see the 9 people picture, is invisible. It is now speculated that the photo of these people are simply empty. So I decided to add some new records.



In this routine, I implemented a simple WinForm program that selects a picture file (BMP or JPG) by clicking the "Open" button and displays it in the graphics control PictureBox. Save to the database by using the saving button. Click the "View" checkbox to switch to the browse state and watch the image stored in the database.


The System.IO namespace of the Microsoft. NET Framework provides us with a FileStream file stream class. We can use this file stream to easily read and write binary large objects. Because of the flow operation used for binary large objects, it is universal for any file. You can also do this by reading and writing text files.

Steps:

1, first look at the Northwind database Employees table structure


2, know the method of. NET connection SQL Server, do not know, look at my written "ado.net in the Connection object" bar. It's OK to read the following sentences:

Dim Conn As New SqlConnection ("server=localhost;database=northwind;integrated security=true;"

Dim Sqlcomm as New SqlCommand

Sqlcomm. Connection = conn

Sqlcomm.commandtext = "INSERT into employees (Lastname,firstname,photo) VALUES (@lastname, @firstname, @photo)"

Dim Prm1 as New SqlParameter ("@lastname", Txtln.text)

Dim Prm2 as New SqlParameter ("@firstname", Txtfn.text)

Dim prm3 as New SqlParameter ("@photo", SqlDbType.VarBinary, Int (FS). Length), _ ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data)

Sqlcomm. Parameters.Add (PRM1)

Sqlcomm. Parameters.Add (PRM2)

Sqlcomm. Parameters.Add (PRM3)




3, create a new Windows application form, the interface is as follows: (Figure 1)



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.