-1

如何使用 vb 语言和 SQL server 2008 在 ASP.net 中上传和保存图像。

4

1 回答 1

1

您可以使用本教程

将图像保存在数据库中的链接的一部分:

Private Sub Button2_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox1.Text = "" Then
        MsgBox("Fill the Name Field")
    Else
        Dim sql As String = "INSERT INTO Information VALUES(@name,@photo)"
        Dim cmd As New SqlCommand(sql, con)
        cmd.Parameters.AddWithValue("@name", TextBox1.Text)
        Dim ms As New MemoryStream()
        PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
        Dim data As Byte() = ms.GetBuffer()
        Dim p As New SqlParameter("@photo", SqlDbType.Image)
        p.Value = data
        cmd.Parameters.Add(p)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK)
        Label1.Visible = False
        TextBox1.Visible = False
    End If
End Sub

这是图像检索的方式:

Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As _
            System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
    cmd = New SqlCommand("select photo from Information where name='" & _
              DataGridView1.CurrentRow.Cells(0).Value() & "'", con)
    Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
    If Not imageData Is Nothing Then
        Using ms As New MemoryStream(imageData, 0, imageData.Length)
            ms.Write(imageData, 0, imageData.Length)
            PictureBox1.BackgroundImage = Image.FromStream(ms, True)
        End Using
    End If
    GroupBox2.SendToBack()
    GroupBox2.Visible = False
    Label1.Visible = True
    Label1.Text = DataGridView1.CurrentRow.Cells(0).Value()
End Sub

比较your findingsthis sample探索你所缺少的。

于 2012-12-03T10:26:39.650 回答