有sqlDataReader的问题

时间:2010-05-14 15:32:49

标签: asp.net vb.net sqldatareader

我使用sqlDataReader来获取数据并将其设置为会话变量。问题是它不想使用表达式。我可以引用表中的任何其他列,但不能引用表达式。 SQL确实有效。代码如下。提前谢谢,安东尼

Using myConnectionCheck As New SqlConnection(myConnectionString)
    Dim myCommandCheck As New SqlCommand()
    myCommandCheck.Connection = myConnectionCheck
    myCommandCheck.CommandText = "SELECT Projects.Pro_Ver, Projects.Pro_Name, Projects.TL_Num, Projects.LP_Num, Projects.Dev_Num, Projects.Val_Num, Projects.Completed, Flow.Initiate_Date, Flow.Requirements, Flow.Req_Date, Flow.Dev_Review, Flow.Dev_Review_Date, Flow.Interface, Flow.Interface_Date, Flow.Approval, Flow.Approval_Date, Flow.Test_Plan, Flow.Test_Plan_Date, Flow.Dev_Start, Flow.Dev_Start_Date, Flow.Val_Start, Flow.Val_Start_Date, Flow.Val_Complete, Flow.Val_Complete_Date, Flow.Stage_Production, Flow.Stage_Production_Date, Flow.MKS, Flow.MKS_Date, Flow.DIET, Flow.DIET_Date, Flow.Closed, Flow.Closed_Date, Flow.Dev_End, Flow.Dev_End_Date, Users_1.Email AS Expr1, Users_2.Email AS Expr2, Users_3.Email AS Expr3, Users_4.Email AS Expr4, Users_4.FNAME, Users_3.FNAME AS Expr5, Users_2.FNAME AS Expr6, Users_1.FNAME AS Expr7 FROM Projects INNER JOIN Users AS Users_1 ON Projects.TL_Num = Users_1.PIN INNER JOIN Users AS Users_2 ON Projects.LP_Num = Users_2.PIN INNER JOIN Users AS Users_3 ON Projects.Dev_Num = Users_3.PIN INNER JOIN Users AS Users_4 ON Projects.Val_Num = Users_4.PIN INNER JOIN Flow ON Projects.id = Flow.Flow_Pro_Num WHERE id = "
    myCommandCheck.CommandText += QSid
    myConnectionCheck.Open()
    myCommandCheck.ExecuteNonQuery()
    Dim count As Int16 = myCommandCheck.ExecuteScalar
    If count = 1 Then
        Dim myDataReader As SqlDataReader
        myDataReader = myCommandCheck.ExecuteReader()
        While myDataReader.Read()
            Session("TL_email") = myDataReader("Expr1").ToString()
            Session("PE_email") = myDataReader("Expr2").ToString()
            Session("DEV_email") = myDataReader("Expr3").ToString()
            Session("VAL_email") = myDataReader("Expr4").ToString()
            Session("Project_Name") = myDataReader("Pro_Name").ToString()
        End While
        myDataReader.Close()
    End If
End Using

3 个答案:

答案 0 :(得分:0)

这可能是因为列名必须是唯一的,以便SqlDataReader能够使用列的字符串名称对它们进行索引。

答案 1 :(得分:0)

有几件事:

1)您正在执行查询3次。您可以丢失ExecuteNonQuery和ExecuteScalar调用,并使用“if myDataReader.Read()/ end if”替换while循环以获取第一个结果记录的数据值。如果没有找到记录,则不会设置会话变量,就像在当前代码中一样。

2)看起来更像问题在于你的会话管理(即从 Session获取值)而不是你的sql查询,这对我来说是好的。

检查:

  • 您在web.config文件中启用了sessionState,
  • 您不会在任何地方重置会话值,
  • 您在尝试发送电子邮件时要求使用相同的会话字段名称。 (例如,您是在设置会话(“DEV_Email”)但要求会话(“DEV电子邮件”)(空格而不是下划线)?

答案 2 :(得分:0)

对不起大家。代码工作得很好。 sqlDataReader将接受表达式作为列名。

我收到错误的原因是说from和to参数的值不能为null。对于我表中的任何记录,该列中没有数据。