ado.net问题请教
NC 2008-04-10 05:40:10 下面是我的程序
'Imports System.Data.OleDb
Imports IBM.Data.Informix
Public Class Form1
Dim mycon As IfxConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim constring As String = "Provider=SQLOLEDB.1;User ID=cspdev;Password=cspdevcsp;Data Source=cspdev;Initial Catalog=cspdb;"
Dim constring As String = "host=192.168.102.31;server=whcspdev;protocol=onsoctcp;service=8888;database=cspdb;uid=cspdev;password=cspdevcsp;"
Try
mycon = New IfxConnection(constring)
mycon.ClientLocale = "ZH_CN.GB18030-2000"
mycon.DatabaseLocale = "ZH_CN.GB18030-2000"
mycon.Open()
MsgBox("连接成功" & mycon.ConnectionTimeout)
Button1.Enabled = False
Catch ex As Exception
MsgBox("连接失败" & ex.ToString())
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
mycon.Close()
Button1.Enabled = True
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim cmd As IfxCommand = New IfxCommand("select * from cmsinstinfo", mycon)
cmd.CommandTimeout = 30
Dim str As String
Dim i As Integer = 0
str = ""
Try
Dim dr As IfxDataReader
dr = cmd.ExecuteReader()
While dr.Read()
str += dr(i).ToString()
End While
MsgBox(str)
'cmd.Dispose()
Catch ex As Exception
Debug.WriteLine(ex.ToString())
End Try
End Sub
End Class
为什么把cmd.Dispose()注释掉,然后连续点command3就会抛异常出来?但也不是每次都抛,偶尔又正常。异常信息如下:
System.InvalidOperationException: 已有打开的 DataReader 与此连接相关联,必须先将此 DataReader 关闭。
在 IBM.Data.Informix.IfxConnection.SetStateExecuting(String method, IfxTransaction transaction)
在 IBM.Data.Informix.IfxCommand.ValidateConnectionAndTransaction(String method)
在 IBM.Data.Informix.IfxCommand.ExecuteReaderObject(CommandBehavior behavior, String method)
在 IBM.Data.Informix.IfxCommand.ExecuteReader(CommandBehavior behavior)
在 IBM.Data.Informix.IfxCommand.ExecuteReader()
在 ConnectDB.Form1.Button3_Click(Object sender, EventArgs e) 位置 d:\vbsrc\Dbop\ConnectDB\Form1.vb:行号 34
请指教,谢谢!