| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1049 人关注过本帖
标题:ADO记录集的值不正确,请问怎么回事?
只看楼主 加入收藏
liuvking
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2009-7-22
结帖率:87.5%
收藏
已结贴  问题点数:0 回复次数:4 
ADO记录集的值不正确,请问怎么回事?
我用ADO连接数据库,代码如下:
Private Sub cmdrefresh_Click()
cmdrefresh.Enabled = False


SQL = "select * from 学生信息表 order by ID"
rs.Close
rs.Open SQL, conn, 3, 3


Set DataGrid1.DataSource = rs
Textid.Text = rs("ID")
Textname.Text = rs!姓名 & ""
Textsex.Text = rs!性别 & ""
Textxuehao.Text = rs!学号 & ""
Textsf.Text = rs!身份证号 & ""
Textzy.Text = rs!专业 & ""
Textaddr.Text = rs!住址 & ""
Textyb.Text = rs!邮编 & ""
Textht.Text = rs!合同号 & ""
Textbiye.Text = rs!毕业 & ""
Textfirst.Text = rs!第一次划款 & ""
Textfirsttime.Text = rs!第一次划款时间 & ""
Textsecond.Text = rs!第二次划款 & ""
Textsecondtime.Text = rs!第二次划款时间 & ""
Textthird.Text = rs!第三次划款 & ""
Textthirdtime.Text = rs!第三次划款时间 & ""
Textforth.Text = rs!第四次划款 & ""
Textforthtime.Text = rs!第四次划款时间 & ""


Labsit.Caption = "状态:正常"
cmdrefresh.Enabled = True
End Sub
但是,文本框中显示的记录,前几项都正确,均为第一条记录的值,但红色部分的几个字段的值,却都是最后一条记录的值,请问这是怎么回事?有谁知道吗?
搜索更多相关主题的帖子: 记录 ADO 
2009-12-20 23:14
三断笛
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:31
帖 子:1621
专家分:1617
注 册:2007-5-24
收藏
得分:3 
不可能的。 发代码和数据库上来
2009-12-21 10:15
liuvking
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2009-7-22
收藏
得分:0 
回复 2楼 三断笛
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public SQL As String
Public db As Database
Public temp As Integer

Private Sub Form_Load()
Dim sum As Double
Dim textfirst1 As Double
Dim textsecond1 As Double
Dim textthird1 As Double
Dim textforth1 As Double
sum = 0
I = 0

cover.Show
Set db = Workspaces(0).OpenDatabase(App.Path & "\db2.mdb", False, False, "MS Access;PWD=ZXDK")
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db2.mdb;Persist Security Info=False;Jet OLEDB:Database Password=ZXDK" '设置数据库路径
conn.CursorLocation = adUseClient
conn.Open
SQL = "select * from 学生信息表"
rs.Open SQL, conn, 3, 3
If rs.BOF Or rs.EOF Then
MsgBox "数据库没有记录!", vbInformation, "提示"
Exit Sub
Else
Set DataGrid1.DataSource = rs
display

Do While Not rs.EOF
Textfirst = Val(rs!第一次划款 & "")
Textsecond = Val(rs!第二次划款 & "")
Textthird = Val(rs!第三次划款 & "")
Textforth = Val(rs!第四次划款 & "")
textfirst1 = textfirst1 + Val(Textfirst)
textsecond1 = textsecond1 + Val(Textsecond)
textthird1 = textthird1 + Val(Textthird)
textforth1 = textforth1 + Val(Textforth)
total = total + 1
cover.ProgressBar1.Value = Int((I / rs.RecordCount) * 100)
I = I + 1
rs.MoveNext
Loop
sum = textfirst1 + textsecond1 + textthird1 + textforth1
Labeltotal.Caption = "数据库共有记录:" & total & "条"
Labelsum.Caption = "总贷款金额:" & sum & "元"
Labfirst.Caption = "第一次:" & textfirst1 & "元"
Labsecond.Caption = "第二次:" & textsecond1 & "元"
Labthird.Caption = "第三次:" & textthird1 & "元"
Labforth.Caption = "第四次:" & textforth1 & "元"
End If
Unload cover
Labsit.Caption = "状态:正常"
End Sub

Private Sub cmdclear_Click()
Textname.Text = ""
Textname.Text = ""
Textsex.Text = ""
Textxuehao.Text = ""
Textsf.Text = ""
Textzy.Text = ""
Textaddr.Text = ""
Textyb.Text = ""
Textht.Text = ""
Textbiye.Text = ""
Textfirst.Text = ""
Textfirsttime.Text = ""
Textsecond.Text = ""
Textsecondtime.Text = ""
Textthird.Text = ""
Textthirdtime.Text = ""
Textforth.Text = ""
Textforthtime.Text = ""
End Sub

Private Sub cmddelall_Click()
If MsgBox("( 慎!) 该操作将删除全部记录且不可恢复!确认删除吗?", vbYesNo + vbExclamation, "确认删除") = vbYes Then

rs.Close
SQL = "delete * from 学生信息表"
rs.Open SQL, conn, 3, 3
Else
Exit Sub
End If
MsgBox "记录已全部删除,请重新运行系统。", vbInformation, "提示"
Unload Me
End Sub


Private Sub cmdend_Click()
rs.MoveLast
display
Labsit.Caption = "状态:已经是末记录"
End Sub

Private Sub cmdexit_Click()
Unload Me
End Sub

Private Sub cmdhead_Click()
rs.MoveFirst
display
Labsit.Caption = "状态:已经是首记录"
End Sub

Private Sub cmdinput_Click()
addrecords.Show
End Sub


Private Sub cmdnext_Click()

If Not rs.EOF Then
              rs.MoveNext
              If Not rs.EOF Then
                          display     
        Else
                          MsgBox "已到最后一条记录", vbInformation, "提示"
              End If
        Else
              MsgBox "已到最后一条记录", vbInformation, "提示"
        End If

End Sub

Private Sub cmdpre_Click()
If Not rs.BOF Then
              rs.MovePrevious
              
              If Not rs.BOF Then
                          display   
        Else
                          MsgBox "已到最前一条记录", vbInformation, "提示"
              End If
        Else
              MsgBox "已到最前一条记录", vbInformation, "提示"
        End If

End Sub

Private Sub cmdrefresh_Click()
cmdrefresh.Enabled = False
SQL = "select * from 学生信息表 order by ID"
rs.Close
rs.Open SQL, conn, 3, 3
display
sum
Labsit.Caption = "状态:正常"
cmdrefresh.Enabled = True
Set DataGrid1.DataSource = rs
End Sub
Private Sub funrefresh()
SQL = "select * from 学生信息表"
rs.Close
rs.Open SQL, conn, 3, 3
Set DataGrid1.DataSource = rs
display

End Sub

Private Sub display()
Textid.Text = rs("ID")
Textname.Text = rs!姓名 & ""
Textsex.Text = rs!性别 & ""
Textxuehao.Text = rs!学号 & ""
Textsf.Text = rs!身份证号 & ""
Textzy.Text = rs!专业 & ""
Textaddr.Text = rs!住址 & ""
Textyb.Text = rs!邮编 & ""
Textht.Text = rs!合同号 & ""
Textbiye.Text = rs!毕业 & ""

Textfirst.Text = rs!第一次划款 & ""
Textfirsttime.Text = rs!第一次划款时间 & ""
Textsecond.Text = rs!第二次划款 & ""
Textsecondtime.Text = rs!第二次划款时间 & ""
Textthird.Text = rs!第三次划款 & ""
Textthirdtime.Text = rs!第三次划款时间 & ""
Textforth.Text = rs!第四次划款 & ""
Textforthtime.Text = rs!第四次划款时间 & ""
temp = Textid.Text
End Sub


Private Sub sum()

Dim total As Double
Dim sum As Double
Dim textfirst1 As Double
Dim textsecond1 As Double
Dim textthird1 As Double
Dim textforth1 As Double
Dim conn_sum As New ADODB.Connection
Dim rs_sum As New ADODB.Recordset
Dim SQL_sum As String

conn_sum.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db2.mdb;Persist Security Info=False;Jet OLEDB:Database Password=ZXDK"
conn_sum.CursorLocation = adUseClient
conn_sum.Open
SQL_sum = "select * from 学生信息表"
rs_sum.Open SQL_sum, conn, 3, 3



Do While Not rs_sum.EOF
Textfirst = Val(rs_sum!第一次划款 & "")
Textsecond = Val(rs_sum!第二次划款 & "")
Textthird = Val(rs_sum!第三次划款 & "")
Textforth = Val(rs_sum!第四次划款 & "")
textfirst1 = textfirst1 + Val(Textfirst)
textsecond1 = textsecond1 + Val(Textsecond)
textthird1 = textthird1 + Val(Textthird)
textforth1 = textforth1 + Val(Textforth)
total = total + 1
rs_sum.MoveNext
Loop
sum = textfirst1 + textsecond1 + textthird1 + textforth1
Labeltotal.Caption = "数据库共有记录:" & total & "条"
Labelsum.Caption = "总贷款金额:" & sum & "元"
Labfirst.Caption = "第一次:" & textfirst1 & "元"
Labsecond.Caption = "第二次:" & textsecond1 & "元"
Labthird.Caption = "第三次:" & textthird1 & "元"
Labforth.Caption = "第四次:" & textforth1 & "元"
End Sub
把一些无用的代码精简了,版主看看这是怎么回事:

昨天仔细研究发现:每次点击“刷新”后,在display()函数中,“毕业”字段之前的几个字段的值是第一条记录的值,是正确的。而“毕业”字段之后的那几个字段赋给text框的却是最后一条记录的值,而且,最后一条记录中那些字段有的无数值,运行后却发现自动添加上了“0”,并赋给了text。只有手动点击“移动”按钮,把指针向后移动再回到第一条记录,text框中才是显示正确的值(也就是均为第一条记录的值)。
附有截图和数据库,数据库密码为ZXDK
桌面.rar (248.91 KB)

因为涉及隐私,有些值临时删去了,请原谅!
2009-12-21 11:25
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:3 
只有数据库,没有工程,没法帮你测试。楼主请仔细检查一下以下的步骤:
1)看你的代码中没有涉及到修改数据库,请将rs.Open SQL, conn, 3, 3后面的 3 全部改为 1 后再调试一下;rs.Open SQL, conn, 1,1
2)请仔细检查一下,在设计状态下,窗体上的每一个文本框控件有没有设置数据源,如果有的话,请将其全部清空后测试一下;
3)程序其它部分有没有另外的事件给文本框赋值。
4)数据库中的ID列最好设置成主键

[ 本帖最后由 不说也罢 于 2009-12-21 12:10 编辑 ]

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2009-12-21 12:08
liuvking
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2009-7-22
收藏
得分:0 
回复 4楼 不说也罢
唉···所有可能的都排除了,但问题依旧···我都开始怀疑自己的编译器问题了···
2009-12-22 23:43
快速回复:ADO记录集的值不正确,请问怎么回事?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.070026 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved