向SQL Server数据库添加图片和文字
From:
http://dotnet.aspx.cc/article/j9ubrver-l3vb-49m3-gou1-z6c2pvr6fz3k/read.aspx
首先,在SQL查询分析器中执行下面的SQL语句,以创建表和存储过程。
界面如下:

后台代码:
http://dotnet.aspx.cc/article/j9ubrver-l3vb-49m3-gou1-z6c2pvr6fz3k/read.aspx
首先,在SQL查询分析器中执行下面的SQL语句,以创建表和存储过程。
CREATE TABLE Photos (
[name] varchar(50),
[photo] image NULL
)
Go
CREATE PROCEDURE sp_InsertPhoto
@name AS VARCHAR(50),
@image AS IMAGE
AS
INSERT INTO Photos ([name], [photo])
VALUES (@name, @image)
GO
[name] varchar(50),
[photo] image NULL
)
Go
CREATE PROCEDURE sp_InsertPhoto
@name AS VARCHAR(50),
@image AS IMAGE
AS
INSERT INTO Photos ([name], [photo])
VALUES (@name, @image)
GO
界面如下:

后台代码:
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn As SqlClient.SqlConnection
Public Sub New()
MyBase.New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'open picture
OpenFileDialog1.InitialDirectory = "E:\test code\picture"
OpenFileDialog1.DefaultExt = "jpg"
OpenFileDialog1.Filter = "Bmp Files(*.jpg)|*.jpg|Gif Files(*.gif)|*.gif|Jpg Files(*.bmp)|*.bmp"
OpenFileDialog1.ShowDialog()
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'insert record
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) Then
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 Sub DrawToScale(ByVal bmp As Image)
PictureBox1.Image = New Bitmap(bmp)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim i As String = InputBox("请输入名字:")
ShowImage(i)
End Sub
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("请输入要删除的名字:")
Dim cmd As New SqlClient.SqlCommand("delete from photos where name='" & s & "'", cn)
cmd.ExecuteNonQuery()
MsgBox("Image deleted")
cn.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Dispose()
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn As SqlClient.SqlConnection
Public Sub New()
MyBase.New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'open picture
OpenFileDialog1.InitialDirectory = "E:\test code\picture"
OpenFileDialog1.DefaultExt = "jpg"
OpenFileDialog1.Filter = "Bmp Files(*.jpg)|*.jpg|Gif Files(*.gif)|*.gif|Jpg Files(*.bmp)|*.bmp"
OpenFileDialog1.ShowDialog()
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'insert record
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) Then
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 Sub DrawToScale(ByVal bmp As Image)
PictureBox1.Image = New Bitmap(bmp)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim i As String = InputBox("请输入名字:")
ShowImage(i)
End Sub
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("请输入要删除的名字:")
Dim cmd As New SqlClient.SqlCommand("delete from photos where name='" & s & "'", cn)
cmd.ExecuteNonQuery()
MsgBox("Image deleted")
cn.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Dispose()
End Sub
End Class
浙公网安备 33010602011771号