VB.net:MS Access-第二次添加记录而不关闭调试应用程序的问题?

时间:2015-08-29 07:08:53

标签: vb.net ms-access-2010

我使用带有MS Access DB的VB.net开发了一个Windows应用程序。我在第二次/更多时间添加记录时遇到一个问题。我的第一次添加,它正确地在DB中添加了一次。在第二次添加新记录时,它添加了两次。 第三次添加时,之前的记录代替了新的记录。完成后,我看到了我的数据库,它看起来像那样(它只是一个例子,不是我的数据库,因为它太大了)

jo 18 USA 15000             //first records
ji 19 UAE 12000             //second record  
ji 19 UAE 12000             //extra second record   
ji 19 UAE 12000             //second record in place third

这是我的添加代码:

Public Class GPE_Addstaff
    Dim db As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.accdb")
    Dim cmd As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim ds As New DataSet
    Private Sub addnew_Click(sender As System.Object, e As System.EventArgs) Handles addnew.Click
        If empname.Text <> "" And addressline1.Text <> "" And addressline2.Text <> "" And city.Text <> "" And pincode.Text <> "" And phone.Text <> "" And email.Text <> "" And designation.Text <> "" And empid.Text <> "" And salary.Text <> "" And workingdays.Text <> "" And pf.Text <> "" And esi.Text <> "" And netpay.Text <> "" And bankname.Text <> "" And accountno.Text <> "" And salary.Text <> "" Then
            db.Open()
            cmd.Connection = db
            cmd.CommandText = "select *from staffdetails where [employee_ID]='" & empid.Text & "' "
            cmd.ExecuteNonQuery()
            da.SelectCommand = cmd
            da.Fill(ds)
            If ds.Tables(0).Rows.Count = 0 Then
                cmd.Connection = db
                cmd.CommandText = "insert into staffdetails values(@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n,@o,@p)"
                cmd.Parameters.AddWithValue("a", empname.Text)
                cmd.Parameters.AddWithValue("b", addressline1.Text)
                cmd.Parameters.AddWithValue("c", addressline2.Text)
                cmd.Parameters.AddWithValue("d", city.Text)
                cmd.Parameters.AddWithValue("e", pincode.Text)
                cmd.Parameters.AddWithValue("f", phone.Text)
                cmd.Parameters.AddWithValue("g", email.Text)
                cmd.Parameters.AddWithValue("h", designation.Text)
                cmd.Parameters.AddWithValue("i", empid.Text)
                cmd.Parameters.AddWithValue("j", salary.Text)
                cmd.Parameters.AddWithValue("k", workingdays.Text)
                cmd.Parameters.AddWithValue("l", pf.Text)
                cmd.Parameters.AddWithValue("m", esi.Text)
                cmd.Parameters.AddWithValue("n", netpay.Text)
                cmd.Parameters.AddWithValue("o", bankname.Text)
                cmd.Parameters.AddWithValue("p", accountno.Text)
                cmd.ExecuteNonQuery()
                MsgBox("New staff database Added successfully")
                ds.Clear()
                da.Dispose()
                db.Close()
                empname.Text = ""
                addressline1.Text = ""
                addressline2.Text = ""
                phone.Text = ""
                email.Text = "           @   .com"
                empid.Text = ""
                salary.Text = 0
                esi.Text = 0
                pf.Text = 0
                netpay.Text = 0
                bankname.Text = ""
                accountno.Text = "AC000000000"
            Else
                MsgBox("Employee_ID already exists")
            End If
        Else
            MsgBox("please fill all the details")
        End If
        db.Close()
    End Sub
End Class

0 个答案:

没有答案