我有多个DataReader的问题 [英] i have a problem with multiple DataReader

查看:58
本文介绍了我有多个DataReader的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是:

< pre>
如果IsNumeric(txtID.Text)= True而txtID.Text& lt;& gt;那没事
Dim com As New SqlCommand(从员工中选择*,其中EmpID =" CInt(txtID.Text)",con)
试试
con.Open()
Dr3 = com.ExecuteReader
而Dr3.Read()
txtID.Text = Dr3.Item(0)
txtName.Text = Dr3.Item(1)
txtDOP.Text = Dr3.Item(2)
txtPhone.Text = Dr3.Item(3)
txtEmail.Text = Dr3.Item(4)
会话("SecID")= CInt(Dr3.Item(5))
会话("DepID")= CInt(Dr3.Item(6))
会话("SectID")= CInt(Dr3.Item(7))
Dr3.Close()
con.Close()

con.Open()
昏暗的SecItem作为新的SqlCommand(从SecID =" Session("SecID")",con)的扇区中选择SecName
Dr4 = SecItem.ExecuteReader()
而Dr4.Read()
DropDownSec.SelectedValue = Dr4.Item(0)
结束时
Dr4.Close()
con.Close()

con.Open()
Dim DepItem As New SqlCommand(从部门中选择DepName,其中DepID =" Session("DepID")",con)
Dr5 = DepItem.ExecuteReader()
而Dr5.Read()
DropDownDep.SelectedValue = Dr5.Item(0)
结束时
Dr5.Close()
con.Close()

con.Open()
将SectItem设为新的SqlCommand(从SectID ="& Session("SectID")",con)的节中选择SectName
Dr6 = SectItem.ExecuteReader()
而Dr6.Read()
DropDownSect.SelectedValue = Dr6.Item(0)
结束时
Dr6.Close()
con.Close()
结束时

赶上
Label1.Text =无效的ID"
结束尝试
其他
Label1.Text =必须输入数字"
如果结束
</pre>


连接字符串:
昏暗的新SqlConnection(ConfigurationManager.ConnectionStrings("WebApplicationConnectionString").ConnectionString)

并在Web.Config中:
< code>< pre lang ="xml">& lt; connectionStrings& gt;
& lt;添加名称=&"WebApplicationConnectionString&" connectionString =<数据源= MALAHMAD-PC \ SQLEXPRESS;初始目录= WebApplication;集成安全性= True;< b> MultipleActiveResultSets = True</b> providerName =& quot; System.Data.SqlClient& quot;/& gt;
& lt//connectionStrings& gt;</pre>
</code>


当运行此代码时,Dr5和其余的try代码不会执行并执行Catch代码!!!!!!
我真的很想知道,这是什么问题?他为什么去抓代码?

如果有任何想法请告诉我...

问候

my code is:

<pre>
If IsNumeric(txtID.Text) = True And txtID.Text &lt;&gt; Nothing Then
Dim com As New SqlCommand("Select * from Employees where EmpID=" CInt(txtID.Text) "", con)
Try
con.Open()
Dr3 = com.ExecuteReader
While Dr3.Read()
txtID.Text = Dr3.Item(0)
txtName.Text = Dr3.Item(1)
txtDOP.Text = Dr3.Item(2)
txtPhone.Text = Dr3.Item(3)
txtEmail.Text = Dr3.Item(4)
Session("SecID") = CInt(Dr3.Item(5))
Session("DepID") = CInt(Dr3.Item(6))
Session("SectID") = CInt(Dr3.Item(7))
Dr3.Close()
con.Close()

con.Open()
Dim SecItem As New SqlCommand("select SecName from sectores where SecID=" Session("SecID") "", con)
Dr4 = SecItem.ExecuteReader()
While Dr4.Read()
DropDownSec.SelectedValue = Dr4.Item(0)
End While
Dr4.Close()
con.Close()

con.Open()
Dim DepItem As New SqlCommand("select DepName from Departments where DepID=" Session("DepID") "", con)
Dr5 = DepItem.ExecuteReader()
While Dr5.Read()
DropDownDep.SelectedValue = Dr5.Item(0)
End While
Dr5.Close()
con.Close()

con.Open()
Dim SectItem As New SqlCommand("select SectName from Sections where SectID=" & Session("SectID") "", con)
Dr6 = SectItem.ExecuteReader()
While Dr6.Read()
DropDownSect.SelectedValue = Dr6.Item(0)
End While
Dr6.Close()
con.Close()
End While

Catch
Label1.Text = "Invalid ID"
End Try
Else
Label1.Text = "Required To Enter Number "
End If
</pre>


connection String:
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("WebApplicationConnectionString").ConnectionString)

and in Web.Config:
<code><pre lang="xml">&lt;connectionStrings&gt;
&lt;add name=&quot;WebApplicationConnectionString&quot; connectionString=&quot;Data Source=MALAHMAD-PC\SQLEXPRESS;Initial Catalog=WebApplication;Integrated Security=True;<b>MultipleActiveResultSets=True</b>&quot; providerName=&quot;System.Data.SqlClient&quot;/&gt;
&lt;/connectionStrings&gt;</pre>
</code>


when run this code, Dr5 and the rest of try code dose not Execute and execute Catch code !!!!!!
i''m realy want to know,, what is the problem? why he go to catch code?

Plz if any one have any idea tell me...

regardes

推荐答案

首先,您需要添加更好的异常处理程序.

代替使用
< pre lang ="vb">
赶上
Label1.Text =& quot;无效的ID& quot;
结束尝试
</pre>

用途:
< pre lang ="vb">
将sqlEx捕获为SqlException
Label1.Text = string.Format("SqlException:{0}",sqlEx.Message)
异常捕获
Label1.Text = string.Format(通用:{0}",例如消息)
结束尝试
</pre>

这样,您将得到正确的错误,然后可以弄清楚为什么会得到该错误.

其次,您需要阅读Sql Injection.格式化查询的方法不是最佳方法.您也不需要打开和关闭连接.完成连接之前,请保持连接处于打开状态.

最后,处理完使用它们创建的所有对象(con.Dispose(),secItem.Dispos()等).
First of all you need to add better exception handler.

Instead of using
<pre lang="vb">
Catch
Label1.Text = &quot;Invalid ID&quot;
End Try
</pre>

use:
<pre lang="vb">
Catch sqlEx as SqlException
Label1.Text = string.Format("SqlException: {0}", sqlEx.Message)
Catch ex as Exception
Label1.Text = string.Format("Generic: {0}", ex.Message)
End Try
</pre>

This way you will get the correct error and then you can figure out why you are getting the error.

Secondly, you need to read up on Sql Injection. The way you have formated your query is NOT the best way. You also do not need to open and close the connection. Keep the connection open until you are done with it.

Lastly, dispose of all objects you create when you are done with them (con.Dispose(), secItem.Dispos(), etc).


请提供错误详细信息,以便我可以为您提供精确的帮助...

从您的代码..我可以理解,您不检查数据读取器中是否有任何行.因此,如果任何读取器的行数为0,那么在您使用时会出现某些错误

DropDownDep.SelectedValue = Dr5.Item(0)

请检查


如果Dr5.hasrows然后
而Dr5.Read()
DropDownDep.SelectedValue = Dr5.Item(0)
结束时

如果
结束
我想您的数据读取器5没有任何行.这样它就不会执行并执行捕获代码....

再次请提供错误描述
Will you provide the error details so that i can give u precise help...

From your code.. i can understand that u r not checking is there any rows in the data reader. so if any reader having 0 rows then there will be certain error as you are using

DropDownDep.SelectedValue = Dr5.Item(0)

please check that


if Dr5.hasrows then
While Dr5.Read()
DropDownDep.SelectedValue = Dr5.Item(0)
End While

end if

i guess your data reader 5 doesn''t have any rows. so that it does not executes and execute catch code.......

again please provide error description


这篇关于我有多个DataReader的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆