'/ login'应用程序中的服务器错误

时间:2014-05-16 22:39:32

标签: c#

我是C#新手,我遇到了问题。

下面的代码不会关闭连接。 (连接保持打开状态)。关于我做错了什么的建议?

    conn.Open();
    string checkuser = "select count(*) from UserDataT where Username='" + TextBoxUSERNAME.Text + "'";
    SqlCommand com = new SqlCommand(checkuser, conn);
    int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
    if (temp == 1)
    {
        conn.Open();
        string checkPasswordQuery = "select password from UserDataT where Username='" + TextBoxUSERNAME.Text + "'";
        SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
        string password = passComm.ExecuteScalar().ToString().Replace(" "," ");
        if (password == TextBoxPASSWORD.Text)
        { 
            Session["New"]= TextBoxUSERNAME.Text;
            Response.Write("Passwordi eshte korrekt.");

        }
        else
        {       
            Response.Write("Passwordi nuk eshte korrekt.");                
        }
        conn.Close();
    }
    else
    {
     Response.Write("Username nuk eshte korrekt.");        
    }
    conn.Close();
}

1 个答案:

答案 0 :(得分:0)

如果您想再次打开它,请在完成后关闭conn

    conn.Open();
    string checkuser = "select count(*) from UserDataT where Username='" + TextBoxUSERNAME.Text + "'";
    SqlCommand com = new SqlCommand(checkuser, conn);
    int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

    conn.Close();

    if (temp == 1)
    {
        conn.Open();
        string checkPasswordQuery = "select password from UserDataT where Username='" + TextBoxUSERNAME.Text + "'";
        SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
        string password = passComm.ExecuteScalar().ToString().Replace(" "," ");
        if (password == TextBoxPASSWORD.Text)
        { 
            Session["New"]= TextBoxUSERNAME.Text;
            Response.Write("Passwordi eshte korrekt.");
        }
        else
        {
            Response.Write("Passwordi nuk eshte korrekt.");
        }
        conn.Close();
    }
    else
    {
     Response.Write("Username nuk eshte korrekt.");
    }

仅打开和关闭conn一次。

    conn.Open();
    string checkuser = "select count(*) from UserDataT where Username='" + TextBoxUSERNAME.Text + "'";
    SqlCommand com = new SqlCommand(checkuser, conn);
    int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

    if (temp == 1)
    {
        string checkPasswordQuery = "select password from UserDataT where Username='" + TextBoxUSERNAME.Text + "'";
        SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
        string password = passComm.ExecuteScalar().ToString().Replace(" "," ");
        if (password == TextBoxPASSWORD.Text)
        { 
            Session["New"]= TextBoxUSERNAME.Text;
            Response.Write("Passwordi eshte korrekt.");
        }
        else
        {
            Response.Write("Passwordi nuk eshte korrekt.");
        }
    }
    else
    {
     Response.Write("Username nuk eshte korrekt.");
    }
    conn.Close();