。。
string user = this.USERtB1.Text;
string pwd = this.PWDtB2.Text;
SqlConnection conn = new SqlConnection(@"server=(Local)\sqlexpress;Integrated Security=True;" + "Database=examination");//LOCAL
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) as flag from login where user=" + "'" + user
+ "'" + "and pwd =" + "'" + pwd + "'", conn);
int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString()); if (flag > 0)
{
main mmm = new main();
mmm.Show();
this.Visible = false;
}
else
{
MessageBox .Show ("出错","出错");
}
总是直接执行 MessageBox,数据库我已经由ncher改成int的了还是不行,求助大家帮忙。谢谢
string user = this.USERtB1.Text;
string pwd = this.PWDtB2.Text;
SqlConnection conn = new SqlConnection(@"server=(Local)\sqlexpress;Integrated Security=True;" + "Database=examination");//LOCAL
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) as flag from login where user=" + "'" + user
+ "'" + "and pwd =" + "'" + pwd + "'", conn);
int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString()); if (flag > 0)
{
main mmm = new main();
mmm.Show();
this.Visible = false;
}
else
{
MessageBox .Show ("出错","出错");
}
总是直接执行 MessageBox,数据库我已经由ncher改成int的了还是不行,求助大家帮忙。谢谢
and pwd ='" + pwd + "'", conn);
加上try catch看看抛出的是什么错误
string user = this.USERtB1.Text;
string pwd = this.PWDtB2.Text;
try{
SqlConnection conn = new SqlConnection(@"server=(Local)\sqlexpress;Integrated Security=True;" + "Database=examination");//LOCAL
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) as flag from login where user='"+user+"'
and pwd ='" + pwd + "'", conn);
int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString());
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
if (flag > 0)
{
main mmm = new main();
mmm.Show();
this.Visible = false;
}
else
{
MessageBox .Show ("出错","出错");
}
string user = this.USERtB1.Text;
string pwd = this.PWDtB2.Text;
try
{
SqlConnection conn = new SqlConnection(@"server=(Local)\sqlexpress;Integrated Security=True;" + "Database=examination");//LOCAL
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) as flag from login where user='"+user+"'and pwd ='" + pwd + "'", conn);
int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString());
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
} /* if ( flag > 0)
{
main mmm = new main();
mmm.Show();
this.Visible = false;
}
else
{
MessageBox .Show ("出错","出错");
}
*/
}
"select count(*) as flag from login where user=" + "'" + user
+ "'" + "and pwd =" + "'" + pwd + "'"最终得到的是 Select Count(*) as flag from login where user='u'and pwd='p'
也就是说 and 之前少了个空格.
+ "'" + "and pwd =" + "'" + pwd + "'", conn); flag是关键字,改为[flag]
所以不可能运行if语句中的内容,怎么没检索出来东西呢?
string pwd = this.textBox2.Text;
SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=master;Trusted_Connection=Yes;");
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) as flag from test where uid='"+user+"'and pwd ='" + pwd + "'", conn);
int flag = Convert.ToInt32(cmd.ExecuteScalar().ToString());
if (flag > 0)
{
Form2 frm = new Form2();
frm.Show();
this.Visible = false;
}
else
{
MessageBox.Show("出错", "出错");
}
可能连接字符串有问题!
+ "'" + "and pwd =" + "'" + pwd + "'", conn);
SqlCommand cmd = new SqlCommand("select count(*) as [flag] from login where user='" + user + "' and pwd ='" + pwd + "'", conn);
这样就应该对了