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

求助:为什么通过GetString()取出的字符串与TextBox.Text取出的字符串始终无法比较成功?
各位大侠,我是个.NET新手,有个问题向大家请教。我想做个登陆程序,但是通过getstring()取出的字符串与Textbox里输入的字符串即使一样,在比较时也显示不同,小弟百思不得其解,请各位大侠指教,下面是部分代码:
        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                string   gh   =   TextBox1.Text;      
                SqlConnection   conn   =   new   SqlConnection();
                conn.ConnectionString   =   "Data   Source=WLEI;Initial   Catalog=MIS;User   ID=sa;Password=3329 ";
                SqlCommand   cmd   =   new   SqlCommand();
                cmd.CommandText   =   "select   gh,pwd   from   employee   where   gh= ' "+gh+ " ' ";
                cmd.Connection   =   conn;
                conn.Open();
                SqlDataReader   reader   =   cmd.ExecuteReader();

               
              try
                {
                        if   (reader.Read())
                        {
                                if   (reader.GetString(1)   ==   TextBox2.Text)
                                {
                                        Response.Write( "OK ");
                                }
                                else
                                {
                                        Response.Write( "bad ");
                                }
                        }
                        else
                        {
                                Response.Write( "no   such   user ");
                        }
                }