c#/.net程序设计
请问这段代码是不是那里错了,为什么我输入数据库里有的用户名还是提示nao啊
SqlCommand darling = new SqlCommand(search, god);
int Count = (int)darling.ExecuteScalar();
特别是这两句是什么意思。
SqlConnection god = new SqlConnection("data source = .;initial catalog=QDDB; user id = sa; pwd = ");
god.Open();
string search = "select count(*)from account where(username='" + textBox1.Text + "' and password='" + textBox2.Text + "')";
SqlCommand darling = new SqlCommand(search, god);
int Count = (int)darling.ExecuteScalar();
if (Count > 0)
{
MessageBox.Show("hao");
}
else
{
MessageBox.Show("nao");
}
god.Close();
}
答案:SqlCommand darling = new SqlCommand(search, god);
int Count = (int)darling.ExecuteScalar();
1.search在CMD实例中的参数位置表示: 要查询的文本,也就是SQL的执行语句
2.god在CMD实例中的参数位置表示:数据库的链接实例。
当有这两个参数之后才能实例cmd对象,他负责发出执行命令和命令要到达那种效果
darling.ExecuteScalar():执行查询并返回结果集的第一行第一列的值,并忽视其他行和列。
你老出现nao,也就是说在IF else的判断中判断的条件是不成立的,也就是说count<=0,也就是说在数据库正常链接下,执行的SQL语句(search )的结果集中没有任何东西。
其实这些东西,看帮助文档都能解决,还有VS也有相关的提示。希望你能细心并解决掉你的问题
其他:string search = "select * from account where(username='" + textBox1.Text + "' and password='" + textBox2.Text + "')";
用dataset装填数据,再用dataset.table[0].rows.count判定
上一个:开发.net项目是指什么,比如面试的时候要拿自己的多少个项目过去,项目是不是可以理解成案例呢?
下一个:.net访问JS数组???