无法将 sql 数据从不同的外部 table(2 tables)获取到列表视图中
Cant get sql data into listview from a different foreign table (2 tables)
几天来一直被同一个问题困扰,我一直在网上搜索答案。
我的问题是:在下面的代码中,我得到了两个具有 1 个外键的 table。我想显示 table 中的 nvarchar 而不是 ID。
在我的 aspx 页面中,我得到了一个 Listview。
<asp:ListView ID="ListView_Fruit" ItemType="DifferentFruit.Model.Fruit"
SelectMethod="ListView_GetData" DataKeyNames="FruitID" runat="server"
DeleteMethod="ListView_Del"
UpdateMethod="ListView_Update" >
<LayoutTemplate>
<table>
<tr>
<th>FruitID</th>
<th>Fruit Name</th>
<th>TypeOfFruitID</th>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Item.FruitID %></td>
<td><%# Item.FruitName %></td>
<td><%# Item.TypeOfFruitID %></td>
</tr>
</ItemTemplate>
数据库
Fruits
FruitID(int) Pk
FruitName nvarchar(20)
FruitsTypeID(int)
FruitsType
FruitsTypeID(int) (fk)
FruitType(nvarchar(20)
现在我显示存储过程中的每个水果。
Getting all data.
SELECT FruitID, FruitName, TypeOfFruitID
FROM Fruits
Getting all data from FruitsType
SELECT FruitsTypeID, FruitType FROM FruitsType
WHERE FruitsTypeID = CAST(FruitsTypeID as nvarchar(20))
如何显示 FruitsType 的数据?现在它只显示 FruitsTypeID 而不是 FruitsType 中的另一行。我要显示 FruitType
我的水果数据库层
public IEnumerable<Fruits> GettingAllFruits(int startRow, int maxRow, out int totalRows)
{
using (SqlConnection conn = Create())
{
SqlCommand cmd = new SqlCommand("appSchema.app_getFruits", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@PageIndex", startRow / maxRow + 1);
cmd.Parameters.Add("@PageSize", maxRow);
cmd.Parameters.Add("@RecordCount", ParameterDirection.Output)
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
int fruitID = reader.GetOrdinal("FruitID");
int fruitName = reader.GetOrdinal("FruitName");
int fruitType = reader.GetOrdinal("TypeOfFruitID");
while (reader.Read())
{
Fruits fruit = new Fruits();
fruit.fruitID = reader.GetInt32(fruitID);
fruit.fruitName = reader.GetString(fruitName);
fruit.typeOfFruitID = reader.GetInt32(fruitType);
fruitslist.Add(fruits);
}
}
totalRows = (int)cmd.Parameters["@RecordCount"].Value;
fruitlist.TrimExcess();
return fruitlist;
}
}
}
我的存储过程(更新)
BEGIN SELECT ROW_NUMBER()
OVER ( ORDER BY
[FruitID] DESC
)AS RowNumber
,[FruitID]
,[FruitName]
,[FruitsTypeID]
INTO #FruitsRes
FROM [Fruits]
INNER JOIN FruitsType ON Fruits.TypeOfFruitID = FruitsType.FruitsTypeID
WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
DROP TABLE #Results
END
存储过程没问题,但是当存储过程被回调回应用程序时,出现转换错误。也就是说不能从 'Apple' 转换为 int。
这里出错 -->
int fruitType = reader.GetOrdinal("TypeOfFruitID");
您基本上是在寻找 Inner Join,试试这个:-
SELECT FruitID, FruitName, FruitType
FROM Fruits INNER JOIN FruitsType
ON Fruits.TypeOfFruitID = FruitsType.FruitsTypeID
几天来一直被同一个问题困扰,我一直在网上搜索答案。
我的问题是:在下面的代码中,我得到了两个具有 1 个外键的 table。我想显示 table 中的 nvarchar 而不是 ID。
在我的 aspx 页面中,我得到了一个 Listview。
<asp:ListView ID="ListView_Fruit" ItemType="DifferentFruit.Model.Fruit"
SelectMethod="ListView_GetData" DataKeyNames="FruitID" runat="server"
DeleteMethod="ListView_Del"
UpdateMethod="ListView_Update" >
<LayoutTemplate>
<table>
<tr>
<th>FruitID</th>
<th>Fruit Name</th>
<th>TypeOfFruitID</th>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Item.FruitID %></td>
<td><%# Item.FruitName %></td>
<td><%# Item.TypeOfFruitID %></td>
</tr>
</ItemTemplate>
数据库
Fruits
FruitID(int) Pk
FruitName nvarchar(20)
FruitsTypeID(int)
FruitsType
FruitsTypeID(int) (fk)
FruitType(nvarchar(20)
现在我显示存储过程中的每个水果。
Getting all data.
SELECT FruitID, FruitName, TypeOfFruitID
FROM Fruits
Getting all data from FruitsType
SELECT FruitsTypeID, FruitType FROM FruitsType
WHERE FruitsTypeID = CAST(FruitsTypeID as nvarchar(20))
如何显示 FruitsType 的数据?现在它只显示 FruitsTypeID 而不是 FruitsType 中的另一行。我要显示 FruitType
我的水果数据库层
public IEnumerable<Fruits> GettingAllFruits(int startRow, int maxRow, out int totalRows)
{
using (SqlConnection conn = Create())
{
SqlCommand cmd = new SqlCommand("appSchema.app_getFruits", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@PageIndex", startRow / maxRow + 1);
cmd.Parameters.Add("@PageSize", maxRow);
cmd.Parameters.Add("@RecordCount", ParameterDirection.Output)
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
int fruitID = reader.GetOrdinal("FruitID");
int fruitName = reader.GetOrdinal("FruitName");
int fruitType = reader.GetOrdinal("TypeOfFruitID");
while (reader.Read())
{
Fruits fruit = new Fruits();
fruit.fruitID = reader.GetInt32(fruitID);
fruit.fruitName = reader.GetString(fruitName);
fruit.typeOfFruitID = reader.GetInt32(fruitType);
fruitslist.Add(fruits);
}
}
totalRows = (int)cmd.Parameters["@RecordCount"].Value;
fruitlist.TrimExcess();
return fruitlist;
}
}
}
我的存储过程(更新)
BEGIN SELECT ROW_NUMBER()
OVER ( ORDER BY
[FruitID] DESC
)AS RowNumber
,[FruitID]
,[FruitName]
,[FruitsTypeID]
INTO #FruitsRes
FROM [Fruits]
INNER JOIN FruitsType ON Fruits.TypeOfFruitID = FruitsType.FruitsTypeID
WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
DROP TABLE #Results
END
存储过程没问题,但是当存储过程被回调回应用程序时,出现转换错误。也就是说不能从 'Apple' 转换为 int。 这里出错 -->
int fruitType = reader.GetOrdinal("TypeOfFruitID");
您基本上是在寻找 Inner Join,试试这个:-
SELECT FruitID, FruitName, FruitType
FROM Fruits INNER JOIN FruitsType
ON Fruits.TypeOfFruitID = FruitsType.FruitsTypeID