SQlDataAdapter连接尚未初始化

时间:2015-11-12 12:43:35

标签: vb.net

也许是简单的问题,但无法搞清楚。在调用GetRodzaje方法将填满我的网格之后,还有一个按钮可以点击以将更改保存回数据库,如果我点击它我的第二种方法GetRodzajeMakeChanges给我一个错误,就像它会丢失与数据库的连接,但我打开那里的联系。

connection has not been initialized

为什么会在我打开连接时发生这种情况?你能支持吗?:

这是我的代码:

Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports DataAccessLayer

Public Class ProjektDAO

    Private GetRodzajeDataSet As New DataSet
    Private GetRodzajeDataAdapter As SqlDataAdapter
    Private MyConnection As SqlConnection

    Private strcon = New AppSettingsReader().GetValue("ConnectionString", GetType(System.String)).ToString()

    Public Function GetRodzaje() As DataSet

        Using MyConnection = New SqlConnection(strcon)
            Dim cmd As New SqlCommand("SELECT * FROM tbProjekt", MyConnection)
            MyConnection.Open()
            GetRodzajeDataAdapter = New SqlDataAdapter(cmd)
            GetRodzajeDataAdapter.Fill(GetRodzajeDataSet, "trial1")
        End Using

        Return GetRodzajeDataSet

    End Function


    Public Sub GetRodzajeMakeChanges()
        If Not GetRodzajeDataSet.HasChanges Then
            MessageBox.Show("Nie ma zmian do zapisania", "Informacja", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else

            Dim cmdbuilder As New SqlCommandBuilder(GetRodzajeDataAdapter)
            Dim i As Integer
            Try
                Using MyConnection = New SqlConnection(strcon)
                    MyConnection.Open()
                    i = GetRodzajeDataAdapter.Update(GetRodzajeDataSet, "trial1")
                End Using

                MsgBox("Zaktualizowano " & i & " rekordów")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Sub


    Public Function GetRodzajeCheckChanges() As Boolean
        Dim result As Boolean = False
        If GetRodzajeDataSet.HasChanges Then
            result = True
        End If
        Return result
    End Function



End Class

这一个正在工作,但是...我在下面的代码没有关闭任何地方的连接,不使用conenction使用条款,所以我认为它不正确,请帮助我,我该怎么办?使用下面的代码或首先使用一些修复程序?:

Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports DataAccessLayer

Public Class ProjektDAO

    Private GetRodzajeDataSet As New DataSet
    Private GetRodzajeDataAdapter As SqlDataAdapter

    Private strcon = New AppSettingsReader().GetValue("ConnectionString", GetType(System.String)).ToString()

    Public Function GetProjects() As DataSet
        Dim con As New SqlConnection(strcon)
        Dim cmd As New SqlCommand("SELECT * FROM tbProjekt", con)
        con.Open()
        GetRodzajeDataAdapter = New SqlDataAdapter(cmd)
            GetRodzajeDataAdapter.Fill(GetRodzajeDataSet, "trial1")


        Return GetRodzajeDataSet

    End Function


    Public Sub GetProjectMakeChanges()
        If Not GetRodzajeDataSet.HasChanges Then
            MessageBox.Show("Nie ma zmian do zapisania", "Informacja", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else

            Dim cmdbuilder As New SqlCommandBuilder(GetRodzajeDataAdapter)
            Dim i As Integer
            Try


                i = GetRodzajeDataAdapter.Update(GetRodzajeDataSet, "trial1")


                MsgBox("Zaktualizowano " & i & " rekordów")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Sub


    Public Function GetProjectCheckChanges() As Boolean
        Dim result As Boolean = False
        If GetRodzajeDataSet.HasChanges Then
            result = True
        End If
        Return result
    End Function

1 个答案:

答案 0 :(得分:1)

在类级别上有一些字段(GetRodzajeDataAdapter,MyConnection)应该移动到类级别以便具有一致的生命周期。现在,您的代码从不在类级别使用MyConnection字段,但始终在方法范围内创建新变量。我将更改代码如下:

' ...
Public Class ProjektDAO

    Private GetRodzajeDataSet As New DataSet
    ' Remove this:  Private GetRodzajeDataAdapter As SqlDataAdapter
    ' Remove this: Private MyConnection As SqlConnection

    Private strcon = New AppSettingsReader().GetValue("ConnectionString", GetType(System.String)).ToString()

    Public Function GetRodzaje() As DataSet

        Using MyConnection = New SqlConnection(strcon)
            Using cmd As New SqlCommand("SELECT * FROM tbProjekt", MyConnection)
                MyConnection.Open()
                ' Create a data adapter in the method and throw it away afterwards
                Using GetRodzajeDataAdapter = New SqlDataAdapter(cmd)
                    GetRodzajeDataAdapter.Fill(GetRodzajeDataSet, "trial1")
                End Using
            End Using
        End Using

        Return GetRodzajeDataSet

    End Function


    Public Sub GetRodzajeMakeChanges()
        If Not GetRodzajeDataSet.HasChanges Then
            MessageBox.Show("Nie ma zmian do zapisania", "Informacja", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else

            Dim i As Integer
            Try
                Using MyConnection = New SqlConnection(strcon)
                    Using cmd As New SqlCommand("SELECT * FROM tbProjekt", MyConnection)
                        MyConnection.Open()
                        ' Create a data adapter in the method and throw it away afterwards
                        Using GetRodzajeDataAdapter = New SqlDataAdapter(cmd)
                            Dim cmdbuilder As New SqlCommandBuilder(GetRodzajeDataAdapter)
                            i = GetRodzajeDataAdapter.Update(GetRodzajeDataSet, "trial1")
                        End Using
                    End Using
                End Using

                MsgBox("Zaktualizowano " & i & " rekordów")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Sub
    ' ...
End Class

这样,始终在方法中创建连接和数据适配器。另外,我在第二种方法中将数据适配器的Using块中的命令构建器移动到了生成插入,更新和删除的命令。