数据库代码不正确.

来源:百度知道 编辑:UC知道 时间:2024/05/25 10:08:40
if(Page.IsValid)
{
string strConn="Data Source='hao-pc';User ID='sa';Password='123';Persist Security Info=True;Pooling=False;Initial Catalog='simpleBBS'";

SqlCommand cmd=new SqlCommand("select (*) from users Where username='"+tbName.Text+"' and password='"+tbWsp.Text"'",strConn); //错误指向此.
Object o=cmd.executeSclar();//执行返回符合条件的总数
if(Convert.ToInt32(o.ToString())!=0)
{Response.write("登陆成功")}
else
{Response.write("不存在")}
}

finally
{
reader.Close();
dbConn.Close();
dbConn.Dispose();
}
改为:
string strConn="Data Source='hao-pc';User ID='sa';Password='123';Persist Security Info=True;Pooling=False;Initial Catalog='simpleBBS'";

and password='"+tbWsp.Text"'",加上"+"
and password='"+tbWsp.Text + "'",

昨天的问题还有码?

SqlCommand cmd=new SqlCommand("select * from users Where username='"+tbName.Text+"' and password='"+tbPsw.Text+"'",strConn);
==========================================
不建议这么写,建议使用string.Format函数
SqlCommand cmd=new SqlCommand(string.Format("select * from users where username='{0}' and password='{1}'",tbName.Text,tbPsw.Text),strConn);
===================================================
试下上面的加进去可以不?有不合法参数表明你刚刚拼凑的字符串有误,用string.Format就会减少这种错误,当然仅仅是减少.

select (*) ,没有括号。select * from……

Object o 直接一个int类型的就可以了。登陆没有你这么判断的。
一般都是传2个参数来判断。
1using System;
2using System.Data;
3using System.Data.OleDb;
4using System.Configuration;
5using System.Collections;
6using System.Web;
7usin