主题:求解 图片存入数据库的代码
jiagong
[专家分:20] 发布于 2006-03-10 10:12:00
我在做一个图片浏览的网页,目前遇到一个很大的问题,我不知道怎么把图片上传到SQL数据库。烦请,大家帮帮忙!小第感激不尽!
回复列表 (共2个回复)
沙发
大海兄 [专家分:50] 发布于 2006-05-13 08:43:00
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Public Class imageup
Inherits System.Web.UI.Page
Protected WithEvents upFile As HtmlInputFile
Protected fileType As String
Sub Page_Load()
End Sub
Sub btnSubmit_Click(ByVal s As Object, ByVal e As EventArgs)
Dim up_File As HttpPostedFile = upFile.PostedFile
Dim imgStream As Stream
Dim fileName As String
fileName = up_File.FileName
fileName = Path.GetFileName(fileName)
Dim length As Integer
length = up_File.ContentLength
If length = 0 Then
Response.Write("请选择图片")
End If
fileType = up_File.ContentType
Dim arrImage(length) As Byte
imgStream = up_File.InputStream
imgStream.Read(arrImage, 0, length)
'存入数据库
Dim consql As SqlConnection
Dim cmdsql As SqlCommand
Dim strsql As String
consql = New SqlConnection("Server=localhost;uid=sa;pwd=;database=同学录")
strsql = "Insert images(imgName,imgContent,imgType,imgLength)values(@name,@content,@type,@length)"
cmdsql = New SqlCommand(strsql, consql)
cmdsql.Parameters.Add("@name", SqlDbType.NVarChar).Value = fileName
cmdsql.Parameters.Add("@content", SqlDbType.Image).Value = arrImage
cmdsql.Parameters.Add("@type", SqlDbType.NVarChar).Value = fileType
cmdsql.Parameters.Add("@length", SqlDbType.Int).Value = length
Try
consql.Open()
cmdsql.ExecuteNonQuery()
consql.Close()
Catch ex As Exception
Response.Write("添加出错:" & ex.Message)
End Try
End Sub
End Class
板凳
02101208 [专家分:10] 发布于 2006-05-21 23:00:00
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn As SqlClient.SqlConnection
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) 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 Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button4.Click
Me.Dispose()
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
End Class
我来回复