为什么登录不行?

lijun521 2004-10-06 08:31:41
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace csharptestbbs
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Data.SqlClient.SqlCommand sqlCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Web.UI.WebControls.Button Button2;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

private void Button1_Click(object sender, System.EventArgs e)
{
sqlCommand1.Connection.Open();
int flag=(int)sqlCommand1.ExecuteNonQuery();
sqlCommand1.Connection.Close();
sqlConnection1.Close();
if (flag>0)//用户存在
{
Session["username"]=TextBox1.Text;
Response.Redirect("main.aspx");
}
else
{
Label3.Text="输入错误";
TextBox1.Text="";
TextBox2.Text="";

}


}
}
}


请问为什么我输入正确的用户名和密码也是显示“输入错误”!!!
...全文
182 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cancersyf 2004-10-06
  • 打赏
  • 举报
回复
从存储过程中获取输出(output)参数不是通过ExecuteNonQuery()来获取的!
lijun521 2004-10-06
  • 打赏
  • 举报
回复
我一直是想拖控件的方法 尽量少编代码 可是发现这样子很不好 错误多多! 哎继续研究
悄悄的回来了 2004-10-06
  • 打赏
  • 举报
回复
select count(*) from table 这种办法是临时想的,我也不知道对不对!
我一直用的是我上面的方法
uffeng 2004-10-06
  • 打赏
  • 举报
回复
Command.CommandText="select count(*) from table"
//sqlCommand1.ExecuteNonQuery(); 不对!!
sqlCommand1.ExecuteScaler();
悄悄的回来了 2004-10-06
  • 打赏
  • 举报
回复
微软的东西,哎!有时就这样...........
lijun521 2004-10-06
  • 打赏
  • 举报
回复
奇怪死了 我真的疯掉了 ~!!!!!!我靠 结帖!!!
悄悄的回来了 2004-10-06
  • 打赏
  • 举报
回复
返回该查询条件的记录总数
lijun521 2004-10-06
  • 打赏
  • 举报
回复
该成 SELECT COUNT(1) FROM dbo.lj_User? 好的 这个是什么意思啊?
悄悄的回来了 2004-10-06
  • 打赏
  • 举报
回复
你把 SELECT UserName, Pwd
FROM dbo.lj_User
该成 SELECT COUNT(1) FROM dbo.lj_User
试一下。
lijun521 2004-10-06
  • 打赏
  • 举报
回复
加了.Trim()还是没用的嘛
悄悄的回来了 2004-10-06
  • 打赏
  • 举报
回复
那就不太清楚了?我马上用你的代码试一下!完后给你回复。
lijun521 2004-10-06
  • 打赏
  • 举报
回复
你的代码是不错!! 但我想知道为什么我的代码这样子不行? 感觉很郁闷的拉 ~:)
悄悄的回来了 2004-10-06
  • 打赏
  • 举报
回复
我用的登陆方式:
SQL存储过程:
--名称:GetUserValidity
--功能:检验用户登陆密码的合法性
--参数:输入参数@UserName用户名,@UserPass 用户密码
--输出参数:@intI 密码合法则返回1
CREATE procedure GetUserValidity
(
@UserName nvarchar(20),
@UserPass nvarchar(256),
@intI int output
)
as
select @intI = (select count(*) from Users where UserName = @UserName and UserPassward = @UserPass)
return(@intI)
GO
//公共函数:
public string UsersLogln(string strUserName,string strPwd)
{
/* 1、编写:50277
* 2、功能:验证用户登录的合法性
* 3、参数:strUserName 用户名;strPwd 密码
* 4、返回值: 字符串
*/
SqlCommand cmdTemp;
SqlParameter spTemp;
string strI;
try
{

OpenDataBase();//打开连接(自定义的函数)
//加密数据
// strPwd=FormsAuthentication.HashPasswordForStoringInConfigFile(strPwd,"md5");
cmdTemp=new SqlCommand("GetUserValidity",conn);
cmdTemp.CommandType=CommandType.StoredProcedure;
cmdTemp.Parameters.Add ("@UserName",strUserName);
cmdTemp.Parameters.Add ("@UserPassWord",strPwd);
spTemp=cmdTemp.Parameters.Add("@intI",SqlDbType.Int);
spTemp.Direction=ParameterDirection.Output;
cmdTemp.ExecuteNonQuery();
strI=spTemp.Value.ToString();//一样的返回结果(返回结果等于1:表示该用户存在,0表示该用户非法)
strI=cmdTemp.Parameters["@intI"].Value.ToString();//一样的返回结果
cmdTemp.Dispose();
CloseDataBase();//关闭连接(自定义的函数)
return strI;
}
catch(Exception)
{
CloseDataBase();
strI="0";
return strI;
}
}
//在Button1_Click(object sender, System.EventArgs e)里调用该函数:
private void Button1_Click(object sender, System.EventArgs e)
{
string strI=UsersLogln(TextBox1.Text.Trim(),TextBox2.Text.Trim());
if("0"==strI)
{
//用户名或密码错误
}
else
{
//成功登陆
}
}
悄悄的回来了 2004-10-06
  • 打赏
  • 举报
回复
在这加:TextBox1.Text.Trim()
lijun521 2004-10-06
  • 打赏
  • 举报
回复
对了 SQL语句是在sqlCommand1的CommandText里面 :
SELECT UserName, Pwd
FROM dbo.lj_User
WHERE (UserName = '"+TextBox1.Text+"') AND (Pwd = '"+TextBox2.Text+"')
lijun521 2004-10-06
  • 打赏
  • 举报
回复
跟踪看flag的值? 这个我不会 我是初学者


还有不要使用ExecuteNonQuery();的返回值来做判断.? 那请问用哪个方法呀?
悄悄的回来了 2004-10-06
  • 打赏
  • 举报
回复
跟踪看flag的值
popcorn 2004-10-06
  • 打赏
  • 举报
回复
你的SQL语句呢?
还有不要使用ExecuteNonQuery();的返回值来做判断.

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧