vb.net sql的查询的问题 !!!

shen_wei 2009-08-18 10:40:48

If conn.State = ConnectionState.Closed Then conn.Open()
sqltmpstr = "select UserID from tempUser" //表里有大量的数据
ds = OleDBHandle.GetOleDataSet(sqltmpstr, "temp0", conn)
If ds.Tables("temp0").Rows.Count > 0 Then //例如:循环20次
For i = 0 To ds.Tables("temp0").Rows.Count - 1
id = ds.Tables("temp0").Rows(i)("UserID") // 先取到id 作为下一个表查询的条件 不知道为什么出错
sqlstr = "select * from TotalReport where Uid='" & id & "'"
ds = OleDBHandle.GetOleDataSet(sqlstr, "temp1", conn)
If ds.Tables("temp1").Rows.Count > 0 Then
esuper = ds.Tables("temp1").Rows(0)("superTotal")
egood = ds.Tables("temp1").Rows(0)("goodTotal")
enormal = ds.Tables("temp1").Rows(0)("normalTotal")
ebad = ds.Tables("temp1").Rows(0)("badTotal")
ctime = ds.Tables("temp1").Rows(0)("cardtime")
End If
sqlstr = "insert into DayReport (Uid,Uname,daytime,cardtime,superDay,goodDay,normalDay,badDay) values ('" & id & "','" & name & "','" & seartime & "','" & ctime & "','" & esuper & "','" & egood & "','" & enormal & "','" & ebad & "')"
OleDBHandle.ExecuteNonQuery(sqlstr, conn)
Next

上述的问题怎么解决??
...全文
102 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wplpss 2009-08-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shen_wei 的回复:]
错误提示::数据库连接错误!System.NullReferenceException:未将对象引用设置到对象的实例中

[/Quote]
你的错误应该在你的代码For循环上面那段,说白了数据库没连上,设断点找吧,别人帮不了你,因为只有你有那个库。
jane_zhao 2009-08-18
  • 打赏
  • 举报
回复
1.首先:数据库连接错误----》应该检查这个类OleDBHandle的这个方法GetOleDataSet。
2.还有这个地方要修改:
If ds.Tables("temp0").Rows.Count > 0 Then //例如:循环20次
---》
If ds isnot nothing andalso ds.Tables("temp0").Rows.Count > 0 Then
同理,下面这句也要修改,
If ds.Tables("temp1").Rows.Count > 0 Then
---》If ds isnot nothing andalso ds.Tables("temp1").Rows.Count > 0 Then
因为你连接数据库错误,查询出来的ds为nothing,引用它的table就会报错:System.NullReferenceException。

wuxiong100 2009-08-18
  • 打赏
  • 举报
回复
是不是数据库链接字符串写错误啦
shen_wei 2009-08-18
  • 打赏
  • 举报
回复
错误提示::数据库连接错误!System.NullReferenceException:未将对象引用设置到对象的实例中
xunis 2009-08-18
  • 打赏
  • 举报
回复
你把提示错误的信息贴出来 要不然看没注释的代码比自己写都累 虽然我不一定能帮你解决问题
wplpss 2009-08-18
  • 打赏
  • 举报
回复

id = ds.Tables("temp0").Rows(i)("UserID").toString
shen_wei 2009-08-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wplpss 的回复:]
你那个id 到底是怎么定义的?
类型是什么?
[/Quote]

Dim id As String = ""
shen_wei 2009-08-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xunis 的回复:]
VB.NET code
id= ds.Tables("temp0").Rows(i)("UserID")// 先取到id 作为下一个表查询的条件 不知道为什么出错

Tables("temp0").'这是什么东西("temp0")??
[/Quote]

ds = OleDBHandle.GetOleDataSet(sqltmpstr, "temp0", conn)
临时的一个表 暂时把数据存到里面的一个临时表
wplpss 2009-08-18
  • 打赏
  • 举报
回复
你那个id 到底是怎么定义的?
类型是什么?
xunis 2009-08-18
  • 打赏
  • 举报
回复

id = ds.Tables("temp0").Rows(i)("UserID") // 先取到id 作为下一个表查询的条件 不知道为什么出错

Tables("temp0"). '这是什么东西("temp0")??

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧