executeread命令同时从一个数据库中访问库中的两张表!!!!!

xzhy80 2004-05-08 11:36:57
If Page.IsValid Then

Dim StrConn As String = "user ID = sa; password =;"
StrConn += "database = bugs; server = (local);"
StrConn += "connect timeout =30"

Dim Conn As New SqlConnection(StrConn)

Conn.Open()

Dim StrSql As String = "SELECT * FROM muser WHERE userid =" & TempUser & ", passwd=" & TempPassword

Dim Cmd As New SqlCommand(StrSql, Conn)

Dim Dr As SqlDataReader = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Dim ReadTrue As Boolean = False

If Dr.Read() = True Then

Dim TempStr As String
TempStr = Dr("sect")
Response.Write("SECT:" & TempStr & "<br>")
ReadTrue = True

End If

If ReadTrue = True Then

StrSql = "SELECT msg FROM mmsg WHERE msgid = 'msg001'"
Cmd = New SqlCommand(StrSql, Conn)
Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

If Dr.Read() = False Then

Else

Session("msgid") = Dr("msgid")
Response.Write("MSGID:" & Session("msgid") & "<br>")

Session("msg") = Dr("msg")
Response.Write("MSG:" & Session("msg") & "<br>")


Dim num As Double = 0.1
Do While num > 99999.0
num += 0.1
Loop

End If

Conn.Close()
Response.Redirect("PCom002.aspx")

End If

End If


执行总有错误,executereader行有错误
...全文
86 9 点赞 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xzhy80 2004-05-08
如果是第二条产生的错误
没有关闭连接要怎么写啊
才刚用.net,对这个不很熟悉啊
能给代码说明一下麽
  • 打赏
  • 举报
回复
zjh135 2004-05-08
如果产生这个错误,一个是SQL语句有问题,二是没有关闭连接。。
  • 打赏
  • 举报
回复
xzhy80 2004-05-08
我打开的是两个表
本来想定义2套的,但要求用一个连接访问
所以才这么改,
但总有问题
不知道是怎么回事
  • 打赏
  • 举报
回复
xzhy80 2004-05-08
呵呵
这个还能执行下去
我主要的问题是executereader的
对这个语句没有什么理解
想问问这个
  • 打赏
  • 举报
回复
zhuyaowei 2004-05-08
Dim StrSql As String = "SELECT * FROM muser WHERE userid =" & TempUser & ", passwd=" & TempPassword
sql语句有错误嘛?
你的userid和passwd都是数值型嘛?怎么没引号?
  • 打赏
  • 举报
回复
xzhy80 2004-05-08
If Page.IsValid Then

Dim StrConn As String = "user ID = sa; password =;"
StrConn += "database = bugs; server = (local);"
StrConn += "connect timeout =60"

Dim Conn As New SqlConnection(StrConn)

If Conn.State <> ConnectionState.Open Then
Conn.Open()
End If

Dim StrSql As String = "SELECT * FROM muser WHERE userid ='" & TempUser & "'AND passwd='" & TempPassword & "'"

Dim Cmd As New SqlCommand(StrSql, Conn)

Dim Dr As SqlDataReader = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Dim ReadTrue As Boolean = False

If Dr.Read() = True Then

Dim TempStr As String
TempStr = Dr("sect")
Response.Write("SECT:" & TempStr & "<br>")
ReadTrue = True

End If
Dr.Close()

If Conn.State <> ConnectionState.Closed Then
Conn.Close()
End If

If ReadTrue = True Then

Conn = New SqlConnection(StrConn)

If Conn.State <> ConnectionState.Open Then
Conn.Open()
End If

StrSql = "SELECT msg,msgid FROM mmsg WHERE msgid = 'msg001'"

Cmd = New SqlCommand(StrSql, Conn)
Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

If Dr.Read() = False Then

Else

Session("msgid") = Dr("msgid")
Response.Write("MSGID:" & Session("msgid") & "<br>")

Session("msg") = Dr("msg")
Response.Write("MSG:" & Session("msg") & "<br>")


Dim num As Double = 0.1
Do While num = 99999.0
num += 0.1
Loop

End If
Dr.Close()
If Conn.State <> ConnectionState.Closed Then
Conn.Close()
End If
Response.Redirect("PCom002.aspx")

End If

End If

在打开和关闭的时候,先判断一下状态,然后再开关
这样就不会有错误了:)
  • 打赏
  • 举报
回复
xzhy80 2004-05-08
问题已经解决了
也明白了点道理
谢谢上面的3位
  • 打赏
  • 举报
回复
yistudio 2004-05-08
修改一下
If ReadTrue = True Then

StrSql = "SELECT msg FROM mmsg WHERE msgid = 'msg001'"
Cmd = New SqlCommand(StrSql, Conn)
'加上一句
if Dr.IsClosed=False Then Dr.Close()
Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

If Dr.Read() = False Then

Else
  • 打赏
  • 举报
回复
xzhy80 2004-05-08
这个不是一个简单的问题麽
怎么没有人来啊
  • 打赏
  • 举报
回复
相关推荐
发帖
VB
加入

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2004-05-08 11:36
社区公告
暂无公告