Select 两个表用ID连接。 PHP ASP.NET 中的替代代码
Select two Tables connect with ID. PHP code alternative in ASP.NET
我从 ASP.NET 开始。我需要从通过 ID 链接的两个 table 写入数据。我想写在循环中。
示例:我有 table 个州和城市 table:
1.United 个州
一个。纽约
b.华盛顿
C。洛杉矶
d.芝加哥
e.休斯顿
2. 俄罗斯
一个。莫斯科
b.圣彼得堡
C。鄂木斯克
d.喀山
3. 法国
一个。巴黎
b.里昂
C。马赛
在PHP我解决这个问题如下
`
// cycle cities
$sql = mssql_query("SELECT * FROM States");
while($row = mssql_fetch_assoc($sql))
{
$id_state = row['ID_State'];
echo ($ row ['Name_State']);
// cycle city with id state
$sql_city = mssql_query("SELECT * FROM City WHERE ID_State = '. $id_state.'");
while ($row_city = mssql_fetch_assoc($sql_city))
{
echo($row['Name_city']);
}
}
或者,只是关于如何调用该操作的建议,我不知道如何在搜索中正确询问这个问题
我在中继器中尝试了中继器,但我有问题我在中继器中尝试了中继器,但我在给定行传递参数时遇到问题
Repeater in Repeater
我在DataReader中也试过DataReader,但是这是一个打开的DataReader报错。
C# MySQL second DataReader in DataReader while loop
我也试过一个treeview,但是我在city和state的名字必须要有,一样的"name"和"name"里面我不能有这个。
Treeview validation
我现在尝试了好的代码
select.aspx.cs
SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM V_Dic", cnn);
//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds, "Dic");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from T_Obdobi", cnn);
cmd2.Fill(ds, "Obdobi");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd3 = new SqlDataAdapter("select * from V_AktualizaceDic", cnn);
cmd3.Fill(ds, "OsCislo");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("Obdobi",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["Obdobi"].Columns["ID_Dic"]);
//ds.Relations.add
ds.Relations.Add("OsCislo",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["OsCislo"].Columns["ID_Dic"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["Dic"];
Page.DataBind();
select.aspx
<asp:Repeater ID="parentRepeater" runat="server">
<ItemTemplate>
<div class="accordion">
<div class="hlavni">
<%# DataBinder.Eval(Container.DataItem,"Dic") %>
</div>
<div class="rozbalovany">
<table>
<tr>
<td>Osobní číslo: </td>
<td><%# DataBinder.Eval(Container.DataItem,"OsCislo") %></td>
</tr>
<tr>
<td>Šetřené zdaňovací období: </td>
<td>
<div class="box2">
<!-- start child repeater -->
<asp:Repeater ID="childRepeater" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("Obdobi") %>'
runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "[\"Obdobi\"]")%><br />
</ItemTemplate>
</asp:Repeater>
</div>
</td>
</tr>
</table>
<h3>Aktualizace:</h3>
<!-- end child repeater -->
<div class="box">
<table class="aktualizace" style="border: 1px solid #e9e9e9">
<tr>
<td><strong>Osobní číslo</strong></td>
<td>Jméno a Příjmení</td>
<td>Datum Aktualizace</td>
<td>Poznámka</td>
<td>Šetřené zdaňovací období</td>
<td>Změna Řešitele</td>
<td>Změna Plné moci</td>
<td>Záznam v Insolvenčním řejstříku</td>
<td>Významná změna s vazbou na data v OR</td>
<td>Jiná významná změna</td>
</tr>
<asp:Repeater ID="childRepeater2" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("OsCislo") %>'
runat="server">
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "[\"OsCislo\"]")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
但是,我无法将参数添加到第一个 select。我试过了
cmd1.SelectCommand.Parameters.Add("@EvCislo", SqlDbType.NVarChar, 50, "EvCislo");
但是程序有错误信息"this constraint cannot be enabled as not all values have corresponding parent values."
这个例子我看到 here
您可以使用 JOINS 从两个使用 ID 的表中获取数据,例如
SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id = table2.id WHERE table1.id = '1';
我有答案,在关系后面加上"false"
SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM V_Dic", cnn);
//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds, "Dic");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from T_Obdobi", cnn);
cmd2.Fill(ds, "Obdobi");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd3 = new SqlDataAdapter("select * from V_AktualizaceDic", cnn);
cmd3.Fill(ds, "OsCislo");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("Obdobi",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["Obdobi"].Columns["ID_Dic"], false);
//ds.Relations.add
ds.Relations.Add("OsCislo",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["OsCislo"].Columns["ID_Dic"], false);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["Dic"];
Page.DataBind();
我从 ASP.NET 开始。我需要从通过 ID 链接的两个 table 写入数据。我想写在循环中。 示例:我有 table 个州和城市 table: 1.United 个州 一个。纽约 b.华盛顿 C。洛杉矶 d.芝加哥 e.休斯顿 2. 俄罗斯 一个。莫斯科 b.圣彼得堡 C。鄂木斯克 d.喀山 3. 法国 一个。巴黎 b.里昂 C。马赛
在PHP我解决这个问题如下 `
// cycle cities
$sql = mssql_query("SELECT * FROM States");
while($row = mssql_fetch_assoc($sql))
{
$id_state = row['ID_State'];
echo ($ row ['Name_State']);
// cycle city with id state
$sql_city = mssql_query("SELECT * FROM City WHERE ID_State = '. $id_state.'");
while ($row_city = mssql_fetch_assoc($sql_city))
{
echo($row['Name_city']);
}
}
或者,只是关于如何调用该操作的建议,我不知道如何在搜索中正确询问这个问题
我在中继器中尝试了中继器,但我有问题我在中继器中尝试了中继器,但我在给定行传递参数时遇到问题 Repeater in Repeater
我在DataReader中也试过DataReader,但是这是一个打开的DataReader报错。 C# MySQL second DataReader in DataReader while loop
我也试过一个treeview,但是我在city和state的名字必须要有,一样的"name"和"name"里面我不能有这个。 Treeview validation
我现在尝试了好的代码
select.aspx.cs
SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM V_Dic", cnn);
//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds, "Dic");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from T_Obdobi", cnn);
cmd2.Fill(ds, "Obdobi");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd3 = new SqlDataAdapter("select * from V_AktualizaceDic", cnn);
cmd3.Fill(ds, "OsCislo");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("Obdobi",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["Obdobi"].Columns["ID_Dic"]);
//ds.Relations.add
ds.Relations.Add("OsCislo",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["OsCislo"].Columns["ID_Dic"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["Dic"];
Page.DataBind();
select.aspx
<asp:Repeater ID="parentRepeater" runat="server">
<ItemTemplate>
<div class="accordion">
<div class="hlavni">
<%# DataBinder.Eval(Container.DataItem,"Dic") %>
</div>
<div class="rozbalovany">
<table>
<tr>
<td>Osobní číslo: </td>
<td><%# DataBinder.Eval(Container.DataItem,"OsCislo") %></td>
</tr>
<tr>
<td>Šetřené zdaňovací období: </td>
<td>
<div class="box2">
<!-- start child repeater -->
<asp:Repeater ID="childRepeater" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("Obdobi") %>'
runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "[\"Obdobi\"]")%><br />
</ItemTemplate>
</asp:Repeater>
</div>
</td>
</tr>
</table>
<h3>Aktualizace:</h3>
<!-- end child repeater -->
<div class="box">
<table class="aktualizace" style="border: 1px solid #e9e9e9">
<tr>
<td><strong>Osobní číslo</strong></td>
<td>Jméno a Příjmení</td>
<td>Datum Aktualizace</td>
<td>Poznámka</td>
<td>Šetřené zdaňovací období</td>
<td>Změna Řešitele</td>
<td>Změna Plné moci</td>
<td>Záznam v Insolvenčním řejstříku</td>
<td>Významná změna s vazbou na data v OR</td>
<td>Jiná významná změna</td>
</tr>
<asp:Repeater ID="childRepeater2" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("OsCislo") %>'
runat="server">
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "[\"OsCislo\"]")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
但是,我无法将参数添加到第一个 select。我试过了
cmd1.SelectCommand.Parameters.Add("@EvCislo", SqlDbType.NVarChar, 50, "EvCislo");
但是程序有错误信息"this constraint cannot be enabled as not all values have corresponding parent values." 这个例子我看到 here
您可以使用 JOINS 从两个使用 ID 的表中获取数据,例如
SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id = table2.id WHERE table1.id = '1';
我有答案,在关系后面加上"false"
SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM V_Dic", cnn);
//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds, "Dic");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from T_Obdobi", cnn);
cmd2.Fill(ds, "Obdobi");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd3 = new SqlDataAdapter("select * from V_AktualizaceDic", cnn);
cmd3.Fill(ds, "OsCislo");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("Obdobi",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["Obdobi"].Columns["ID_Dic"], false);
//ds.Relations.add
ds.Relations.Add("OsCislo",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["OsCislo"].Columns["ID_Dic"], false);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["Dic"];
Page.DataBind();