日期:2014-05-18  浏览次数:20455 次

初学者的一个小错误,请大家指点

public   static   SqlConnection   con;
  public   static   SqlCommand   cmd;
       
//建立连接对象
        public   static   SqlConnection   createCon()
        {
              con   =   new   SqlConnection( "server=.\\sqlexpress;database=vote;uid=sa;pwd=44116501 ");
              if(con.State.Equals(ConnectionState.Closed))
                {
                        con.Open();
                }
                return   con;
        }
        //建立连接对象完毕

       
        //建立命令对象
           
        public   static   SqlCommand   createCmd(sql)
        {
                SqlConnection   con   =   createCon();
                SqlCommand   cmd=   new   SqlCommand(sql,con);
                return   cmd;
        }
          //建立命令对象完毕

        //用户名有效性
        public   static   bool   ViladateUserName(strUserName)
        {  
        SqlConnection   con=createCon();
        SqlCommand   cmd=createCmd( "select   *   from   admin   where   userName= ' "+strName+ " ' ");
        if(Convert.ToInt32(cmd.ExecuteScalar())> 0)
        {
                  return   false;
        }
        else
        {
                return   true;
        }

        //
        }

        public     static   void     closeCon()
        {
            if(con.State.Equals(ConnectionState.Open))
            {
            con.Close();
            }
        }

这是我的DB类的一部分,在public   static   SqlCommand   createCmd(sql)   的sql部分和public   static   bool   ViladateUserName(strName)   的strName部分提示应输入标识符
不知道是错在哪里?


------解决方案--------------------
SqlCommand cmd=createCmd( "select * from admin where userName= ' "+strName+ " ' ",con);
试试

------解决方案--------------------
public static bool ViladateUserName(string strName)
------解决方案--------------------
参数没有定义类型
字符串型转换成float型的有关问题