开发者

stuck while uploading an image file in sql server 2008 from vb.net code

开发者 https://www.devze.com 2022-12-18 02:43 出处:网络
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 -

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 file开发者_Python百科name 
    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 

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 (???)


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 

to

With cmdInsertAttachment 
    .Parameters("@Img").Value = bytContent 
    .ExecuteNonQuery() 
End With 

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消