【问题标题】:How to replace IDs of users with their fullNames in the current scenario?如何在当前场景中将用户的 ID 替换为全名?
【发布时间】:2018-05-21 23:29:02
【问题描述】:

我有两个具有以下架构的数据库:

tbl_users(userID, fullName, emailID, password)

userID 是主键

tbl_savings(savingsID, creditorName, amount, interestRate, interestType, interestCalculationMethod, ownerID, dataEnteredByID) 

savingsID 是主键。

ownerID 是具有一对多关系的 tbl_users userID 上的外键。

ownerID 指定保存所有者的 ID。同样,dataEnteredByID 指定输入数据的人的 ID。

我有一个具有以下规范的 GridView:

<asp:GridView ID="GridViewSavingsTracker" AutoGenerateColumns="false" runat="server" DataKeyNames="savingsID" OnRowCommand="GridViewSavingsTracker_RowCommand" AllowPaging="true" OnSelectedIndexChanged="GridViewSavingsTracker_SelectedIndexChanged" OnPageIndexChanging="GridViewSavingsTracker_PageIndexChanging" PageSize="10">
    <Columns>
        <asp:CommandField ShowSelectButton="true" />
        <asp:BoundField DataField="creditorName" HeaderText="Creditor Name" />
        <asp:BoundField DataField="amount" HeaderText="Principal Amount" />
        <asp:BoundField DataField="interestRate" HeaderText="Interest Rate" />
        <asp:BoundField DataField="interestType" HeaderText="Interest Type" />
        <asp:BoundField DataField="interestCalculationMethod" HeaderText="Interest Calculation Method" />
        <asp:BoundField DataField="insertedDate" HeaderText="Date" />
        <asp:BoundField DataField="ownerID" HeaderText="Owner" />
        <asp:BoundField DataField="dataEnteredByID" HeaderText="Data Entered By" />

    </Columns>
</asp:GridView>

我想要做什么:我不想打印 ownerID 和 dataEnteredByID,而是想打印相应 ID 的 fullNames。我该怎么做?

我已经拥有的:我有以下用于 GridView 的服务器端代码。我有一个方法 ShowGridView() 将数据从数据库填充到 GridView 中。

protected void ShowGridView()
{
    string connectionString = GetConnString();
    OleDbConnection con = new OleDbConnection(connectionString);
    con.Open();

    string showGridViewQuery = "(SELECT s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName FROM tbl_savings s LEFT JOIN tbl_users u ON s.ownerID = u.usersID) UNION (select s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName from tbl_savings s LEFT JOIN tbl_users u ON s.dataEnteredByID = u.usersID)";

    OleDbCommand showGridViewCommand = new OleDbCommand(showGridViewQuery, con);
    showGridViewCommand.ExecuteNonQuery();
    DataTable dt = new DataTable();
    OleDbDataAdapter olda = new OleDbDataAdapter(showGridViewCommand);
    olda.Fill(dt);

    GridViewSavingsTracker.DataSource = dt;
    GridViewSavingsTracker.DataBind();

    con.Close();
}

【问题讨论】:

  • 您不需要UNIONJOIN储​​值表与你的用户表两次,一次获取所有者的姓名,一次获取输入者的姓名。
  • SELF JOIN 不会解决我的问题,因为我不是在寻找具有相同 ID 或同名的人。我已经在输入这两列的 ID。我只想打印相应的全名,而不是打印 ID。
  • 我没有谈论自我加入。再读一遍。
  • 它说找不到页面
  • 抱歉,这是私人的。再次,这次公开:rextester.com/YCYA27221

标签: sql asp.net webforms oledb


【解决方案1】:

您可以创建一个单独的代码隐藏方法来获取全名并从 gridview 中调用它。

为该字段创建一个 ItemTemplate。 Text 属性调用该方法。将 UserID 替换为您的字段名称。

<ItemTemplate>
    <asp:Label ID="lblUserNameItem" runat="server" 
        Text='<%# GetUserNameByID(Eval("UserID").ToString()) %>'></asp:Label>
</ItemTemplate>

后面的代码:

protected string GetUserNameByID(string userID)
{
    // your data-code...
    ... uname = GetUserByID(int.Parse(userID));
    return uname;
}

对另一个 id 做同样的事情,用同样的方法或另一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多