请教vb高手们!!!看一下我的代码哪里错了!!!

2024-12-21 22:09:23
推荐回答(3个)
回答1:

数据库链接字符串错了,正确的写法是:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xszy.mdb;Persist Security Info=False"
还有myrs.Open 的参数补全,在后面加上 adOpenStatic, adLockOptimistic
即:
myrs.Open "select * from ywcj", mycon, adOpenStatic, adLockOptimistic

回答补充:
你确定按我的方法改了吗?上面的两个参数加上了吗?(这两个参数很重要)这是修改后的代码,你再试试
Private Sub Command1_Click()
Dim mycon As New ADODB.Connection
Dim myrs As New ADODB.Recordset
Dim value1 As Integer
Dim converage1 As Integer
mycon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xszy.mdb;Persist Security Info=False"
mycon.Open
myrs.Open "select * from ywcj", mycon, adOpenStatic, adLockOptimistic
myrs.MoveFirst
Show
Do While Not myrs.EOF
value1 = value1 + myrs.Fields("成绩")
count1 = count1 + 1
myrs.MoveNext
Loop
coverage1 = value1 / count1
Text1.Text = coverage1
myrs.Close
End Sub

回答2:

mycon.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=xszy;Initial Catalog=xszy"
连接字符串有问题!你用的是access数据库又不是sql,做如下改动试试:
mycon.ConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xszy;Jet OLEDB:Database Password=123" (如果没有密码,最后的Database Password=123可去掉。

回答3:

求平均值
myrs.Open "select avg(成绩) from ywcj", mycon