个人收藏(关于C#程序做的登录窗口问题)

本文提供三个使用C#实现的登录验证代码示例,包括从网页表单收集用户名和密码进行数据库验证的方法,以及利用DataSet和SqlDataAdapter完成数据读取的过程。每个示例都展示了不同的异常处理和用户体验优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.  string name=Request.Form["username"];
   string pass=Request.Form["password"];
   SqlConnection conn=new SqlConnection("server=服务器名字iis;uid=sa;pwd=123456;database=数据库名");
   conn.Open();
   string sql="select * from 表名";  
   SqlCommand myCommand= new SqlCommand(sql,conn);
   SqlDataReader rstobj=myCommand.ExecuteReader();
   try{

   if(rstobj.Read()){
Response.Write("登陆成功");
}
  
  
   }   
   catch(Exception ex)
   {
    Response.Write(ex.Message);
   }
   finally
   {
    rstobj.Close();
    conn.Close();
   }

2.private void button1_Click(object sender, System.EventArgs e)
{
user_id = USER_ID.Text;
if(user_id == "")
{
MessageBox.Show("please input USER_ID");
return;
}

string user_pwd = USER_PWD.Text;
if(user_pwd == "")
{
MessageBox.Show("please input password");
return;
}


string strSQL;

strSQL = "SELECT * FROM A2_USER WHERE USER_ID = '" + user_id + "'";

SqlDataAdapter da = new SqlDataAdapter(strSQL, source);
DataSet ds = new DataSet();
da.Fill(ds,"USER");
try
{
if(ds.Tables["USER"].Rows.Count != 0)
{
if(ds.Tables["USER"].Rows[0]["USER_PWD"].ToString() == user_pwd.ToString())
{
this.Hide();
contract myForm = new contract();
myForm.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("password is wrong,please enter correct password");
return;
}
}
else
{
MessageBox.Show("Not exist this user");
return;
}
}
catch(System.Exception err)
{
MessageBox.Show(err.Message);
}

finally
{

}

}

3.private void loginqueding_Click(object sender, System.EventArgs e)
{


if(textBox2.Text==""||comboBox1.Text=="")
MessageBox.Show("用户名或密码为空!!");
else
{

try
{

string str="server=.;database=renshi;Integrated Security=True;";
SqlConnection con=new SqlConnection(str);
int res=0;
string  pssd=textBox2.Text.ToString().Trim();
con.Open();
string cmdText="select  * from userlist where name='"+username+"'and psd='"+pssd+"'  ";
SqlCommand cmd=new SqlCommand();
cmd.CommandText=cmdText;
cmd.Connection=con;
SqlDataReader sqldr=null;
sqldr=cmd.ExecuteReader();
sqldr.Read();

string ss=sqldr["name"].ToString().Trim();
ppow=sqldr["qxian"].ToString().Trim();
pssd=sqldr["psd"].ToString().Trim();
//MessageBox.Show(pssd);
//ppow=tmppow;


if(ss==username)
res=1;
if(res==1)
{

this.Hide();
//Application.Exit();
Form1 f1=new Form1();
f1.tmppower=ppow;
f1.yuanmima=pssd;
f1.tmpname=ss;//name
f1.Show();

}
else
{
MessageBox.Show("code0err");
//textBox2.Text=res.ToString();
}


}
catch(Exception ex)
{
string nn=ex.Message.ToString();
//MessageBox.Show(nn);
MessageBox.Show("用户名或密码错误!!");
textBox2.Text="";
//comboBox1.Text="";
}
finally
{
//sqldr.Close();
//con.Close();
//MessageBox.Show(ppow);
    }


}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值