将映像存入SQL DB [英] Sav an Image into SQL DB
本文介绍了将映像存入SQL DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
您能帮助我学习如何使用VB.NET
Hello every one
can you help me to learn how can i Add an image to my SQL Database using VB.NET
推荐答案
将图像添加到SQL数据库中吗? 使用VB从SQL Server数据库中保存和检索图像. NET [ ^ ]
Refer :
Save and Retrieve Image from a SQL Server Database using VB.NET[^]
Protected Sub btnUpload_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim strImageName As String = txtName.Text.ToString()
If FileUpload1.PostedFile IsNot Nothing AndAlso
FileUpload1.PostedFile.FileName <> "" Then
Dim imageSize As Byte() = New Byte
(FileUpload1.PostedFile.ContentLength - 1) {}
Dim uploadedImage__1 As HttpPostedFile =
FileUpload1.PostedFile
uploadedImage__1.InputStream.Read(imageSize, 0,
CInt(FileUpload1.PostedFile.ContentLength))
' Create SQL Connection
Dim con As New SqlConnection()
con.ConnectionString =
ConfigurationManager.ConnectionStrings
("ConnectionString").ConnectionString
' Create SQL Command
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Images
(ImageName,Image) VALUES (@ImageName,@Image)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
Dim ImageName As New SqlParameter
("@ImageName", SqlDbType.VarChar, 50)
ImageName.Value = strImageName.ToString()
cmd.Parameters.Add(ImageName)
Dim UploadedImage__2 As New SqlParameter
("@Image", SqlDbType.Image, imageSize.Length)
UploadedImage__2.Value = imageSize
cmd.Parameters.Add(UploadedImage__2)
con.Open()
Dim result As Integer = cmd.ExecuteNonQuery()
con.Close()
If result > 0 Then
lblMessage.Text = "File Uploaded"
End If
GridView1.DataBind()
End If
End Sub
这篇关于将映像存入SQL DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文