VB.NET Save Pictures in SQL Server Database (1)

zhaozj2021-02-16  56

VB.NET Save Pictures in SQL Server Database

Yuan Zhi

Preparatory knowledge: stream, ado.net

Microsoft's SQL Server Database Image, Text and other fields are binary large objects. The access and other light objects of these objects are slightly different. For example, when we open a data sheet, the normal type field can be seen, but the field of the image type is not, only by programming methods. This information is to describe how to use VB.NET to store pictures to the SQL Server database, how to take a picture browse from the database. I also cost the boss here, mainly to answer a study by a classmate. (He planed the way to ask the bottom learning method, and I urged me to sweat, embarrassing ...)

This routine uses the northwind database of SQL Server, where the Employees data table has a field "photo". It is used to store photos, there are 9 records inside, I spent halfway, I want to see That 9 people pictures are invisible. Now guess that these people's photo is empty. So, I decided to add some new records.

In this routine, I implemented a simple WinForm program, which selects a picture file (BMP or JPG) by clicking the "Open" button and appears in the graphic control PictureBox. Use the "Save" button to store the database. Click "View" checkbox to switch to the browsing status, watch the image stored in the database.

Microsoft. NET FRAMEWORK SYSTEM.IO Namespace provides us with a FileStream file stream class. We can use this file to easily read and write the binary large object. Since the flow operations used for binary large objects, there is versatility for any file. I can do this with writing text files. step:

1, let's take a look at the Employees table structure in the Northwind database.

2, know the way .NET connection SQL Server method, don't know, look at the "ADO.NET in ADO.NET". I don't have any questions below.

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): there is a checkbox: Checkbox1,

There are two TEXTBOX: TXTLN, TXTFN,

There are four Lebel, one of which doesn't have text, which (used to store the photo file location),

Four Button: btnback, btnforward, btnopen, btnsave

4, enter the code design section, first declare the namespace of the application:

Imports system

Imports system.drawing

Imports system.collections

Imports system.componentmodel

Imports System.Windows.Forms

Imports system.data

Imports system.drawing.Image

Imports system.io

Imports system.data.sqlclient

It seems to be a little, but it is not poisoned anyway.

(2)

转载请注明原文地址:https://www.9cbs.com/read-21909.html

New Post(0)