代码:
string strSal = "select conut(*) from Admin where LoginId = '"+userName+" '
and LoginPwd = '""+pwd+'";
conn.Open();
SqlCommand comm = new SqlCommand(strSql,conn)
int iRet = (int)comm.ExecuteScalar();
其中 UserName是用户名 pwd 是密码 conn 是数据库连接对象连接对象
string strSal = "select conut(*) from Admin where LoginId = '"+userName+" '
and LoginPwd = '""+pwd+'";
这一句为什么要打单引号‘’
int iRet = (int)comm.ExecuteScalar();
这句为什么要强转,强转之前 comm 是字符串类型? 还是 对象也是一种类型?
打单引号是因为你传值进去是字符串,没引号会报错,ExecuteScalar返回值是执行查询,并返回查询所返回的结果集中第一行的第一列。
你数据库字段是字符数据需要单引号括起,不是字符串不需要,这个是基础的sql语法,不会自己找本sql书来看
ExecuteScalar返回的是object,你不强制转换int iRet应该为object iRet
字符串格式需要加单引号,int类型不需要加单引号
楼上都回答到点子上了