2010-01-27 36 views
0

好吧,這是我從這個網站使用的代碼。一切工作正常,但我需要一點點不同的代碼上傳圖片,我不知道該怎麼辦 - 這裏的代碼 -從vb.net上載圖像文件在sql server 2008中卡住代碼

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 

一切正常,當涉及到這部分除了 -

With cmdInsertAttachment 
     .Parameters("@FileName").Value = sFileName 
     .Parameters("@FileSize").Value = iLength 
     .Parameters("@FileData").Value = bytContent 
     .Parameters("@ContentType").Value = sContentType 
     .ExecuteNonQuery() 
    End With 

我沒有這些參數。我只有1個字段在sql server表中,表示Img並且有圖像數據類型。我如何使用此代碼使用此插入語句在db中獲取映像? insert into table1(img)values(???)

+0

如果您發佈了實際的表格結構,我們會更容易回答 – 2010-01-27 21:11:35

回答

0

考慮將表更改爲具有FileName,FileSize,FileData和ContentType字段。

如果你真的不想存儲在數據庫中的名稱,大小和內容類型,那麼你可以

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後更改存儲過程應該獲取表中的數據。儘管如此,你可以用它來做什麼是任何人的猜測。