C#SQL DataReader:如何只读一次

时间:2016-01-26 18:03:52

标签: c# sql sql-server datareader

只有一次,谢谢你的帮助!

Public Class MyPage
    Inherits Page

    ' Assumed web controls present
    'Private UserName As WebControls.TextBox
    'Private Password As WebControls.TextBox
    'Private LtlLogin as WebControls.Label

    ' Create a simple structure so we can store username,
    ' password and (optionally) a page to be passed off to
    ' once logged in
    Private Structure Cred
        Public Username As String
        Public Password As String
        Public RedirectUrl As String
        Public Sub New(un As String, pw As String, Optional ru As String = "/admin/default.aspx")
            Username = un
            Password = pw
            RedirectUrl = ru
        End Sub
    End Structure

    Private ReadOnly _credentials As IEnumerable(Of Cred) = New Cred() { _
        ' Current credentials
        New Cred("userone", "passwordone"), _
        New Cred("usertwo", "passwordtwo"), _
        New Cred("userthree", "passwordthree", "/admin/custom.aspx") _
    }

    Public Sub Page_Load(sender As Object, e As EventArgs)
        ' See if we have a credential match
        Dim user = _credentials.SingleOrDefault(Function(x) x.Username = UserName.Text AndAlso x.Password = Password.Text)
        If Not string.IsNullOrEmpty(user.Username) Then
            ' Match found, log them in and redirect
            Session("Admin") = True
            Response.Redirect(user.RedirectUrl)
        Else
            ' No match found, deny access and notify
            Session("Admin") = False
            LtlLogin.Text = "<p>Sorry, you have provided incorrect login details.</p>"
        End If
    End Sub
End Class

我正在寻找如何让读者阅读数据并将其仅在列表框中发布一次!

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你只想进入读者循环一次。 &#39;虽然没有双关语,但是有更有效的方法可以解决这个问题,你可以声明一个bool标志,看看你是否已经击中了循环。进入循环后,将其更改为false,以便在下次计算条件时,它将评估为false以结束循环。见下文。

public string Show_details(string Command = "Select name From panda")
{
     SqlConnection cn = new SqlConnection(panda());
     SqlCommand Show;
     SqlDataReader read;

     Show = new SqlCommand(Command, cn);
     cn.Open();

     read = Show.ExecuteReader();

     // Declare flag to see if you've hit the reader yet.
     bool hasntYetRead = true;

     // Add a second condition to determine if to cursor through again
     while (read.Read() && hasntYetRead )
     {
        listBox1.Items.Add(read["name"]);

         // Change the flag to false
         hasntYetRead = false;
     }

     return Command;
}

答案 1 :(得分:0)

如果您希望将来能够更改数量或迭代次数,可以使用计数器。

public string Show_details(string Command = "Select name From panda")
{
     SqlConnection cn = new SqlConnection(panda());
     SqlCommand Show;
     SqlDataReader read;

     Show = new SqlCommand(Command, cn);
     cn.Open();

     read = Show.ExecuteReader();

     // declare counter
     int counter = 0;

     // Add a second condition to determine if to cursor through again
     while (read.Read() && counter < 1) //could get counter to count to user input number
     {
         listBox1.Items.Add(read["name"]);

         // Change the flag to false
         counter++;
     }

     return Command;
}