求用户验证(登录)的c#.net代码
解决方案 »
- 图片无法显示问题!
- C# 打开远程目录
- c#对word操作wdFieldNumPages 怎样减少总页码数?
- 怎么查询局域网内主机ip
- C# Windows的TreeView如何得到第一个节点的子节点?各位不好意思了, 只有10分了,可还是期待大家帮忙解难题。
- 基础不牢靠啊 大家指点一下
- C#分段循环,节约内存的算法
- 在线等ServiceController.ExecuteCommand的用法
- 按照时间来进行查询?
- 偷偷的问一下:用VS2010编写的C#程序,在没有安装.NET的XP操作系统上是不是真的不能运行?
- 高分回报。请问如何接收引用类中的字符串?急用!
- 刚接触,问个简单的问题!谢谢~(运行问题)
{
Cmd.CommandText="PRO_Reg_Login";
Cmd.CommandType=CommandType.StoredProcedure;
Cmd.Parameters.Add("@User",SqlDbType.NVarChar,255);
Cmd.Parameters["@User"].Value=User.Text;
Cmd.Parameters.Add("@Pass",SqlDbType.NVarChar,255);
Cmd.Parameters["@Pass"].Value=Pass.Text;
Cmd.Parameters.Add("@COUNTS",SqlDbType.Int,4);
Cmd.Parameters["@COUNTS"].Direction=ParameterDirection.Output;
try
{
Cmd.ExecuteNonQuery();
}
catch
{
Response.Write(DataBase.ErrorMessage("您没有通过验证","window.location.href='index.aspx'"));
Response.End();
}
int con=Convert.ToInt32( Cmd.Parameters["@COUNTS"].Value.ToString().Trim());
if(con>0)
{
Session["good"]="good";
Session["User"]=User.Text;
list.Visible=false;
reg.Visible=true;
UserName.Text=User.Text; }
else
{
Response.Write(DataBase.ErrorMessage("您没有通过验证","window.location.href='index.aspx'"));
Response.End();
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string strConn="Server=TSJ;uid=sa;Pwd=sa;database=BBSv1";
string strCmd="select count(*) from lyUser where(UserName like '"+TextBox1.Text+"'and Pwd like '"+TextBox2.Text+"')";
SqlConnection myConnection=new SqlConnection();
myConnection.ConnectionString=strConn;
SqlCommand myCommand=new SqlCommand(strCmd,myConnection);
myCommand.Connection.Open();
//检索出的用户数
int flag=(int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
myConnection.Close();
if(flag>0)//用户存在
{
Session["UserName"]=TextBox1.Text;
Response.Redirect("Ex09_03.aspx");
}
else
{
Label1.Text ="用户名和密码不正确!";
TextBox1.Text="";
TextBox2.Text="";
}
}
将上边的数据库等进行适当修改就可以了!
{
//'如果是系统预设的管理者, 就不用再进数据库去检查身份
if (txUserID.Text =="" & txPasswordCode.Text =="")
{
strUserID="Admin";
bnClose=true;
return;
}
//检查用户代号与密码的SQL指令
cmdSQL.CommandText ="Select UserId From Users"+"where UserID='"+txUserID.Text+ "'"+"And PasswordCode='"+txPasswordCode.Text+"'";
cnKS.Open ();
//开启数据库连接
try
{
strUserID=((string)(cmdSQL.ExecuteScalar() ));//将查询结果丢给strUserID
if(strUserID=="")//如果结果是空白的, 代表找不到该用户,至于是密码错误或者是用户错误则不管
{
MessageBox.Show ("错误","失败",MessageBoxButtons.OK ,MessageBoxIcon.Error );
bnClose=false;//身份错误不可按OK离开
}
else
{
bnClose=true;//找到, 可以离开
strUserID=txUserID.Text ;
}
}
finally
{
cnKS.Close ();//关毕数据库连接
}
}
private void button1_Click(object sender, System.EventArgs e)
{
OleDbConnection con=new OleDbConnection ("Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Application.StartupPath +@"\sdms.mdb");
string sql="select * from login where uname='"+txtUname.Text .Replace ("'","''")+"' and pwd='"+txtPwd.Text .Replace ("'","''")+"' and Authority='"+cbxAuthority.Text+"'" ;
try
{
con.Open ();
OleDbCommand cmd=new OleDbCommand(sql,con);
dr=cmd.ExecuteReader ();
if(dr.Read ())
{
if(this.cbxAuthority .Text =="操作员")
{
GlobalValue.Name="操作员";
}
else
{
GlobalValue.Name="管理员";
}
this.Hide ();
Form1 main=new Form1 ();
main.ShowDialog ();
}
else
{
MessageBox.Show ("请填写正确的用户名、密码、用户角色","警告",MessageBoxButtons.OK ,MessageBoxIcon.Warning );
}
}
catch(Exception err)
{
throw err;
}
finally
{
con.Close ();
dr.Close ();
}
}