日期:2014-05-19  浏览次数:20458 次

两个表同时查询的问题,在线等!!!
string   userName   =   Request.Form.Get( "txtUserName ");
                string   userPwd   =   Request.Form.Get( "txtUserPwd ");
                SqlConnection   con   =   LoginDB.createCon();
                con.Open();
                SqlCommand   cmd=new   SqlCommand( "select   count(*)   from   admin,member   serName= ' "+userName+ " '   and   userPwd= ' "+userPwd+ " ' ",con);
                int   count   =   Convert.ToInt32(cmd.ExecuteScalar());
                if   (count   >   0)
                {
                        Session[ "flag "]   =   true;
                        Response.Write( "登陆成功 ");
                }
                else
                {
                        Response.Write( "登陆失败 ");
                }


同一个库中有两个表admin和member两个表(同为userName,userPwd两个字段),在登陆时,判断登陆时只要是两个表里的用户,用户名和密码正确就显示登陆成功.
上面的代码应该怎么写呢?

------解决方案--------------------
SqlCommand cmd=new SqlCommand( "select count(*) from admin,member where (admin.userName= ' "+userName+ " ' and admin.userPwd= ' "+userPwd+ " ') and (member.userName= ' "+userName+ " ' and member.userPwd= ' "+userPwd+ " '),con);
------解决方案--------------------
应该是或关系吧!
SqlCommand cmd=new SqlCommand( "select count(*) from admin,member where (admin.userName= ' "+userName+ " ' and admin.userPwd= ' "+userPwd+ " ') OR (member.userName= ' "+userName+ " ' and member.userPwd= ' "+userPwd+ " '),con);

------解决方案--------------------
或者 select count(*) from (select * from admin union all select * from menber) as AA where ........