[asp.net]显示登录用户的详细信息

发布时间: 2016/7/13 0:01:25
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在开发一个 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());
        }
    }

可以轻松地显示所有的客户会细节,但我不能弄清楚如何显示当前登录的用户。我将感谢任何帮助。

解决方法 1:

您需要获得相关数据的 UI。GridView控件使得这容易。它可以自动生成一个 HTML 表,基于绑定到它的数据。我们就要去声明 GridView 在标记中的,选择我们的数据到一个数据表中,然后绑定到 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();

你应该也包裹你 SqlConnectionusing 语句。

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();
官方微信
官方QQ群
31647020