如何通过sqldatasource计算获取的行数

时间:2010-11-07 17:15:03

标签: asp.net sql vb.net sqldatasource

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim LoginChecker As New SqlDataSource()
    LoginChecker.ConnectionString = ConfigurationManager.ConnectionStrings("A1ConnectionString1").ToString()
    LoginChecker.SelectCommandType = SqlDataSourceCommandType.Text
    LoginChecker.SelectCommand = "SELECT username FROM A1login WHERE username=@username AND password=@password"
    LoginChecker.SelectParameters.Add("username", username.Text)
    LoginChecker.SelectParameters.Add("password", password.Text)
    Dim rowsAffected As Integer = 0
    Try
        rowsAffected = LoginChecker.<what i have to write here>
    Catch ex As Exception
        'Server.Transfer("LoginSucessful.aspx")
    Finally
        LoginChecker = Nothing
    End Try
    username.Text = rowsAffected
    ' If rowsAffected = 1 Then
    'Server.Transfer("A1success.aspx")
    '   Else
    'Server.Transfer("A1failure.aspx")
    '  End If

End Sub

这是login.aspx.vb的代码   它根据返回的行检查数据库中的用户名和密码,并重定向到相应的页面。我在sqldatareader命名空间中找到正确的函数时遇到问题,因此它会计算受影响的行数。谁能告诉我应该在那里使用的功能?提前致谢。

7 个答案:

答案 0 :(得分:2)

使用SqlDataSource控件的Selected事件。 Select操作完成后引发事件。

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    lblRecordCount.Text = "Record Count: " + e.AffectedRows.ToString();
}

答案 1 :(得分:1)

protected void Button1_Click(object sender, EventArgs e)
{
    DataView d =(DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    Response.Write(d.Count);
}

答案 2 :(得分:0)

Select将返回IEnumerable。您可以ToList()。Count就此发表。

答案 3 :(得分:0)

SQLDataReader没有行计数属性,获取计数的最简单方法就是执行此操作...

int rowCount = 0;
if (dr.HasRows)  
{  
    while (dr.Read())  
        {  
            rowCount++;
            dgResults.DataSource = dr;  
            dgResults.DataBind();  
            dgResults.Visible = true;  
        }  

lblMsg.Text = rowCount.ToString();
dr.Close(); 

答案 4 :(得分:0)

You should read this page

 Namespace:  System.Data 
 Assembly:  System.Data (in System.Data.dll)

答案 5 :(得分:0)

要获取您的行,请声明sqlcommandsqlconnection

Dim cmd as sqlcommand
Dim con as new sqlconnection("Ur connection string")
cmd.connection=con
cmd.commandtext="SELECT count(username) FROM A1login WHERE username=@username AND password=@password"
Dim rowsAffected As Integer = cmd.executescalar()

rowsaffected中,您将获得提取的总行数

答案 6 :(得分:0)

SELECT Count(*) FROM A1login WHERE username=@username AND password=@password"

此代码返回计数值