在asp.net中建立gridview数据之间的关系

时间:2011-11-14 03:57:35

标签: c# asp.net sql-server entity-relationship

我有一个方法,它将使用sql表中的数据填充数据表,而sql表又用于填充gridview。现在在sql表中我有一个名为" hotel"其中包含" ID"一家酒店与另一张名为"酒店"与酒店ID和名称。

现在在我的网格视图中,我想显示酒店名称而不是酒店ID。我怎么能这样做。

 public static DataTable GetRequests(string empid)
        {
            DataTable dt = new DataTable();
            string strConnection = ConfigurationManager.AppSettings["connStr"];
            using (SqlConnection connection = new SqlConnection(strConnection))
            {
                connection.Open();
                SqlCommand sqlcmd = new SqlCommand();
                SqlDataAdapter sAdap = new SqlDataAdapter();                
                sqlcmd.Connection = connection;
                sqlcmd.CommandType = System.Data.CommandType.Text;
                sqlcmd.CommandText = "Select request_date,hotel,dining_date,status from requests Where emp_id='" + empid + "'";
                sAdap.SelectCommand = sqlcmd;
                sAdap.Fill(dt);
            }
            return dt;            
        }

这是检索记录的方法。酒店字段包含我想要名称的ID。

2 个答案:

答案 0 :(得分:1)

您必须使用Joins

答案 1 :(得分:1)

更改为您的SQL查询,事情就会结束。

让我们说'Hotel'包含酒店ID,在另一个表'HotelDetails中包含酒店ID和酒店名称。

酒店的表结构   HotelId int HotelDetails的表结构   HotelId int,   酒店名称varchar(10)

现在你的查询应该是

SELECT b.HotelName as HotelName, c.request_date as RequestDate,c.dining_date as DiningDate ,c.status as Status FROM Hotel a, HotelDetails b, requests c WHERE a.HotelId = b.HotelId and emp_id='" + empid + "'

在GridView中,您的DataFild应该是HotelName才能显示酒店名称

例如

<Columns>
                                                            <asp:TemplateField HeaderText="Slno">
                                                                <ItemTemplate>
                                                                    <%# Container.DataItemIndex + 1 %>
                                                                </ItemTemplate>
                                                                <ControlStyle Width="30px" />
                                                                <ItemStyle ForeColor="#00846F" Width="30px" />
                                                            </asp:TemplateField>
                                                            <asp:BoundField ControlStyle-Width="90" DataField="HotelName" 
                                                                HeaderText="Hotel Name" ItemStyle-ForeColor="#00846F" 
                                                                ItemStyle-Width="30">
                                                                <ControlStyle Width="30px" />
                                                                <ItemStyle Width="30px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="190" 
                                                                DataField="RequestDate" HeaderText="Request Date" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="100">
                                                                <ControlStyle Width="100px" />
                                                                <ItemStyle Width="100px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="100" 
                                                                DataField="DiningDate" HeaderText="Dining Date" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="50">
                                                                <ControlStyle Width="50px" />
                                                                <ItemStyle Width="50px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="100" 
                                                                DataField="Status" HeaderText="Status" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="50">
                                                                <ControlStyle Width="50px" />
                                                                <ItemStyle Width="50px" />
                                                            </asp:BoundField>



                                                        </Columns>

如果您发现它有用,请将其标记为您的答案,否则请告诉我....