0

任何人都可以帮助我,我有一个表格,我通过它在我的 ms 访问数据库中保存日期。有时我单击保存按钮数据正确保存但有时它给我错误“溢出”'ExecuteNonQuery'未声明。由于其保护级别,它可能无法访问。 我正在使用这段代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If MsgBox("Are you sure you want to Add Data?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then
        Dim OleDBC As New OleDbCommand
        With OleDBC

            .Connection = conn
            .CommandText = "Insert Into tblmaritlistBA_I (form_number,name_of_candidate,fathers_name,mothers_name,category,minority,date_of_birth,gender,mobile,address,board,passed_year,intermediate_marks_obtained,intermediate_total_marks,percentage,normalization_factor,total_percentage)  VALUES ('" & txtformnumber.Text & "','" & txtstuname.Text & "','" & txtfathname.Text & "','" & txtmothname.Text & "','" & cmbcategory.Text & "','" & cmbminority.Text & "','" & dobPicker1.Text & "','" & cmbgender.Text & "','" & txtmobile.Text & "','" & txtaddress.Text & "','" & cmbboard.Text & "','" & cmbpassedyear.Text & "','" & txtintermarks.Text & "','" & txtintertotalmarks.Text & "','" & txtpercentage.Text & "','" & Lblnormalization.Text & "','" & txtpercentageafterN.Text & "')"
            .ExecuteNonQuery()
        End With
        MsgBox("Data Added!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "SUCCESS")
        Me.Hide()

        Call initgrid()
    End If
End Sub 

请帮助我(“_”)

4

1 回答 1

1

根据您的代码,您从未打开过数据库连接。

您设置connCommand对象的连接,但您从未先实例化它也没有打开它。

请参阅下面的代码。Connection创建对象(放入您自己的连接字符串),然后将其打开。

最后,必须在一个Finally块中关闭该连接,以便即使出现错误我们也可以关闭它

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

    Dim conn As OleDbConnection
    Try
        If MsgBox("Are you sure you want to Add Data?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then

            conn = New OleDbConnection("YOUR_CONNECTIONSTRING_HERE")
            conn.Open()

            Dim OleDBC As New OleDbCommand

            With OleDBC
                .Connection = conn
                .CommandText = "Insert Into tblmaritlistBA_I (form_number,name_of_candidate,fathers_name,mothers_name,category,minority,date_of_birth,gender,mobile,address,board,passed_year,intermediate_marks_obtained,intermediate_total_marks,percentage,normalization_factor,total_percentage)  VALUES ('" & txtformnumber.Text & "','" & txtstuname.Text & "','" & txtfathname.Text & "','" & txtmothname.Text & "','" & cmbcategory.Text & "','" & cmbminority.Text & "','" & dobPicker1.Text & "','" & cmbgender.Text & "','" & txtmobile.Text & "','" & txtaddress.Text & "','" & cmbboard.Text & "','" & cmbpassedyear.Text & "','" & txtintermarks.Text & "','" & txtintertotalmarks.Text & "','" & txtpercentage.Text & "','" & Lblnormalization.Text & "','" & txtpercentageafterN.Text & "')"
                .ExecuteNonQuery()
            End With

            MsgBox("Data Added!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "SUCCESS")
            Me.Hide()

            Call initgrid()
        End If

    Catch ex As Exception
        MsgBox("Error : " & ex.ToString)
    Finally
        If (conn.state and ConnectionState.Open) <>0 Then 
            conn.Close 
        End If
    End Try

End Sub
于 2012-05-31T03:23:42.750 回答