2

我的问题涉及从数据库中的表中返回特定值。我想让它在您登录后显示用户的名字和姓氏。

桌子看起来像这样。

UID(PK), FName, LName, Uname, Upass

我如何只返回FnameandLName以便我可以将它放在标签中?

这是我当前的代码。请记住,这是针对母版页的。

string strConn = WebConfigurationManager.ConnectionStrings["cloud2"].ConnectionString;

protected void btnlogin2_Click(object sender, EventArgs e)
{
    string strCmd = "Select * From Person Where uname = @uname and upass = @upass";

    SqlConnection objConn = new SqlConnection(strConn);

    SqlCommand objCmd = new SqlCommand(strCmd, objConn);
    objCmd.Parameters.AddWithValue("@uname", txtusername.Text);
    objCmd.Parameters.AddWithValue("@upass", txtpassword.Text);

    using (objConn)
    {
        objConn.Open();
        SqlDataReader objDR = objCmd.ExecuteReader();

        if (objDR.HasRows)
        {
            btnlogin2.Visible = false;
            txtusername.Visible = false;
            txtpassword.Visible = false;

            Response.Redirect("member.aspx");

            lblLogin.Text = "Logged in as: Test " ; //Display login name?
        }
        else
        {
            btnlogin2.Text = "Failed";
        }
    } 
}
4

6 回答 6

1

我会做三件事:

  • 将您的 UI 逻辑与数据库逻辑分开 - 不要在button_Click事件处理程序中间直接访问您的数据库......

  • 如果您只需要名字和姓氏,那么只需从表中选择这两列!

  • 把所有的一次性物品都像块一样SqlConnection, SqlCommand, SqlDataReaderusing(...) {...}以便在使用后妥善处理

所以我的代码看起来像这样:

界面层

protected void btnlogin2_Click(object sender, EventArgs e)
{
    // call the database layer to get the first and last name
    FirstAndLastName data = GetFirstAndLastName(txtusername.Text, txtpassword.Text);

    if (data != null)  // something was returned - show it
    {
         lblLogin.Text = string.Format("You're logged in as {0} {1}", 
                                       data.FirstName, data.LastName);
    }
    else
    {
         lblLogin.Text = "Not logged in...."; 
    }
}

数据库层

string strConn = WebConfigurationManager.ConnectionStrings["cloud2"].ConnectionString;

internal class FirstAndLastName
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

protected FirstAndLastName GetFirstAndLastName(string userName, string password)
{
    FirstAndLastName result = null;

    // if you only need FName und LName - then only select those two columns! 
    // don't just use SELECT * all the time because it's easy and because you're lazy
    string strCmd = "SELECT FName, LName FROM dbo.Person WHERE uname = @uname AND upass = @upass";

    // wrap everything into using blocks
    using(SqlConnection objConn = new SqlConnection(strConn))
    using(SqlCommand objCmd = new SqlCommand(strCmd, objConn))
    {
       objCmd.Parameters.AddWithValue("@uname", userName);
       objCmd.Parameters.AddWithValue("@upass", password);

       objConn.Open();

       using(SqlDataReader objDR = objCmd.ExecuteReader())
       {
          if (objDR.Read())
          {
              result = new FirstAndLastName();

              result.FirstName = objDR.GetString(objDR.GetOrdinal("FName"));
              result.LastName = objDR.GetString(objDR.GetOrdinal("LName"));
          }
       }
    } 

    return result;
}
于 2012-04-05T05:22:48.543 回答
1

首先,我会稍微重构一下您的逻辑,以将 UI VS 数据库调用稍微分开。其次,重构您的代码以检索使用连接字符串、用户名、密码和输出名和姓的代码的名字和姓氏。

string firstName, lastName;
if(ValidateUser(txtusername.Text, txtpassword.Text, out firstName, out lastName)
{
    //put your UI Logic and redirect in here
    lblLogin.Text = "Welcome: " + firstName + " " + " lastName;
}
else
{
    //login faild code
}

private bool ValidateUser(string strConn, string username, string password, out string firstName, out string lastName)
{
    firstName = string.Empty;
    lastName = string.Empty;

    using (var con = new SqlConnection(strConn))
    using (var cmd = con.CreateCommand())
    {
        cmd.CommandText = "Select * From Person Where uname=@uname and upass=@upass";
        cmd.Parameters.AddWithValue("@uname", username);
        cmd.Parameters.AddWithValue("@upass", password);

        con.Open();
        using (var r = cmd.ExecuteReader())
        {
            if (r.Read())
            {
                firstName = r["FName"].ToString();
                lastName = r["LName"].ToString();

                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
于 2012-04-05T05:00:40.487 回答
0
string Name=objDR ["FName"].ToString() +  ["LName"].ToString()
lblLogin.Text = "Logged in as:"+Name
于 2012-04-05T04:51:10.103 回答
0

SqlDataReader您可以通过以下方式阅读实例:

WebConfigurationManager.ConnectionStrings["cloud2"].ConnectionString; protected void btnlogin2_Click(object sender, EventArgs e) {

string strCmd = "Select * From Person Where uname=@uname and upass=@upass";
SqlConnection objConn = new SqlConnection(strConn);
SqlCommand objCmd = new SqlCommand(strCmd, objConn);
objCmd.Parameters.AddWithValue("@uname", txtusername.Text);
objCmd.Parameters.AddWithValue("@upass", txtpassword.Text);


using (objConn)
{
    objConn.Open();
    SqlDataReader objDR = objCmd.ExecuteReader();

    if (objDR.Read())
    {

        string fName= (string)objDR["FName"];
        string lName= (string)objDR["LName"];
        btnlogin2.Visible = false;
        txtusername.Visible = false;
        txtpassword.Visible = false;
       Response.Redirect("member.aspx");
        lblLogin.Text = "Logged in as: Test "+fName+" "+lName; //Display login name?


    }
    else
    {

        btnlogin2.Text = "Failed";

    }
} 
}
于 2012-04-05T04:52:00.087 回答
0

您的问题是如何从查询中只返回您想要的数据,或者如何从数据读取器访问它?

听起来您可能需要 ExecuteScalar(),并且您可以在查询中组合 fname 和 lastname 字段,这样您就可以只提取一个结果:

string strCmd = "Select Fname + ' ' + LName From Person Where uname=@uname and upass=@upass";
SqlConnection objConn = new SqlConnection(strConn);
SqlCommand objCmd = new SqlCommand(strCmd, objConn);
objCmd.Parameters.AddWithValue("@uname", txtusername.Text);
objCmd.Parameters.AddWithValue("@upass", txtpassword.Text);


using (objConn)
{
    objConn.Open();
    var username = objConn.ExecuteScalar() as string;

    if (!string.IsNullOrEmpty())
    {
        btnlogin2.Visible = false;
        txtusername.Visible = false;
        txtpassword.Visible = false;
       Response.Redirect("member.aspx");
        lblLogin.Text = "Logged in as:  " + username ; //Display login name?
    }

编辑:刚刚注意到一个细节......你在重定向后设置 lblLogin.Text ......所以不要认为任何人都会看到它。可以将其添加到查询字符串中并在 member.aspx 上显示...

于 2012-04-05T05:11:02.443 回答
-2

你可以做类似的事情

lblLogin.Text = "Logged in as: " + objDR.GetString(dr.GetOrdinal("FName")) + " " + objDR.GetString(dr.GetOrdinal("LName"));

如果有记忆,您可能还需要先调用objDR.Read();以开始遍历数据读取器。

于 2012-04-05T04:49:08.740 回答