从vb.net代码上传sql server 2008中的图像文件时卡住了 [英] stuck while uploading an image file in sql server 2008 from vb.net code
问题描述
好的,这是我在这个网站上使用的代码。一切正常,但我需要一些不同的代码上传图像,我不知道该怎么办 -
这里是代码 -
ok so this is the code i used from this site. everything works fine, but i need a little different code to upload image, and i dont know what to do - here's the code -
Private Sub btnAttach_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAttach.Click
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer)
If iLength = 0 Then Exit Sub 'not a valid file
Dim sContentType As String = File1.PostedFile.ContentType
Dim sFileName As String, i As Integer
Dim bytContent As Byte()
ReDim bytContent(iLength) 'byte array, set to file size
'strip the path off the filename
i = InStrRev(File1.PostedFile.FileName.Trim, "\")
If i = 0 Then
sFileName = File1.PostedFile.FileName.Trim
Else
sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)
End If
Try
File1.PostedFile.InputStream.Read(bytContent, 0, iLength)
With cmdInsertAttachment
.Parameters("@FileName").Value = sFileName
.Parameters("@FileSize").Value = iLength
.Parameters("@FileData").Value = bytContent
.Parameters("@ContentType").Value = sContentType
.ExecuteNonQuery()
End With
Catch ex As Exception
'Handle your database error here
dbConn.Close()
End Try
Response.Redirect(Request.Url.ToString) 'Refresh page
End Sub
一切正常,除非涉及到这一部分 -
everything works fine except when it comes to this part -
With cmdInsertAttachment
.Parameters("@FileName").Value = sFileName
.Parameters("@FileSize").Value = iLength
.Parameters("@FileData").Value = bytContent
.Parameters("@ContentType").Value = sContentType
.ExecuteNonQuery()
End With
我没有这些参数。我在sql server表中只有1个字段,表示Img并且有image数据类型。我如何使用此代码使用此insert语句在db中获取我的图像?
插入table1(img)值(???)
i dont have these parameters. i only have 1 field in sql server table that says Img and has image datatype. how do i use this code to get my image in db using this insert statement? insert into table1 (img) values (???)
推荐答案
考虑将表更改为具有FileName ,FileSize,FileData和ContentType字段。
Consider changing your table to be have the FileName, FileSize, FileData and ContentType fields.
如果你真的不想存储在数据库中的名称,大小和内容类型,那么你可以改变
If you really don't want the name, size and content type stored in the database, then you can change
With cmdInsertAttachment
.Parameters("@FileName").Value = sFileName
.Parameters("@FileSize").Value = iLength
.Parameters("@FileData").Value = bytContent
.Parameters("@ContentType").Value = sContentType
.ExecuteNonQuery()
End With
到
With cmdInsertAttachment
.Parameters("@Img").Value = bytContent
.ExecuteNonQuery()
End With
在适当编辑cmdInsertAttachment存储过程之后,应该获取表中的数据。你之后可以用它做什么,但是有人猜不到。
And after the appropriate edit to the cmdInsertAttachment stored procedure that should get the data in the table. WHat you can do with it afterwards is anyone's guess though.
这篇关于从vb.net代码上传sql server 2008中的图像文件时卡住了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!