我想实现一个通过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
从而判断出用户输入是否是数据库中的帐号!
以下代码能够运行,但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
解决方案 »
- 取随机数
- 网站上的木马,怎么跑到客户端电脑上去的
- 各位大虾救命啊。。。。应用程序已预编译,因此不允许使用目录“/hh56web/App_Code/”。 这个怎么搞
- xslt的问题,高手帮忙!!!!!!!!!!
- 周一就要去现场了,还有个很奇怪问题请各位帮忙看看吧,就是引用Microsoft.Office.Interop.Excel没有权限的问题
- 菜鸟问题:关于ado.net连接字符串的问题
- 强名称的问题
- 高手们看看这是怎么回事--出现 web访问的用户过多.请刷新
- 怎么将字符串写入文本文件中
- DevExpress的GridView,怎么用表单外面的Button来实现增删改呢
- MemberShip中的角色都有什么作用?
- 今天面试,感谢论坛里的朋友,散分祈福
SqlCommand1.ExecuteNonQuery();
select roots from table2 where roots=@roots
没有数据就进入else还有最后一定要SqlConnection1.Close()看你上面的程序好像没有Close
提供一些事例:
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
if(dr.Read()){....}