执行读卡器连接错误

时间:2013-11-02 06:21:56

标签: c# asp.net

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=MJ-PC;Initial Catalog=Test;Integrated Security=True ");
        con.Open();
    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        //SqlCommand cmd = con.CreateCommand();
        SqlCommand cmd = new SqlCommand("select password from TestDemo where userName'" + txtusername .Text+ "'", con);

        //cmd.Connection = con;

        SqlDataReader da;
        da = cmd.ExecuteReader();
        if (!da.Read())
        {
            Response.Write("Wrong Details");
        }
        else
        {
            if(da[0].ToString()==txtusername.Text)
                 Response.Redirect("WebForm1.aspx");
            else
                Response.Write("Wrong Password");
        }
    }

4 个答案:

答案 0 :(得分:1)

where username **=** 忘记了相等标志此外,您打开的连接和您使用的连接是不同的

答案 1 :(得分:0)

我看到它的方式,你在Page_Load处理程序中打开与SQL服务器的连接。但你不要关闭它。

如果您尝试打开另一个,或尝试在已关闭的SqlConnection对象上执行,则可能会出错。

执行此操作的好方法是执行以下操作:

using (SqlConnection connection = new SqlConnection(connectionString)) 
{        
  try    
   {
            connection.Open();
          //do something here

   } 
   catch (Exception) 
   { 
      /*Handle error*/ 
   }

}

答案 2 :(得分:0)

     SqlConnection con;    
    protected void Page_Load(object sender, EventArgs e)
            {
    try
    {            
    con = new SqlConnection("Data Source=MJ-PC;Initial Catalog=Test;Integrated Security=True");
                con.Open();
    }
    catch
    {
    //Handles exceptions here
    }
            }

            protected void btnsubmit_Click(object sender, EventArgs e)
            {
              try
              {
                //SqlCommand cmd = con.CreateCommand();
                SqlCommand cmd = new SqlCommand("select password from TestDemo where userName='" + txtusername .Text+ "'", con);

                //cmd.Connection = con;

                SqlDataReader da;
                da = cmd.ExecuteReader();
                if (!da.Read())
                {
                    Response.Write("Wrong Details");
                }
                else
                {
                    if(da[0].ToString()==txtusername.Text)
                         Response.Redirect("WebForm1.aspx");
                    else
                        Response.Write("Wrong Password");
                }
              }
              finally
              {
              con.Close();
              }
            }

答案 3 :(得分:0)

如果你为登录代码,那么这里有一个简洁的版本代码,依赖你的flagset你可以重定向或显示错误的密码msg

    bool flagset=false;
    SqlDataReader dr;
    using (SqlConnection con = new SqlConnection(cn.ConnectionString))
    {
     using (SqlCommand cmd = new SqlCommand())
       {
        cmd.CommandText = "select password from TestDemo where userName=@uName";
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@uName", txtusername.Text); 
        cmd.Connection = con;
        con.Open();
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
         if (dr.HasRows){
           while (dr.Read())
            {
                if(dr[0].ToString()==txtusername.Text)
                 {     flagset=true;   }
            }
            }dr.Close();
             con.Close();
      }
}return flagset;