dongshubang7816 2015-03-27 10:03
浏览 65
已采纳

选择两个连接ID的表。 ASP.NET中的PHP代码替代方案

I'm starting in ASP.NET. I need to write data from two tables that are linked via ID. I would like to write in the cycle. Example: I have a table States and Cities table: 1.United states     a. New York     b. Washington     c. Los Angeles     d. Chicago     e. Houston 2. Russia     a. Moscow     b. St. Petersburg     c. Omsk     d. Kazan 3. France     a. Paris     b. Lyon     c. Marseille

In PHP I solve this problem as follows `

// 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']);
   }
}

Alternatively, just advice on how the operation is called, I do not know how to properly ask this in search

I tried Repeater in repeater but i have problem withI tried repeater in repeater, but I have a problem with passing parameters given line Repeater in Repeater

I tried also in DataReader DataReader, but this is an open DataReader reports an error. C# MySQL second DataReader in DataReader while loop

I also tried a treeview, but I am in the city must have a name and the name of the State, the same "name" and "name" which I can not have this. Treeview validation

I now tried good code

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>

but, I can't add parameter to first select. I tried

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

but program have error message "this constraint cannot be enabled as not all values have corresponding parent values." this example i see here

  • 写回答

2条回答 默认 最新

  • douxi5940 2015-04-08 06:44
    关注

    I have answer, add "false" behin relations

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图