SqlConnection conn = “***”
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = " SELECT  工号 FROM  PUB_User where 系统识别 = 3 and 工号='" + UserName.Text + "' and 口令='" + Password.Text + "' and md5='" + Md5 + "'";
                cmd.Connection = conn;                conn.Open();
                SqlDataReader recu =cmd.ExecuteReader (); 
                
                ///判断用户是否合法
                if (recu.Read())
                {
                    userId = recu["工号"].ToString().Trim();
                }
                recu.Close();
                conn.Close();
                Response.Write(userId);
                    Response.Redirect("~/Main.aspx");
                    //Response.Write("正确");
——————————————————————————————————————————————
代码如上,请高手赐教

解决方案 »

  1.   

    可能是使用了連接池的原因,把SQL連接字符串貼出來看下。
      

  2.   

     SqlConnection conn = “***” 
                    SqlCommand cmd = new SqlCommand(); 
                    cmd.CommandType = CommandType.Text; 
                    cmd.CommandText = " SELECT  工号 FROM  PUB_User where 系统识别 = 3 and 工号='" + UserName.Text + "' and 口令='" + Password.Text + "' and md5='" + Md5 + "'"; 
                    cmd.Connection = conn; 因为cmd属性已经设置连接了。                conn.Open(); ////////////这一行去掉
                    SqlDataReader recu =cmd.ExecuteReader ();  
                     
                    ///判断用户是否合法 
                    if (recu.Read()) 
                    { 
                        userId = recu["工号"].ToString().Trim(); 
                    } 
                    recu.Close(); 
                    conn.Close(); 
                    Response.Write(userId); 
                        Response.Redirect("~/Main.aspx"); 
                        //Response.Write("正确"); 
      

  3.   

    你是想问为什么conn.Close()以后.
    还能输出正确的userId对吗?
    那是因为你的userId和conn没有关系.
      

  4.   

    回复2楼~删除conn.open();
    然后显示 未打开连接
      

  5.   

    回复1楼:
    conn=data source=(local);uid=sa;pwd=123;database=YPZB
    或者 conn=Data Source=(local);Initial Catalog=YPZB;Persist Security Info=True;User ID=sa;Password=123
      

  6.   

    SqlDataReader 用USing 括起来ExecuteReader  记得里面有一个是设置 用完 就关闭的参数的吧 
      

  7.   

    看連接字符串不是使用了連接池,那么你怎么知道還有一個連接?为什么执行conn.close()之后,sql还有一个连接啊??  
    ------------------------
    另外,我想知道你“sql还有一个连接啊??”表達的是什么意思?
    是說還有一個連接一直與SQL Server服務器保持連接?
    還是說還有一個SqlConnection?
      

  8.   

    回复10楼:
    我的后台sql2000数据库中,点击分离连接,显示有一个正在使用的连接~原提示:使用本数据库的连接   1
    我感觉应该是交换完数据就把所有连接关闭了才对啊~不然,当多个用户同时登录时,服务器岂不是要死机吗?
      

  9.   

    其實你可是試一下﹐在close()之后﹐再去讀取DB信息﹐看能否正常讀取來判斷連接是否存在~~~~~~