【问题标题】:Select two Tables connect with ID. PHP code alternative in ASP.NET选择两个与 ID 连接的表。 ASP.NET 中的 PHP 代码替代方案
【发布时间】:2015-03-27 10:03:09
【问题描述】:

我从 ASP.NET 开始。我需要从通过 ID 链接的两个表中写入数据。我想在循环中写。 示例:我有一个表 States 和 Cities 表: 1.美国 一种。纽约 湾。华盛顿 C。洛杉矶 d。芝加哥 e.休斯顿 2. 俄罗斯 一种。莫斯科 湾。圣彼得堡 C。鄂木斯克 d。喀山 3. 法国 一种。巴黎 湾。里昂 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必须有一个name和state的name,同样的“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();

选择.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>

但是,我无法将参数添加到第一个选择。我试过了

cmd1.SelectCommand.Parameters.Add("@EvCislo", SqlDbType.NVarChar, 50, "EvCislo");

但程序有错误消息“无法启用此约束,因为并非所有值都有对应的父值。” 这个例子我看到here

【问题讨论】:

  • 请编辑您的问题

标签: c# php asp.net sql-server treeview


【解决方案1】:

您可以使用 JOINS 从两个使用 ID 之类的表中获取数据

SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id = table2.id WHERE table1.id = '1';

【讨论】:

  • 谢谢,但我想谢谢,但我想你的问题回到表 1。行美国纽约2。行美国华盛顿3。行美国洛杉矶4。行美国芝加哥...下一行俄罗斯莫斯科下一个俄罗斯圣彼得堡,但我需要&lt;div&gt;&lt;h1&gt;United states&lt;/h1&gt; &lt;p&gt;new york&lt;/p&gt; &lt;p&gt;washington&lt;/p&gt;&lt;/div&gt; &lt;div&gt;&lt;h1&gt;Russia&lt;/h1&gt; &lt;p&gt;Moscow&lt;/p&gt; &lt;p&gt;Omsk&lt;/p&gt;&lt;/div&gt;
  • 你能把两张表的结构发给我吗?我没有得到你。
  • 谢谢你的回答,但我的数据库没有问题。我需要在我的 asp.net 应用程序中循环。现在我需要在我的数据库询问中添加任何“位置”。如果我将 Where to code SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM V_Dic", cnn); 添加到我的代码中,应用程序会写入错误消息“无法启用此约束,因为并非所有值都有相应的父值。”。你能帮我解决这个问题吗?
【解决方案2】:

我有答案,在关系后面加“假”

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2011-08-21
    • 2015-07-08
    相关资源
    最近更新 更多