Imports System.Data.SqlClient Imports System.IO Imports System.Data Public Class Image Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim imageInfo As FileInfo = New FileInfo(Me.File1.value.Trim()) If imageInfo.Exists() = False Then Me.RegisterClientScriptBlock("alertMsg", "<script>alert('please select one image file.');</script>") Else Select Case (imageInfo.Extension.ToUpper()) Case ".JPG" : UpLoadImageFile(Me.File1.Value.Trim()) Case ".GIF" : UpLoadImageFile(Me.File1.Value.Trim()) Case ".BMP" : UpLoadImageFile(Me.File1.Value.Trim()) 'default: RegisterClientScriptBlock("alertMsg", "<script>alert('file type error.');</script>") End Select End If End Sub Private Sub UpLoadImageFile(ByVal info As String) Dim objConn As SqlConnection Dim objCom As SqlCommand Try Dim imagestream As FileStream = New FileStream(info, FileMode.Open) Dim data() As Byte ReDim data(imagestream.Length - 1) imagestream.Read(data, 0, imagestream.Length) imagestream.Close() objConn = New SqlConnection("loginConnectionString") objCom = New SqlCommand("insert into img(pic)values(@Pic)", objConn) Dim pictureParameter As SqlParameter = New SqlParameter("@Pic", SqlDbType.Image) pictureParameter.Value = data objCom.Parameters.Add(pictureParameter) objConn.Open() objCom.ExecuteNonQuery() objConn.Close() Catch ex As Exception Throw New Exception(ex.Message) objConn.Close() End Try End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)