请假下某管理系统登录问题
--------------------编程问答-------------------- 是否输入的文本带空格了?这样直接拼装sql语句不好,容易被sql注入,建议用参数!
myCom.CommandText = "SELECT COUNT(*) FROM userlist WHERE user= ' " + txtB_user.Text + " ' ";
是否txtB_user.Text有空格导致了问题?加上txtB_user.Text.Trim()看看?
另外建议用参数
--------------------编程问答-------------------- 楼上说的就不错,那个sql语句不要那么些,可以用楼上的方法,也可以用存储过程!
myCom.CommandText = "SELECT COUNT(*) FROM userlist WHERE user=@user";
myCom.Parameter.Add("@user",SqlDbType.NVChar,20);
myCon.Paramter["@user"].Value=txtB_user.Text;
存储过程的好处:
存储过程的能力大大增强了SQL语言的功能和灵活性。存储过程可以用流控制语句编写,有很强的灵活性,可以完成复杂的判断和较复杂的 运算。
可保证数据的安全性和完整性。
可以降低网络的通信量。
使体现企业规则的运算程序放入数据库服务器中
语法直接百度N多个...呵呵!!! --------------------编程问答-------------------- using(SqlConnection conn = new SqlConnection("se"))
{if(string.isNullOrEmpty(txtB_user.Text){}}
myCom.CommandText = "SELECT COUNT(*) FROM userlist WHERE user= '" + txtB_user.Text.Trim() + "'";
查询分析器执行看看 --------------------编程问答-------------------- using(SqlConnection conn = new SqlConnection(""))
{if(string.isNullOrEmpty(txtB_user.Text){}}
--------------------编程问答-------------------- 先把sql语句在数据库新建查询中执行哈看有没有问题!还有就是最好用参数,不要那样连接字符串! --------------------编程问答-------------------- myCom.CommandText = "SELECT COUNT(*) FROM userlist WHERE user= ' " + txtB_user.Text + " ' ";
红色部分有空格,导致查不到数据:
改为:user='" + txtB_user.Text.Trim() + "'"; --------------------编程问答-------------------- 一定主意单引号(''):例如userA
应该是'userA'才能查到
而不是' userA ' --------------------编程问答-------------------- user= ' " + txtB_user.Text + " ' ";
' "之间的空格会惹祸 --------------------编程问答-------------------- string myConnectionString = "Initial Catalog=dengru;Data Source=localhost;Integrated Security=SSPI;";
myConnection = new SqlConnection(myConnectionString);
cmd = new SqlCommand();
cmd.Connection = myConnection;
myConnection.Open();
string sql;
sql = "select user from dr where ID='" + textBox1.Text.Trim() + "' and Pass= '" + textBox2.Text.Trim() + "'";
cmd.CommandText = sql;
if (cmd.ExecuteScalar() != null)
{
main ma = new main();//合法显示主窗体
ma.Show();
textBox1.Text = "";
textBox2.Text = "";
myConnection.Close();
} --------------------编程问答-------------------- 我猜你的SQL语句有可能有空格。直接在SQL中执行以下
补充:.NET技术 , C#