问题描述
我的具体代码是:PublicClassForm1InheritsSystem.Windows.Forms.FormDimdatardAsSystem.Data.OleDb.OleDbDataReaderPrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadOleDbCon.ConnectionString="provider=sqloledb;"OleDbCon.ConnectionString+="datasource=8AA0F42127FA473;"OleDbCon.ConnectionString+="initialcatalog=hubery1;"OleDbCon.ConnectionString+="userid=sa;"OleDbCon.ConnectionString+="password=;"OleDbCon.Open()OleDbCmd.Connection=OleDbConEndSubPrivateSubBok_Click(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesBok.ClickDimcmdstringAsStringIftxtname.Text=""ThenMsgBox("pleaseinputyourname",MsgBoxStyle.Question,"don'tinoutname")txtname.Focus()ExitSubEndIfIftxtpasswd.Text=""ThenMsgBox("pleaseinputyourpassword",MsgBoxStyle.Information,"don'tinputpassword")txtpasswd.Focus()ExitSubEndIfOleDbCmd.CommandType=CommandType.Textcmdstring="select*fromhubery1wherename='"+txtname.Text+"'andpassword='"+txtpasswd.Text+"'"OleDbCmd.CommandText=cmdstringOleDbCon.Close()OleDbCon.Open()datard=OleDbCmd.ExecuteReaderIfdatard.ReadThenMsgBox("okey")DimfmAsNewForm2fm.ShowDialog()ElseMsgBox("wrong")ExitSubEndIfEndSubPrivateSubBcancel_Click(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesBcancel.ClickEndEndSubEndClass我想知道为什么有的时候我运行时能跑通,而有的时候却出现上述问题呢?
解决方案
解决方案二:
PublicClassForm1InheritsSystem.Windows.Forms.FormDimdatardAsSystem.Data.OleDb.OleDbDataReaderPrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadOleDbCon.ConnectionString="provider=sqloledb;"OleDbCon.ConnectionString+="datasource=8AA0F42127FA473;"OleDbCon.ConnectionString+="initialcatalog=hubery1;"OleDbCon.ConnectionString+="userid=sa;"OleDbCon.ConnectionString+="password=;"'OleDbCon.Open()notneedOleDbCmd.Connection=OleDbConEndSubPrivateSubBok_Click(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesBok.ClickDimcmdstringAsStringIftxtname.Text=""ThenMsgBox("pleaseinputyourname",MsgBoxStyle.Question,"don'tinoutname")txtname.Focus()ExitSubEndIfIftxtpasswd.Text=""ThenMsgBox("pleaseinputyourpassword",MsgBoxStyle.Information,"don'tinputpassword")txtpasswd.Focus()ExitSubEndIfOleDbCmd.CommandType=CommandType.Textcmdstring="select*fromhubery1wherename='"+txtname.Text+"'andpassword='"+txtpasswd.Text+"'"OleDbCmd.CommandText=cmdstring'OleDbCon.Close()movetobelowOleDbCon.Open()datard=OleDbCmd.ExecuteReaderIfdatard.ReadThenMsgBox("okey")DimfmAsNewForm2fm.ShowDialog()ElseMsgBox("wrong")ExitSubEndIfOleDbCon.Close()'movetohereEndSubPrivateSubBcancel_Click(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesBcancel.ClickEndEndSubEndClass
解决方案三:
能不能给出详细点的解释?
解决方案四:
你原来的代码是先openconnection然后又closeconnection然后又openconnection下面你没有closeconnection.首先你前一组的openconnection和closeconnection,好无意义.第二次openconnection是对的,因为你要开始read了.可是read之后,你没有close.这为你再次运行埋下问题,因为你sqlconnection还没close呢.看看msdn吧若要创建SqlDataReader,必须调用SqlCommand对象的ExecuteReader方法,而不要直接使用构造函数。在使用SqlDataReader时,关联的SqlConnection正忙于为SqlDataReader服务,对SqlConnection无法执行任何其他操作,只能将其关闭。除非调用SqlDataReader的Close方法,否则会一直处于此状态。例如,在调用Close之前,无法检索输出参数。SqlDataReader的用户可能会看到在读取数据时另一进程或线程对结果集所做的更改。但是,确切的行为与执行时间有关。当SqlDataReader关闭后,只能调用IsClosed和RecordsAffected属性。尽管当SqlDataReader存在时可以访问RecordsAffected属性,但是请始终在返回RecordsAffected的值之前调用Close,以保证返回精确的值。注意为了获得最佳性能,SqlDataReader会避免创建不必要的对象或复制不必要的数据。因此,对GetValue等方法的多次调用将返回对相同对象的引用。如果正在修改由GetValue等方法返回的对象的基础值,请使用警告。http://msdn2.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader(VS.80).aspxPrivateSubReadOrderData(ByValconnectionStringAsString)DimqueryStringAsString=_"SELECTOrderID,CustomerIDFROMdbo.Orders;"UsingconnectionAsNewSqlConnection(connectionString)DimcommandAsNewSqlCommand(queryString,connection)connection.Open()DimreaderAsSqlDataReader=command.ExecuteReader()'CallReadbeforeaccessingdata.Whilereader.Read()Console.WriteLine(String.Format("{0},{1}",_reader(0),reader(1)))EndWhile'CallClosewhendonereading.reader.Close()EndUsingEndSub