我想实现一个通过if语句接收存储过程的返回结果,
从而判断出用户输入是否是数据库中的帐号!
以下代码能够运行,但if语句不起作用!点击确定后,
无论数据库有没有用户信息,都直接显示else里的帐号错误;我该怎样修改?
Default.aspx
<%@ import namespace="System.Data"%>
<%@ import namespace="System.Data.SqlClient"%>
<html>
<head>
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <asp:TextBox ID="Textbox1" runat="server"/>
    <asp:Button  ID="Button1"  runat="server" OnClick="Button1_Click"/>
    <asp:Label   ID="Label1"   runat="server"/> 
    </form>
</body>
</html><script runat="server"  language="C#">
    void Button1_Click(object sender, System.EventArgs e)
    {
        SqlConnection SqlConnection1 = new SqlConnection("server=localhost;user id=sa;password=;database=database1;");
        SqlConnection1.Open();        SqlCommand SqlCommand1 = new SqlCommand("APP", SqlConnection1);
        SqlCommand1.CommandType = CommandType.StoredProcedure;
        SqlParameter part1 = new SqlParameter("@root", SqlDbType.VarChar, 8);
        part1.Value = Textbox1.Text;
        SqlCommand1.Parameters.Add(part1);
        SqlCommand1.ExecuteNonQuery();
        SqlDataReader dr = SqlCommand1.ExecuteReader();
        if (dr.HasRows)
        {
            System.Web.Security.FormsAuthentication.SetAuthCookie(Textbox1.Text, false);
            Response.Redirect("data2.aspx");        }
        else
        {
            Label1.Text = "帐号错误";
        }
    }  
    </script>
存储过程为:
CREATE PROCEDURE  APP
@roots  varchar(8)
as
select roots  from table2  where roots='@roots'
GO

解决方案 »

  1.   

    这句删除试试
    SqlCommand1.ExecuteNonQuery();
      

  2.   

    存储过程也有问题:
    select roots  from table2  where roots=@roots
      

  3.   

    应该用 dr.Read() 让游标下移一行,如果有数据自然就进if
    没有数据就进入else还有最后一定要SqlConnection1.Close()看你上面的程序好像没有Close
      

  4.   

    Aden(Aden) SqlCommand1.ExecuteNonQuery();这是返回一个INT型的数据,故你的错误。
    提供一些事例:
    CREATE PROCEDURE sp_AccountRole_Create@CategoryID int,@RoleName nvarchar(10),@Description nvarchar(50),@RoleID int outputAS    DECLARE @Count int    -- 查找是否有相同名称的记录    SELECT @Count = Count(RoleID) FROM Account_Role WHERE        RoleName = @RoleName    IF @Count = 0        INSERT INTO Account_Role         (CategoryID, RoleName, Description) valueS        (@CategoryID, @RoleName, @Description)        SET @RoleID = @@IDENTITY        RETURN 1GO
      
      执行存储过程的C#过程: 
      SqlConnection DbConnection = new SqlConnection(mConnectionString);SqlCommand command = new SqlCommand( "sp_AccountRole_Create", DbConnection );DbConnection.Open(connectString);// 废置SqlCommand的属性为存储过程command.CommandType = CommandType.StoredProcedure;command.Parameters.Add("@CategoryID", SqlDbType.Int, 4);command.Parameters.Add("@RoleName", SqlDbType.NVarChar, 10);command.Parameters.Add("@Description", SqlDbType.NVarChar, 50);command.Parameters.Add("@RoleID", SqlDbType.Int, 4);// 返回值command.Parameters.Add("Returnvalue",            SqlDbType.Int,            4,        // Size            ParameterDirection.Returnvalue,            false,        // is nullable             0,        // byte precision            0,        // byte scale            string.Empty,            DataRowVersion.Default,            null );command.parameters["@CategoryID"].value = permission.CategoryID;command.parameters["@RoleName"].value = permission.PermissionName;command.parameters["@Description"].value = permission.Description;// 可以返回新的ID值command.parameters["@RoleID"].Direction = ParameterDirection.Output;int rowsAffected = command.ExecuteNonQuery();int result = command.parameters["Returnvalue"].value;int newID = command.parameters["@RoleID"].value;
    还有些http://support.microsoft.com/kb/310070/zh-cn
      

  5.   

    select roots  from table2  where roots=@roots
    if(dr.Read()){....}