看流星社区

 找回密码
 注册账号
查看: 2216|回复: 1

[VB] VB中怎么对数据库进行添加删除修改的操作

[复制链接]

该用户从未签到

发表于 2014-7-9 09:15:39 | 显示全部楼层 |阅读模式
我用的是DATA控件我已经把VB和ACCESS 2000连接上了,VB也能正常访问到数据了
我希望可以加几的按钮,拥有添加,删除,修改,查询的功能(相关的数据改动就在原本浏览数据的TEXT中录入就行了)

该用户从未签到

发表于 2014-7-9 09:15:54 | 显示全部楼层
Private Sub Command1_Click()
For i = 0 To 5
Text1(i).Text = ""
Next i
Adodc1.RecordSource = "select *  from " & s1 & " order by 编号"
Adodc1.Refresh
  If Adodc1.Recordset.RecordCount > 0 Then
    Adodc1.Recordset.MoveLast
    Text1(0).Text = "G" + Format((Val(Right(Trim(Adodc1.Recordset.Fields("编号")), 4)) + 1), "0000")
   Else
    Text1(0).Text = "G0001"
  End If
End Sub
Private Sub Command2_Click()
If Adodc1.Recordset.EOF = False Then
c = MsgBox("您确认要删除该记录吗?", vbOKCancel, "删除提示信息")
   If c = vbOK Then
    Adodc1.Recordset.Delete
    Adodc1.RecordSource = "select * from 人员表"
    Adodc1.Refresh
   End If
Else
   MsgBox "当前数据库中没有可删除的数据记录", vbOKOnly, "提示信息"
End If
End Sub
Private Sub Command3_Click()
If Text1(0).Text = "" Or Text1(1).Text = "" Then
   MsgBox "请选择需要改动的记录信息!", vbOKOnly, "错误提示"
Else
   c = MsgBox("确定要修改该记录吗?", vbOKCancel, "提示信息")
       If c = vbOK Then                                   '如果确认修改的话进行修改操作
               If Text1(1).Text = "" Then
                  MsgBox "姓名不能为空值!", 48, "修改信息提示"
               Else
                            '连接所要修改的数据库
                             con.Open "Provider=SQLOLEDB.1;Password=2752;Persist Security Info=True;User ID=sa;Initial Catalog=sample;Data Source=JAMLEEPC"
                            '开始修改数据库
                            con.Execute ("UPDATE " & s1 & "  SET 姓名='" & Text1(1).Text & "',年龄=" & Text1(2).Text & ",学历='" & Text1(3).Text & "',年级=" & Text1(4).Text & ", 入学时间='" & Text1(5).Text & "'  where 编号='" & Trim(Text1(0)) & "'")
                             MsgBox "信息修改成功", 64, "修改信息提示"
                            con.Close
             Adodc1.RecordSource = "select * from 人员表"
             Adodc1.Refresh
               End If
        End If
End If
End Sub
Private Sub Command4_Click()
Adodc1.RecordSource = "select * from 人员表  where 编号='" + Text1(0).Text + "'"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
   MsgBox "该信息已存在,信息保存不成功", 64, "保存信息提示"
Else
   cc = MsgBox("您确定要保存该信息吗?", 33, "信息保存提示")
    If cc = vbOK Then
        If Text1(1).Text = "" Or Text1(2).Text = "" Or Text1(3).Text = "" Or Text1(4).Text = "" Or Text1(5).Text = "" Then
           MsgBox "请确认,人员的姓名、年龄、学历、年级和入学时间不能为空", 48, "保存信息提示"
        Else
         con.Open "Provider=SQLOLEDB.1;Password=2752;Persist Security Info=True;User ID=sa;Initial Catalog=sample;Data Source=JAMLEEPC"
         con.Execute ("insert into 人员表  values('" & Text1(0).Text & "','" & Text1(1).Text & "','" & Text1(2).Text & "','" & Text1(3).Text & "','" & Text1(4).Text & "', '" & Text1(5).Text & "')")
         MsgBox "信息修改成功", 64, "修改信息提示"
         con.Close
         Adodc1.RecordSource = "select * from 人员表"
         Adodc1.Refresh
        End If
     Else
     End If
End If
Set DataGrid1.DataSource = Adodc1
End Sub
给你段代码参考下,这个就是添加,删除,修改的代码。不过我用的是SQL2000你要吧代码稍微修改一下的,呵呵,祝你成功!
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-4-19 09:21

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表