The following code implements the functionality of adding pictures and text to the SQL Server database.
First, perform the following SQL statements in the SQL query analyzer to create a table and stored procedure.
Create Table photos ([Name] varchar (50), [Photo] Image Null
Create Procedure SP_INSERTPHOTO @name as varchar (50), @Image as image assert @Name, [photo]) Values (@name, @Image) Go is the complete code, copying can be run:
Imports System.iopublic Class Form1 inherits System.Windows.Forms.Form Dim Cn As Sqlclient.sqlConnection
#Region "Windows Form Designer Generated Code"
Public Sub new () mybase.new ()
'This call is required by the windows form designer. InitializationComponent ()
'Add anyinitization after the initializecomponent () CALL
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose () End If End If MyBase.Dispose (disposing) End Sub
'Required by The Windows Form Designer Private Components as System.comPonentModel.icontainer
'NOTE: The following procedure is required by the Windows Form Designer' It can be modified using the Windows Form Designer 'Do not modify it using the code editor Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.. .Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection Friend WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System. Windows.Forms.Button Friend WitHevents Button5 As System.Windows.Forms.Button
Label1 'me.label1.location = new system.drawing.point (15, 19) me.label1.name = "label1" me.label1.size = new system.drawing.size (80, 24) me.label1.tabindex = 0 me.label1.text = "Name:" 'Label2' me.label2.location = new system.drawing.point (11, 64) me.label2.name = "label2" me.label2.size = new system .Drawing.size (80, 20) me.label2.tabindex = 1 me.label2.text = "picture" '' TextBox1 'me.textbox1.location = new system.drawing.point (75, 16) Me.TextBox1. name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size (173, 20) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "" '' PictureBox1 'Me.PictureBox1.Location = New System. Drawing.Point (68, 48) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size (376, 222) Me.PictureBox1.TabIndex = 3 Me.PictureBox1.TabStop = False '' Button1 'me.button1.location = new system.drawing.point (278, 13) me.button1.name = "button1" Me.Button1.Size = new system.drawing.size (96, 26) me.button1.tabindex = 4 me.button1.text = "Browse Pictures ..." 'Button2' me.button2.location = new system.drawing. Point (57, 277) me.button2.name = "button2" me.button2.size = new system.drawing.size (100, 32) me.button2.tabindex = 5 me.button2.text = "Add to Database" '' SqlConnection1 'me.sqlconnection1.connectionstring = "data source = .; initial catalog = mxh;
User Id = sa; Password =;. " '' SqlCommand1 'Me.SqlCommand1.CommandText =" dbo [sp_InsertPhoto] "Me.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure Me.SqlCommand1.Connection = Me.SqlConnection1 Me. SqlCommand1.Parameters.Add (New System.Data.SqlClient.SqlParameter ( "@ rETURN_value", _ System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Returnvalue, _ False, CType (10, Byte), CType (0, Byte), "", System.data.DataRowVersion.current, Nothing) Me.sqlcommand1.parameters.add (new system.data.sqlclient.sqlparameter ("@ name", _ system.data.sqldbtype.varchar , 50)) Me.SqlCommand1.Parameters.Add (New System.Data.SqlClient.SqlParameter ( "@ image", _ System.Data.SqlDbType.VarBinary, 2147483647)) '' Button3 'Me.Button3.Location = New System .Drawing.point (265, 277) me.button3.name = "button3" me.button3.size = new system.drawing.size (79, 32) me.button3.tabindex = 6 me.button3.text = display Record "'Button4 'Me.button4.location = new system.drawing.point (362, 277) me.button4.name = "Button4" me.button4.size = new system.drawing.size (72, 32) me.button4.tabindex = 7 me.button4.text = "Exit" '' Button5 'me.button5.location = new system.drawing.point (167, 277) me.button5.name = "Button5" me.button5.size = new system.drawing .Size (85, 32) me.button5.tabindex = 8 me.button5.text = "Delete this record" '' Form1 'me.autoscalebasesize =
New system.drawing.size (5, 13) me.clientsize = new system.drawing.size (491, 324) me.controls.addrange (new system.windows.forms.control () {me.PictureBox1, _ me. Button5, Me.Button4, Me.Button3, Me.Button2, Me.Button1, Me.TextBox1, Me.Label1, Me.Label2}) me.name = "form1" me.text = "form1" me.ResumeLayout (false ) End Sub
#End Region 'Browse Button Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click' Display Picture File OpenFileDialog1.InitialDirectory = "d: / pic" OpenFileDialog1.DefaultExt = "gif" Openfiledialog1.filter = "BMP Files (*. BMP) | * .bmp | gif files (*. Gif) | * .gif | jpg files (*. Jpg) | * .jpg" openfiledialog1.showdialog () PictureBox1.image = Image.fromfile (OpenFiledialog1.FileName) End Sub
'Add Button Private Sub Button2_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button2.Click' To Insert Image Dim st As New FileStream (OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read) Dim s AS String = TextBox1.text Dim Mbr As BinaryReader = New BinaryReader (ST) DIM Buffer (St.Length) AS BYTE MBR.READ (Buffer, 0, Cint (St.Length)) st.close () InsertImage (Buffer, S ) End Sub
'Function For Inserting in the Procdeure in the Database Public Function InsertImage (ByRef buffer, ByVal str) cn = New SqlClient.SqlConnection (SqlConnection1.ConnectionString) cn.Open () Dim cmd As New SqlClient.SqlCommand ( "sp_InsertPhoto", cn) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add ( "@ name", SqlDbType.VarChar) .value = TextBox1.Text cmd.Parameters.Add ( "@ image", SqlDbType.Image) .value = buffer cmd.ExecuteNonQuery () MsgBox ( "Image inserted") cn.Close () End Function'Function to Display Image Private Sub ShowImage (ByVal s As String) cn = New SqlClient.SqlConnection (SqlConnection1.ConnectionString) cn.Open () Dim str As String = "SELECT Photo from photos where name = '" & s "" DIM CMD AS New Sqlclient.sqlcommand (STR, CN) TextBox1.text = s Dim b () as byte b = cmd.executescalar () IF (b .Length> 0) The Dim Stream As New MemoryStream (B, True) Stream.Write (B, 0, B.Length) DrawToscale (New Bitmap (stream)) Stream.close () end if cn.close () end SUB
'Function to create instance for the image from the buffer private supetoscale (BYVAL BMP As Image) PictureBox1.Image = New Bitmap (BMP) End Sub
'Display Process Private Sub Button3_Click (Byval e as system.EventArgs) _ handles button3.click Dim i as string = inputbox ("Please enter the name:") showimage (i) End Sub
'Exit Process Private Sub Button4_Click (Byval E AS System.EventArgs) _ handles button4.click me.dispose () End Sub
'Deletion processing Private Sub Button5_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button5.Click cn = New SqlClient.SqlConnection (SqlConnection1.ConnectionString) cn.Open () Dim s As String = InputBox ( " Please enter the name you want to delete: ") DIM cmd as new sqlclient.sqlcommand (" delete from photos where name = '"& S &"' ", cn) cmd.executenonquery () msgbox (" image deleted ") cn.close () End SubEnd Class source: hai author: penglsh