判断用户名并登陆 我做一个基于C/S的小型系统,数据库有一张user表,其中有用户名和密码等相关消息,请问用什么简短的语句判断登陆的用户名和密码是否正确并登陆主界面 ?希望能够详细一点,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string sql = "select * from user where userName='"+txtUserName.Text.Trim()+"' and Pwd='"+txtPwd.Text+"'"; try { SqlCommand command = new SqlCommand(sql, "数据库连接connection,楼主自己写"); connection.Open();//楼主自己创建connection对象 if(command.ExecuteScalar()==null) { 用户名或密码错误 } else { 登录主界面 } } catch (Exception ex) { MessageBox.Show(ex.Message, "数据库操作-登录"); } finally { connection.Close(); } }在数据库中用按用户名和密码查找,找到记录就说明这个用户名和密码是正确的string sql = "select * from user where userName='"+txtUserName.Text.Trim()+"' and Pwd='"+txtPwd.Text+"'"; private void btnLogin_Click(object sender, EventArgs e) { try { if (txtUserName.Text == "" || txtUserPwd.Text == "") { MessageBox.Show("用户名或密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { string name = txtUserName.Text.Trim(); string pwd = txtUserPwd.Text.Trim(); string ConnectionString = "server=localhost;database=MYdb;User=sa;Pwd=sa"; SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand("select * from tb_User where UserName='" + name + "' and UserPwd='" + pwd + "'", conn); SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); if (sdr.HasRows) { cmd.ExecuteNonQuery(); conn.Close(); this.Hide(); frmMain Main = new frmMain(); Main.Show(); } else { txtUserName.Text = ""; txtUserPwd.Text = ""; MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 比较简单的办法呢,就是利用你输入的用户名跟密码,作为条件来查询用户表,如果查到有记录,就登录成功,登录是否成功可以用一个BOOL变量来作标记。 //登入功能 private void button2_Click(object sender, EventArgs e) { if (comboBox1.Text == "" || textBox1.Text == "") { MessageBox.Show("请输入完整用户名和密码", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } DataClass.SQLConn.Close(); SqlCommand seleltUserinfocomm = new SqlCommand("select * from UserTable where UserID=@USERID and UPassWord=@PASSWORD",DataClass.SQLConn); seleltUserinfocomm.Parameters.Add(new SqlParameter("@USERID", SqlDbType.VarChar, 10)); seleltUserinfocomm.Parameters["@USERID"].Value = comboBox1.Text; seleltUserinfocomm.Parameters.Add(new SqlParameter("@PASSWORD", SqlDbType.VarChar, 10)); seleltUserinfocomm.Parameters["@PASSWORD"].Value = textBox1.Text; DataClass.SQLConn.Open(); SqlDataReader myUinfoRe = seleltUserinfocomm.ExecuteReader(); if (myUinfoRe.Read()) { this.Hide(); ID = myUinfoRe.GetString(0).ToString(); Password = myUinfoRe.GetString(1).ToString(); role = (bool)myUinfoRe.GetSqlBoolean(2); if (role == true) { Role = "管理员"; } else { Role = "普通用户"; } UName = myUinfoRe.GetString(3).ToString(); login = true; //MainForm mainform = new MainForm(); //mainform.ShowDialog(); } else { MessageBox.Show("用户名字或者密码错误"); } DataClass.SQLConn.Close(); myUinfoRe.Close(); } 简单一点,select * from usertable where username='"+userName+"' ";获取用户对像if(userObject!=null && userObject.Pwd==输入的用户密码){登录成功,存入session} 用用户名 和密码为条件 查询 id ok 都说了。先判断用户名,再比较密码。 string ConnectionString = ""; SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand("select * from table where UserName='" + name + "' ", conn); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { if(dr[""].ToString()==密码) { conn.Close(); Main.Show(); } } else { MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } 好歹用户登陆还是要用sql参数安全。 winform程序,如何实现文本框,默认显示“提示信息”,只要一点击,提示就没了 我怎么获得系统IE的路径 c#写的winform,要是需要在没装formwork的环境运行,能否通过某个软件导一下 C#程序如何设计才能不被Reflector等反编译 TabControl问题! 该项不适于在指定状态下使用 installshield 10.5 中的问题????? 很简单的初级扫盲问题,请大家帮我扫盲 求教一道有关类似拼图的问题。。。 能否通过代码拷贝数据库中的表 大侠帮忙..关于.net里用提交按钮添加用户的连接问题 C#调用C++????
string sql = "select * from user where userName='"+txtUserName.Text.Trim()+"' and Pwd='"+txtPwd.Text+"'";
try
{
SqlCommand command = new SqlCommand(sql, "数据库连接connection,楼主自己写");
connection.Open();//楼主自己创建connection对象
if(command.ExecuteScalar()==null)
{
用户名或密码错误
}
else
{
登录主界面
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "数据库操作-登录");
}
finally
{
connection.Close();
}
}
在数据库中用按用户名和密码查找,找到记录就说明这个用户名和密码是正确的
string sql = "select * from user where userName='"+txtUserName.Text.Trim()+"' and Pwd='"+txtPwd.Text+"'";
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
if (txtUserName.Text == "" || txtUserPwd.Text == "")
{
MessageBox.Show("用户名或密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
string name = txtUserName.Text.Trim();
string pwd = txtUserPwd.Text.Trim();
string ConnectionString = "server=localhost;database=MYdb;User=sa;Pwd=sa";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from tb_User where UserName='" + name + "' and UserPwd='" + pwd + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
{
cmd.ExecuteNonQuery();
conn.Close();
this.Hide();
frmMain Main = new frmMain();
Main.Show();
}
else
{
txtUserName.Text = "";
txtUserPwd.Text = "";
MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//登入功能
private void button2_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "" || textBox1.Text == "")
{
MessageBox.Show("请输入完整用户名和密码", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
} DataClass.SQLConn.Close();
SqlCommand seleltUserinfocomm = new SqlCommand("select * from UserTable where UserID=@USERID and UPassWord=@PASSWORD",DataClass.SQLConn);
seleltUserinfocomm.Parameters.Add(new SqlParameter("@USERID", SqlDbType.VarChar, 10));
seleltUserinfocomm.Parameters["@USERID"].Value = comboBox1.Text;
seleltUserinfocomm.Parameters.Add(new SqlParameter("@PASSWORD", SqlDbType.VarChar, 10));
seleltUserinfocomm.Parameters["@PASSWORD"].Value = textBox1.Text;
DataClass.SQLConn.Open();
SqlDataReader myUinfoRe = seleltUserinfocomm.ExecuteReader();
if (myUinfoRe.Read())
{
this.Hide();
ID = myUinfoRe.GetString(0).ToString();
Password = myUinfoRe.GetString(1).ToString();
role = (bool)myUinfoRe.GetSqlBoolean(2);
if (role == true)
{
Role = "管理员";
}
else
{
Role = "普通用户";
}
UName = myUinfoRe.GetString(3).ToString();
login = true;
//MainForm mainform = new MainForm();
//mainform.ShowDialog();
}
else
{
MessageBox.Show("用户名字或者密码错误");
}
DataClass.SQLConn.Close();
myUinfoRe.Close();
}
{
登录成功,存入session
}
先判断用户名,再比较密码。
string ConnectionString = "";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from table where UserName='" + name + "' ", conn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if(dr[""].ToString()==密码)
{
conn.Close();
Main.Show();
}
}
else
{
MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}