c#窗体登录不上去了(毕业设计)

来源:百度知道 编辑:UC知道 时间:2024/05/15 09:22:28
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "Server=localhost;database=gxtsgl;trusted_connection=true";
sqlconn.Open();
string str = "select * from t_user where 用户名=" + "\'" + text_name .Text + "\' and 密码=" + "\'" +text_pass.Text + "\'";
SqlCommand sqlcom = new SqlCommand(str, sqlconn);
SqlDataReader sqldr = null;
sqldr = sqlcom.ExecuteReader();//读取数据库的内容
if ((text_name.Text == "") || (text_pass.Text == ""))
{
MessageBox.Show("请输入用户名和密码!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
else
{
if (sqldr.HasRows)//判断与str字符串的条件是否匹配
{
this.Hide(

你的String str有问题
可改一下
String str="select count(*) from t_user where 用户名='" + this.text_name .Text + "' and 密码='" +this.text_pass.Text + "'";

SqlCommand sqlcom = new SqlCommand(str, sqlconn);
int n = Convert.ToInt32(sqlcom.ExecuteScalar());
if(n>0)
{
this.Hide();
name = this.text_name.Text;
frm_main main = new frm_main();
main.Show();
}

其实用不着这么麻烦啊!
给你看一段我写的代码,我的这个还要判断用户类型哦!
if (txtLoginId.Text == "")
{
MessageBox.Show("请输入用户名", "提示");
}
else if (txtLoginPassword.Text == "")
{
MessageBox.Show("请输入密码", "提示");
}
else
{
try
{
int type=3;