0

我有一个vb.net 2010将注册表添加到 a 的应用程序MS Access Patient db,但有时它不会添加任何内容,直到我重置应用程序(关闭并再次运行它),这非常糟糕!

我要添加一些信息的表如下所示:

PatientId Proyect Email        RFC
-----------------------------------
1         firstP  firs@any.com  KEY
2         secondP sec@any.com   GTE
.... 

(项目是故意而不是项目)

我的表结构有一个自动增量 ID 值 (PatientID): 要知道我需要分配的下一个 id,我有一个函数

Public Function GenerateAutoID()
        Return Me._patientDataTable.Rows(Me._patientDataTable.Rows.Count - 1)("PatientID").ToString()
    End Function

我怀疑这是有时不允许向数据库插入新数据的错误...然后单击保存按钮时执行的功能是:

Public Sub Save()
        Dim query = String.Empty
        If Not _updateFlag Then
            ' save query
            query = "INSERT INTO Patient(Proyect,Email,RFC)"
            query &= " VALUES ('" & txtProyect.Text & "','" & txtEmail.Text & "','" & txtRFC.Text & "')"
            DatabaseFunctions.ExecuteQuery(query)
            MessageBox.Show("Data saved.", "System", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Else
            ' update query
            '... code that updates
        End If
        Clear()
        Reload()
    End Sub

代码包括Clear()仅重置表单中的所有文本字段, Reload()并且LoadDBData()

Public Sub Reload()
        SetToolTipsToButtons() 'this is for some fancy buttons
        LoadDBData()
        txtPatientID.Text = (Integer.Parse(GenerateAutoID()) + 1).ToString()
        FillListNames()
        SetAutoComplete()
    End Sub



Public Sub LoadDBData()

    Dim query As String = "SELECT * FROM Patient "
    Me._patientDataTable = DatabaseFunctions.GetDataTable(query)
    dgvInfo.Rows.Clear()

    For Each dtRow In Me._patientDataTable.Rows

        dgvInfo.Rows.Add()
        dgvInfo.Rows(dgvInfo.RowCount - 1).Cells("colPatientID").Value = dtRow("PatientID").ToString()
        dgvInfo.Rows(dgvInfo.RowCount - 1).Cells("colProyect").Value = dtRow("Proyect").ToString()
        dgvInfo.Rows(dgvInfo.RowCount - 1).Cells("colEmail").Value = dtRow("Email").ToString()
        dgvInfo.Rows(dgvInfo.RowCount - 1).Cells("colRFC").Value = dtRow("RFC").ToString()

    Next    
    dgvInfo.ClearSelection()

End Sub

处理 odbc 连接的类是:

Imports System.Data.Odbc
Imports System


Public Class DatabaseFunctions

    Shared con As OdbcConnection
    Public Shared Sub CreateConnection()
        con = New OdbcConnection
        'con.ConnectionString = "Dsn=XXX"
        con.ConnectionString = "Dsn=XXX;uid=sa;pwd=XXXxxx;"
        con.Open()
    End Sub

    Private Shared Sub CheckConnection()
        If con Is Nothing OrElse con.State = ConnectionState.Closed Then
            CreateConnection()
        End If
    End Sub

    Public Shared Function GetDataReader(ByVal SQL As String) As OdbcDataReader
        CheckConnection()
        Dim cmd As New OdbcCommand(SQL, con)
        Dim dr As OdbcDataReader
        dr = cmd.ExecuteReader
        Return dr
    End Function

    Public Shared Function GetDataTable(ByVal SQL As String) As DataTable
        CheckConnection()
        Dim cmd As New OdbcCommand(SQL, con)
        Dim table As New DataTable
        Dim da As New OdbcDataAdapter(cmd)
        da.Fill(table)
        Return table
    End Function

    Public Shared Sub ExecuteQuery(ByVal SQL As String)
        CheckConnection()
        Dim cmd As New OdbcCommand(SQL, con)
        cmd.ExecuteNonQuery()
    End Sub
End Class

以及表单加载时的代码:

 Private Sub frmPatientRegistration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Reload()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Systema", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

所以我想改变一点 save() 代码来知道表是否有相同数量的行m,如果它还有一行,一切都很好,但如果不是这样,再试一次......

我知道我正在考虑的这个解决方案并不优雅,但也许为了避免这种情况,问题出在代码的其他部分......但我认为它可能在自动增量中,或者与 Access db 文件有关?你有过这样的经历吗??

我已经调试了程序,它工作正常,只有当我将它作为独立应用程序运行时,有时它不会保存任何东西......直到我重新运行程序......

4

1 回答 1

1

没有close联系也没有

con.Close()
cmd.Dispose()
con.Dispose()

改变它:

Imports System.Data.Odbc
Imports System

Public Class DatabaseFunctions

    Shared con As OdbcConnection


    Public Shared Function GetDataReader(ByVal SQL As String) As OdbcDataReader
        Dim dr As OdbcDataReader
        'CheckConnection()
        Try
            con = New OdbcConnection
            con.ConnectionString = "Dsn=XXX;uid=sa;pwd=XXXxxx;"
            con.Open()
            Dim cmd As New OdbcCommand(SQL, con)
            dr = cmd.ExecuteReader
            con.Close()
            cmd.Dispose()
            con.Dispose()
            Return dr
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If (con.State = System.Data.ConnectionState.Open) Then
                con.Close()
                con.Dispose()
            End If
        End Try
        Return dr
    End Function

    Public Shared Function GetDataReaderVal(ByVal SQL As String) As String
        Dim resultado As String
        'CheckConnection()
        Try
            con = New OdbcConnection
            con.ConnectionString = "Dsn=XXX;uid=sa;pwd=XXXxxx;"
            con.Open()
            Dim cmd As New OdbcCommand(SQL, con)
            Dim dr As OdbcDataReader
            resultado = ""
            dr = cmd.ExecuteReader
            dr.Read()
            resultado = dr.GetValue(0).ToString
            con.Close()
            cmd.Dispose()
            con.Dispose()
            Return resultado
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Sistem", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If (con.State = System.Data.ConnectionState.Open) Then
                con.Close()
                con.Dispose()
            End If
        End Try
        resultado = "0"
        Return resultado
    End Function


    Public Shared Function GetDataTable(ByVal SQL As String) As DataTable
        Dim table As New DataTable
        'CheckConnection()
        Try
            con = New OdbcConnection
            con.ConnectionString = "Dsn=XXX;uid=sa;pwd=XXXxxx;"
            con.Open()
            Dim cmd As New OdbcCommand(SQL, con)

            Dim da As New OdbcDataAdapter(cmd)
            da.Fill(table)
            con.Close()
            cmd.Dispose()
            con.Dispose()
            Return table
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Sistem", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If (con.State = System.Data.ConnectionState.Open) Then
                con.Close()
                con.Dispose()
            End If
        End Try
        Return table
    End Function



    Public Shared Sub ExecuteQuery(ByVal SQL As String)
        'CheckConnection()
        Try
            con = New OdbcConnection
            con.ConnectionString = "Dsn=XXX;uid=sa;pwd=XXXxxx;"
            con.Open()
            Dim cmd As New OdbcCommand(SQL, con)
            cmd.ExecuteNonQuery()
            con.Close()
            cmd.Dispose()
            con.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Sistem", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If (con.State = System.Data.ConnectionState.Open) Then
                con.Close()
                con.Dispose()
            End If
        End Try
    End Sub
End Class
于 2012-12-02T16:58:24.880 回答