如何仅从数据库更新一个datagridview列

时间:2014-07-05 20:18:12

标签: vb.net

我有一个datagridiview控件,其中一个图像列连接到sql数据库。我想在运行时更改图片,写入数据库,同时从数据库中检索图像以显示在datagridview单元格上。现在,当我点击图像单元格时,我弹出一个浏览器图像,写入数据库,但我无法检索更新的图像而没有得到其他所有。关于如何做到这一点的任何想法?下面的代码加载整个表 非常感谢

 Public Sub GetImagesFromDatabase()
    Try
        'Initialize SQL Server connection.
        _Connection.Open()

        'Initialize SQL adapter.
        Dim ADAP As New SqlDataAdapter("Select pic from tblimgData", _Connection)

        'Initialize Dataset.
        Dim DS As New DataSet()

        'Fill dataset with ImagesStore table.
        ADAP.Fill(DS, "tblimgData")


        DataGridView1.DataSource = DS.Tables("tblimgData")
        _Connection.Close()
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

导入System.Data.SqlClient

Public Sub GetImagesFromDatabase()
        Dim cmd As New SqlCommand(Sql, cn)' sql: the sql query string
' cn : Initialized SQL Server connection.
    Try

        cmd.Connection.Open()
        dim dr as SqlDataReader
dr=cmd.ExecuteReader()
while not read
  DataGridView1.rows(index).cells(index) = dr.getstring()
  ADAP.Fill(DS,dr.getstring())
end while

        _Connection.Close()
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try

End Sub