在vb中从sqlserver向datagridview加载数据

hivapdat  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(320)

正在将数据从sql server加载到datagridview中,但警告1变量“dtAppliantLists”在赋值之前已被使用。空引用异常可能导致运行时异常。DTAppliantLists.load处的绿色下划线(读卡器)
任何帮助,请。。。

Private Function GetList() As DataTable

        Dim dtApplicantLists As DataTable

        Dim connString As String = ConfigurationManager.ConnectionStrings("dbx").ConnectionString

        Using conn As New SqlConnection(connString)

            Using cmmd As New SqlCommand("SELECT FirstName, LastName, Gender, ChosenProg, Aggregate FROM dbo.Applicants", conn)

                conn.Open()

                Dim reader As SqlDataReader = cmmd.ExecuteReader()

                dtApplicantLists.Load(reader)

            End Using

        End Using

        Return dtApplicantLists

    End Function
57hvy0tb

57hvy0tb1#

您需要调用dtapplicationlists=newdatatable—当前它为null(或在vb中为nothing)。

ufj5ltwl

ufj5ltwl2#

Using ... End Using 方法将保证您不必担心像您得到的这样的警告,正如您的代码中明显演示的那样。

Private Function GetList() As DataTable
        Dim SqlStr As String =
            ("SELECT FirstName, LastName, Gender, ChosenProg, Aggregate FROM dbo.Applicants")
        Using dtApplicantLists As DataTable = New DataTable
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("dbx").ConnectionString),
                    Cmd As New SqlCommand(SqlStr, conn)
                conn.Open()
                Using Reader As SqlDataReader = Cmd.ExecuteReader
                    dtApplicantLists.Load(Reader)
                End Using
            End Using
            Return dtApplicantLists
        End Using
    End Function
pw9qyyiw

pw9qyyiw3#

你可以这样做。

Imports System.Data.SqlClient
Public Class Form1
    Dim connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    Dim cmdBuilder As SqlCommandBuilder
    Dim ds As New DataSet
    Dim changes As DataSet
    Dim sql As String
    Dim i As Int32

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        connection = New SqlConnection(connetionString)
        Sql = "select * from Product"
        Try
            connection.Open()
            adapter = New SqlDataAdapter(Sql, connection)
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.Data Source= ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            cmdBuilder = New SqlCommandBuilder(adapter)
            changes = ds.GetChanges()
            If changes IsNot Nothing Then
                adapter.Update(changes)
            End If
            MsgBox("Changes Done")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

请参阅下面的链接,了解其他一些类似但略有不同的选项。
http://vb.net-informations.com/dataadapter/dataadapter-datagridview-sqlserver.htm

相关问题