显示登录用户详细信息

时间:2015-01-26 17:02:26

标签: c# sql asp.net visual-studio

我目前正在开发一个Web应用程序,我想显示当前登录的用户详细信息。当用户登录时,我创建了一个新会话,我正在尝试使用它来验证当前用户并显示他们的详细信息。 C#代码如下所示。

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            conn.Open();
            string detailsQuery = "select * FROM [Customer] where Customer_No ='" + Session["New"] + "'";
            SqlCommand com = new SqlCommand(detailsQuery, conn);
            com.ExecuteNonQuery();                
            Response.Write("Details Showing");
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Error:" + ex.ToString());
        }
    }

我可以轻松地显示所有客户的详细信息,但我无法弄清楚如何显示当前登录的用户。我将不胜感激任何帮助。

2 个答案:

答案 0 :(得分:1)

您需要将数据导入UI。 GridView控件使这很容易。它可以根据绑定到它的数据自动生成HTML表。我们将在标记中声明一个GridView,将我们的数据选择为DataTable,然后将DataTable绑定到GridView。

<asp:GridView runat="server" id="CustomerDetailsGV" AutoGenerateColumns="true" />

代码背后:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string detailsQuery = "select * FROM [Customer] where Customer_No ='" + Session["New"] + "'";
SqlCommand com = new SqlCommand(detailsQuery, conn);
DataTable dt = new DataTable();
dt.Load(com.ExecuteReader());   
CustomerDetailsGV.DataSource = dt;
CustomerDetailsGV.DataBind();             
conn.Close();

您还应该将SqlConnection包装在using声明中。

string detailsQuery = "select * FROM [Customer] where Customer_No ='" + Session["New"] + "'";
SqlCommand com = new SqlCommand(detailsQuery);
DataTable dt = new DataTable();
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)
{
    com.Connection = conn;
    conn.Open();                
    dt.Load(com.ExecuteReader());                       
}
CustomerDetailsGV.DataSource = dt;
CustomerDetailsGV.DataBind();

您可能会发现这是一个有用的功能:

/// <summary>
/// Executes a database command with the specified connection and returns a data table synchronously.
/// </summary>
/// <param name="command">The command to execute.</param>
/// <param name="connection">The connection to use.</param>
/// <returns>A DataTable representing the command results.</returns>
public static DataTable GetDataTable(SqlCommand command, SqlConnection connection)
{
    DataTable dt = new DataTable();
    command.Connection = connection;
    using (connection)
    {
        connection.Open();
        dt.Load(command.ExecuteReader());
    }
    return dt;
}

使用上述功能,您的代码可以简化为:

string detailsQuery = "select * FROM [Customer] where Customer_No ='" + Session["New"] + "'";
SqlCommand com = new SqlCommand(detailsQuery);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
CustomerDetailsGV.DataSource = GetDataTable(com, con);
CustomerDetailsGV.DataBind();

答案 1 :(得分:-1)

您的网页上的每个人都可以通过Session.SessionID字符串进行标识。 Microsoft Session.sessionID explanation。在Customer表中,添加SessionID字段(SQL Varchar(70))。当客户登录时,更新该客户的session.sessionID。如果要将当前客户的信息显示在屏幕上,只需提供他们的Session.sessionID。

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        conn.Open();
        string detailsQuery = "select * FROM [Customer] where SessionID ='" + Session.SessionID + "'";
        SqlCommand com = new SqlCommand(detailsQuery, conn);
        com.ExecuteNonQuery();                
        Response.Write("Details Showing");
        conn.Close();
    }
    catch (Exception ex)
    {
        Response.Write("Error:" + ex.ToString());
    }
}
相关问题