Part I 创建数据库
代码:
CREATE TABLE imgProcess(
pic_id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
pic_title VARCHAR(50) NULL,
pic_type VARCHAR(50) NULL,
pic_content IMAGE NULL
)
Part II 图片上传Web Server及在数据库存入路径
代码(.aspx)
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtTitle" runat="server"></asp:TextBox><br>
<INPUT type="file" id="myImageFile" runat="server">
<asp:Button id="btnUpload" runat="server" Text="Upload" Height="23px"></asp:Button><br>
<br>
<asp:HyperLink id="hlinkShow" runat="server" NavigateUrl="ShowAllPic.aspx">ShowAllPic</asp:HyperLink>
</form>
代码(.vb)
Private Sub btnUpload_Click()Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim imageType As String
Dim imageSize As Int32
imageType = myImageFile.PostedFile.ContentType
imageSize = myImageFile.PostedFile.ContentLength
If (imageSize = 0) Then
Response.Write("Not Null")
Return
End If
If (imageSize > 300000) Then
Response.Write("Too big")
Return
End If
Dim filesplit() As String = Split(imageType, "/")
imageType = filesplit(filesplit.Length - 1)
If Not (imageType.ToLower.Equals("pjpeg") Or imageType.ToLower.Equals("gif")) Then
Response.Write("Format Error")
End If
filesplit = Split(myImageFile.PostedFile.FileName, "\")
Dim fileName As String = filesplit(filesplit.Length - 1)
myImageFile.PostedFile.SaveAs(Server.MapPath("source\images\" & fileName))
Dim imagePath As String = "source\images\" & fileName
Dim img As String = "<img src=" & imagePath & " border=0>"
Dim dbAccess As New CommonDB
Try
dbAccess.OpenConnection()
dbAccess.Command.CommandText = "INSERT INTO imgProcess(pic_title,pic_type) VALUES( '" & Me.txtTitle.Text.Trim & "','" & img & "')"
dbAccess.Command.ExecuteNonQuery()
Response.Write("Success")
Catch ex As Exception
Response.Write(ex.ToString)
Finally
dbAccess.CloseConnection()
End Try
End Sub
Part III 图片显示
代码(.aspx)
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="Grid1" runat="server" />
</form>
代码(.vb)
Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dbAccess As New CommonDB
Grid1.DataSource = dbAccess.QueryData("SELECT pic_title,pic_type FROM imgProcess", "temp")
Grid1.DataBind()
End Sub