sqlDataReader 出现问题

发布于 2024-09-01 05:53:56 字数 2167 浏览 3 评论 0原文

我正在使用 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

I am using a sqlDataReader to get data and set it to session variables. The problem is it doesn't want to work with expressions. I can reference any other column in the table, but not the expressions. The SQL does work. The code is below. Thanks in advance, Anthony

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一念一轮回 2024-09-08 05:53:56

这可能是因为列名需要唯一,SqlDataReader 才能使用列的字符串名称对其进行索引。

This may be because column names need to be unique for the SqlDataReader to be able to index them using a string name for the column.

怼怹恏 2024-09-08 05:53:56

有几件事:

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

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

检查:

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

A couple of things:

1) You are executing the query 3 times. You can lose the ExecuteNonQuery and ExecuteScalar calls, and replace the while loop with "if myDataReader.Read() / end if" to get the data values for the first resulting record. If no records are found, no session variables are set, just as in your current code.

2) It looks more like the problem lies in your session management (ie getting values from Session) rather than your sql query, which looks OK to me.

Check:

  • that you have sessionState enabled in your web.config file,
  • that you don't reset the Session values anywhere, and
  • that you ask for the same Session field name when you are trying to send the email. (e.g. are you setting Session("DEV_Email") but asking for Session("DEV Email") (space instead of underscore) ?
无名指的心愿 2024-09-08 05:53:56

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

我收到错误消息的原因是 from 和 to 参数的值不能为空。该列中没有我表中任何记录的数据。

Sorry everyone. The code works just fine. The sqlDataReader WILL accept expressions as column names.

The reason I was getting an error saying the value of the from and to parameters cannot be null. There was no data in that column for any of the records in my table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文