这是我的代码问题是,每次运行它时都会说数据已经存在,并在下面的即时窗口中显示一条消息(System.Data中发生类型System.Data.OleDb.OleDbException
的第一次机会异常)。 dll)目前我们在学校使用vb 10 express并且在访问中使用12.0并不是很好用
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Dim Com As New OleDbCommand
Dim ComInsert As New OleDbCommand
Dim ComUpdate As New OleDbCommand
Dim ComDelete As New OleDbCommand
Dim aAdapter As New OleDbDataAdapter
Dim Dset As New DataSet
'Dim dRead As New OleDbDataReader
Private Sub FrmCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ConProvider As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\richardnew.mdb"
Try
con.ConnectionString = ConProvider
If Not con.State = ConnectionState.Open Then
con.Open()
MsgBox("Connection Established")
End If
Catch ex As Exception
MsgBox("No Connection Established")
End Try
ListView1.Columns.Add("Customer No", 100, HorizontalAlignment.Left)
ListView1.Columns.Add("Name", 300, HorizontalAlignment.Left)
ListView1.Columns.Add("City", 300, HorizontalAlignment.Left)
ListView1.View = View.Details
ListView1.GridLines = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Try
If Not con.State = ConnectionState.Open Then
con.Open()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "ERROR CONNECTION")
End Try
If TextBox3.Text = "" Or TextBox2.Text = "" Then
MsgBox("All Fields are required, Check the fields", MsgBoxStyle.Information, "Required Fiels")
Exit Sub
End If
With aAdapter
.SelectCommand = New OleDb.OleDbCommand()
.SelectCommand.CommandText = "select * from [customer] where cust-no = '" & TextBox1.Text & "'"
.SelectCommand.Connection = con
End With
Dim dataRead As OleDb.OleDbDataReader
dataRead = aAdapter.SelectCommand.ExecuteReader()
If Not dataRead.HasRows Then
ComInsert.CommandText = "INSERT INTO customer(cust-no,cust-name,cust-city)" & _
"VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')"
ComInsert.Connection = con
ComInsert.ExecuteNonQuery()
MsgBox(TextBox1.Text & " has been added to the record", MsgBoxStyle.Information, "NEW RECORD")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
aAdapter.Dispose()
ComInsert.Dispose()
Else
MsgBox("WARNING1: Customer Already Exist in the Record!!", MsgBoxStyle.Exclamation, "ERROR SAVING DATA")
End If
Catch ex As Exception
MsgBox("WARNING2: Customer Number Already Exist in the Record!!", MsgBoxStyle.Exclamation, "ERROR SAVING DATA")
TextBox1.Focus()
Exit Sub
End Try
Call lvpop()
End Sub
Public Sub lvpop()
ListView1.Clear()
ListView1.Items.Clear()
With ListView1
.Columns.Add("Customer No", 100, HorizontalAlignment.Left)
.Columns.Add("Name", 300, HorizontalAlignment.Left)
.Columns.Add("City", 300, HorizontalAlignment.Left)
.View = View.Details
.GridLines = True
End With
With aAdapter
.SelectCommand = New OleDb.OleDbCommand()
.SelectCommand.CommandText = "select * from [customer] where cust-no = '" & TextBox1.Text & "'"
.SelectCommand.Connection = con
End With
Dim dataRead As OleDb.OleDbDataReader
dataRead = aAdapter.SelectCommand.ExecuteReader()
Do While dataRead.Read()
Dim Item As New ListViewItem
Item.Text = IIf(dataRead.IsDBNull(0), "", dataRead.Item(0))
For sItems = 1 To dataRead.FieldCount() - 1
If Not dataRead.IsDBNull(sItems) Then
Item.SubItems.Add(dataRead.Item(sItems))
Else
Item.SubItems.Add("")
End If
Next sItems
Me.ListView1.Items.Add(Item)
Loop
End Sub
End Class