Dim Con As New ADODB.Connection '定义数据库连接对象
Dim Rs As New ADODB.Recordset '定义数据库记录集对象
Private Sub Cmd_Delete_Click() '删除一条记录
Rs.Delete
If Not Rs.BOF Then
Rs.MoveNext
Else
Rs.MovePrevIoUs
End If
Call ShowInfo
End Sub
Private Sub Cmd_Exit_Click() '退出程序,关闭记录集对象Rs,关闭连接对象Con
Rs.Close
Con.Close
End
End Sub
Private Sub Cmd_Find_Click() '查询产品信息
Dim Str As String
Dim pname As String
pname = Text_PN.Text
If Text_PN.Text <> "" And Text_PriceDown.Text = "" And Text_PriceUp.Text = "" Then
Str = "Select * from producted Where pName='" & pname & "'"
ElseIf Text_PN.Text = "" And Text_PriceDown.Text <> "" And Text_PriceUp.Text <> "" Then
Str = "Select * from producted Where pPrice between '" & Val(Text_PriceDown.Text) & "' and '" & Val(Text_PriceUp.Text) & "'"
Else
Str = "Select * from producted Where pName= '" & pname & "' and pPrice between '" & Val(Text_PriceDown.Text) & "' and '" & Val(Text_PriceUp.Text) & "'"
End If
With Rs
If Rs.State = adStateOpen Then
Rs.Close
Rs.Open Str,Con,adOpenDynamic,adLockPessimistic
End If
ListShow.Clear
ListShow.AddItem "产品ID " & "产品名称 " & "产品价格 " & "产品生产日期"
Do While Not Rs.EOF
ListShow.AddItem Rs.Fields(0) & " " & Rs.Fields(1) & " " & Rs.Fields(2) & " " & Rs.Fields(3)
Rs.MoveNext
Loop
End With
End Sub
Private Sub Cmd_First_Click() '第一条信息
Rs.MoveFirst
Call ShowInfo
End Sub
Private Sub Cmd_Insert_Click() '增加产品
Rs.AddNew
Text_PID.Text = ""
Text_PName.Text = ""
Text_PPrice.Text = ""
Text_PDate.Text = ""
Call ShowInfo
End Sub
Private Sub Cmd_Last_Click() '最后一条信息
Rs.MoveLast
Call ShowInfo
End Sub
Private Sub Cmd_Next_Click() '下一条信息
If Not Rs.EOF Then
Rs.MoveNext
Call ShowInfo
End If
End Sub
Private Sub Cmd_Prev_Click() '前一条信息
If Not Rs.BOF Then
Rs.MovePrevIoUs
Call ShowInfo
End If
End Sub
Private Sub Cmd_Update_Click() '更新产品信息
If Not Rs.BOF And Not Rs.EOF Then
Rs.Fields(0) = Text_PID.Text
Rs.Fields(1) = Text_PName.Text
Rs.Fields(2) = Text_PPrice.Text
Rs.Fields(3) = Text_PDate.Text
End If
End Sub
Private Sub Form_Load() Con.ConnectionString = "Provider=sqlOLEDB;Persist Security Info=False;User ID=sa;PWD=密码;Initial Catalog=产品;Data Source=服务器名" '连接数据库字符串 Con.Open Con.CommandTimeout = 20 Rs.Open "producted",adLockPessimistic Call ShowInfo ListShow.AddItem "产品ID " & "产品名称 " & "产品价格 " & "产品生产日期" Do While Not Rs.EOF ListShow.AddItem Rs.Fields(0) & " " & Rs.Fields(1) & " " & Rs.Fields(2) & " " & Rs.Fields(3) Rs.MoveNext Loop End Sub Private Sub ShowInfo() '显示产品信息 If Not Rs.BOF And Not Rs.EOF Then Text_PID.Text = Rs.Fields(0) Text_PName.Text = Rs.Fields(1) Text_PPrice.Text = Str(Rs.Fields(2)) Text_PDate.Text = Rs.Fields(3) End If End Sub