从db到datagridview检索数据

时间:2017-03-02 22:36:52

标签: sql-server vb.net visual-studio

Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class StudDetails

    Dim con As New SqlConnection
    Dim sdp As SqlDataAdapter 'help get data from db connect datagridview with database table'
    Dim ds As DataSet  
    Dim dtable As DataTable 
    Dim query As String
    Dim COMMAND As SqlCommand

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles   Button1.Click

        con = New SqlConnection
        con.ConnectionString = "Data Source=DESKTOP-6RSFF1M;Initial Catalog=StudNew ;Integrated Security=True"
        Dim bsource As New BindingSource
        Try
            con.Open()
            query = "SELECT EnrollmentNo,Name,[Subject Name1],[Out Of1],          [Marks Obtained1] from StudNew.Sem1M1 where EnrollmentNo = '" & TextBox1.Text   & "' "
            COMMAND = New SqlCommand(query, con)
            sdp.SelectCommand = COMMAND
            sdp.Fill(dtable)
            bsource.DataSource = dtable
            DataGridView2.DataSource = bsource
            sdp.Update(dtable)
            con.Close()

        Catch ex As Exception

        End Try
    End Sub
End Class

这是用于从名为StudNew的数据库中检索数据的mycode和名为Sem1M1的表。我还将datagridview与我的数据库绑定,并将我的表单连接到数据库。不知道为什么数据不会出现在gridview中。

0 个答案:

没有答案