一次从数据表中显示一些数据(不分页)

时间:2016-02-01 09:45:52

标签: c# asp.net datatable

我有一个数据表,其中 4行但是

  

我想一次只显示2个数据。并且在3秒之后,将显示另外两个

这是我的代码: -

protected void GetDatafortable1()
{
    lblpltfrm_Number.Text = "PlatForm 1";
    list.InnerHtml = "";
    dv_FromStop.InnerHtml = "";
    Dv_ToStop.InnerHtml = "";
    dv_Time.InnerHtml = "";
    dv_status.InnerHtml = "";
    // int svalue = Convert.ToInt32(Session["ReloadValue"]);
    DataTable obj_Dt = new DataTable();
    OracleConnection obj_Connection = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConn"].ToString());

    string Query = "Select x.SR_NO,x.FROM_STOP,x.TO_STOP,x.ORIGIN_STOP_TIME from XXACL_PN_BUS_TIMETABLE x WHERE SCREEN_NUMBER=1";
    using (OracleCommand obj_Command = new OracleCommand(Query))
    {
        OracleDataAdapter obj_Adapter = new OracleDataAdapter(obj_Command);
        obj_Command.Connection = obj_Connection;
        obj_Adapter.SelectCommand = obj_Command;
        obj_Adapter.Fill(obj_Dt);
        int Count = obj_Dt.Rows.Count;
        for (int i = 0; i < obj_Dt.Rows.Count; i++)
        {

            list.InnerHtml = list.InnerHtml + "<br />" +
                 obj_Dt.Rows[i]["SR_NO"];
            dv_FromStop.InnerHtml = dv_FromStop.InnerHtml + "<br />" +
                obj_Dt.Rows[i]["FROM_STOP"];
            Dv_ToStop.InnerHtml = Dv_ToStop.InnerHtml + "<br />" +
                obj_Dt.Rows[i]["TO_STOP"];
            dv_Time.InnerHtml = dv_Time.InnerHtml + "<br />" +
                obj_Dt.Rows[i]["ORIGIN_STOP_TIME"];
        }

        //Data1Arrived = true;
    }
}

注意时间由Timer

处理
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="3000" Enabled="true" />

HTML

<table style="width: 100%; height: 550px; text-align: center; border-collapse: collapse;"
            runat="server" id="tbl1Details">
            <colgroup>
                <col width="5%" />
                <col width="45%" />
            </colgroup>
            <tr>
                <td colspan="4" style="height: 10px; border: 1px solid black;">
                    <asp:Label ID="lblpltfrm_Number" Style="height: 10px; font-size: 20px; font-weight: bold;"
                        runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td style="height: 10px; font-size: 20px; font-weight: bold; border: 1px solid black;">
                    SR.no
                </td>
                <td style="height: 10px; font-size: 20px; font-weight: bold; border: 1px solid black;">
                    Stop
                </td>
                <td style="height: 10px; font-size: 20px; font-weight: bold; border: 1px solid black;">
                    Time
                </td>
                <td style="height: 10px; font-size: 20px; font-weight: bold; border: 1px solid black;">
                    Status
                </td>
            </tr>
            <tr>
                <td id="tTime" runat="server" style="vertical-align: top; border: 1px solid black;">
                    <div id="list" runat="server" style="font-size: 25px;">
                    </div>
                </td>
                <td>
                    <table style="width: 100%; height: 551px; border-collapse: collapse;">
                        <colgroup>
                            <col width="50%" />
                            <col width="50%" />
                        </colgroup>
                        <tr style="vertical-align: top;">
                            <td style="vertical-align: top; border: 0 solid black; text-align: left;">
                                <div id="dv_FromStop" runat="server" style="font-size: 25px;">
                                </div>
                            </td>
                            <td style="vertical-align: top; border: 0 solid black; text-align: left;">
                                <div id="Dv_ToStop" runat="server" style="font-size: 25px;">
                                </div>
                            </td>
                        </tr>
                    </table>
                </td>
                <td style="vertical-align: top; border: 1px solid black;">
                    <div id="dv_Time" runat="server" style="font-size: 25px;">
                    </div>
                </td>
                <td style="vertical-align: top; border: 1px solid black;">
                    <div id="dv_status" runat="server" style="font-size: 25px; vertical-align: top;">
                    </div>
                </td>
            </tr>
        </table>

我工作的早期代码,但我现在不想使用[会话]

using (OracleCommand obj_Command = new OracleCommand(Query))
    {
        OracleDataAdapter obj_Adapter = new OracleDataAdapter(obj_Command);
        obj_Command.Connection = obj_Connection;
        obj_Adapter.SelectCommand = obj_Command;
        obj_Adapter.Fill(obj_Dt);
        int Count = obj_Dt.Rows.Count;
        for (int i = 0; i < obj_Dt.Rows.Count; i++)
        {
            if (i < 2)
            {
                list.InnerHtml = list.InnerHtml + "<br />" +
                     obj_Dt.Rows[i]["SR_NO"];
                dv_FromStop.InnerHtml = dv_FromStop.InnerHtml + "<br />" +
                    obj_Dt.Rows[i]["FROM_STOP"];
                Dv_ToStop.InnerHtml = Dv_ToStop.InnerHtml + "<br />" +
                    obj_Dt.Rows[i]["TO_STOP"];
                dv_Time.InnerHtml = dv_Time.InnerHtml + "<br />" +
                    obj_Dt.Rows[i]["ORIGIN_STOP_TIME"];
            }
            else
            {
                Session["NextPlatForm"] = 1;
                Session["A"] = "PlatForm 1";
                dv2SRNo.InnerHtml = dv2SRNo.InnerHtml + "<br />" +
                 obj_Dt.Rows[i]["SR_NO"];
                dv2From_Stop.InnerHtml = dv2From_Stop.InnerHtml + "<br />" +
                    obj_Dt.Rows[i]["FROM_STOP"];
                dv2ToStop.InnerHtml = dv2ToStop.InnerHtml + "<br />" +
                    obj_Dt.Rows[i]["TO_STOP"];
                dv2Time.InnerHtml = dv2Time.InnerHtml + "<br />" +
                    obj_Dt.Rows[i]["ORIGIN_STOP_TIME"];


            }
        }
    }

II更新

private void UpdateHtmlData()
{
    DataTable dt = (DataTable)Session["TimeTable"];
    int index = (int)Session["Index"];
    for (int i = index; i <= index+1 && i < dt.Rows.Count; i++)
            {
                list.InnerHtml = list.InnerHtml + "<br />" +
                     dt.Rows[i]["SR_NO"];
                dv_FromStop.InnerHtml = dv_FromStop.InnerHtml + "<br />" +
                   dt.Rows[i]["FROM_STOP"];
                Dv_ToStop.InnerHtml = Dv_ToStop.InnerHtml + "<br />" +
                    dt.Rows[i]["TO_STOP"];
                dv_Time.InnerHtml = dv_Time.InnerHtml + "<br />" +
                    dt.Rows[i]["ORIGIN_STOP_TIME"];
            }
    Session["Index"] = index + 2;
}

另见方法

protected void GetDatafortable1()
{
    lblpltfrm_Number.Text = "PlatForm 1";
    list.InnerHtml = "";
    dv_FromStop.InnerHtml = "";
    Dv_ToStop.InnerHtml = "";
    dv_Time.InnerHtml = "";
    dv_status.InnerHtml = "";
    // int svalue = Convert.ToInt32(Session["ReloadValue"]);
    DataTable obj_Dt = new DataTable();
    OracleConnection obj_Connection = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConn"].ToString());

    string Query = "Select x.SR_NO,x.FROM_STOP,x.TO_STOP,x.ORIGIN_STOP_TIME from XXACL_PN_BUS_TIMETABLE x WHERE SCREEN_NUMBER=1";
    using (OracleCommand obj_Command = new OracleCommand(Query))
    {
        OracleDataAdapter obj_Adapter = new OracleDataAdapter(obj_Command);
        obj_Command.Connection = obj_Connection;
        obj_Adapter.SelectCommand = obj_Command;
        obj_Adapter.Fill(obj_Dt);

        Session["TimeTable"] = obj_Dt;
        Session["Index"] = 0;
    }
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    tbl1Details.Visible = false;
    tbl2Details.Visible = false;
    tbl3Details.Visible = false;

    if (Data1Arrived)
    {
        tbl1Details.Visible = true;
        Data1Arrived = false;
        Data2Arrived = true;
        Data3Arrived = false;
        lblpltfrm_Number.Text = "PlatForm 1";
        GetDatafortable1();
        UpdateHtmlData();
    }}

Page_load

 protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        GetDatafortable1();
        GetDatafortable2();
        GetDatafortable3();

        UpdateHtmlData();}

2 个答案:

答案 0 :(得分:1)

您可以移动逻辑以在单独的方法中调用oracle并将结果数据表存储在ViewState或Session中...还存储索引,该索引将指示已读取的行数...然后有一个单独的方法将更新UI计时器勾选

protected void GetDatafortable1()
{
    lblpltfrm_Number.Text = "PlatForm 1";
    list.InnerHtml = "";
    dv_FromStop.InnerHtml = "";
    Dv_ToStop.InnerHtml = "";
    dv_Time.InnerHtml = "";
    dv_status.InnerHtml = "";
    // int svalue = Convert.ToInt32(Session["ReloadValue"]);
    DataTable obj_Dt = new DataTable();
    OracleConnection obj_Connection = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConn"].ToString());

    string Query = "Select x.SR_NO,x.FROM_STOP,x.TO_STOP,x.ORIGIN_STOP_TIME from XXACL_PN_BUS_TIMETABLE x WHERE SCREEN_NUMBER=1";
    using (OracleCommand obj_Command = new OracleCommand(Query))
    {
        OracleDataAdapter obj_Adapter = new OracleDataAdapter(obj_Command);
        obj_Command.Connection = obj_Connection;
        obj_Adapter.SelectCommand = obj_Command;
        obj_Adapter.Fill(obj_Dt);
       ViewState["TimeTable"] = obj_Dt;
       ViewState["Index"] = 0; 

        //Data1Arrived = true;
    }
}

private void UpdateHtml()
{
DataTable dt = (DataTable) ViewState["TimeTable"]
int index = (int) ViewState["Index"];
for (int i = index; i <= index+1 && i < dt.Rows.Count; i++)
        {

            list.InnerHtml = list.InnerHtml + "<br />" +
                 dt.Rows[i]["SR_NO"];
            dv_FromStop.InnerHtml = dv_FromStop.InnerHtml + "<br />" +
               dt.Rows[i]["FROM_STOP"];
            Dv_ToStop.InnerHtml = Dv_ToStop.InnerHtml + "<br />" +
                dt.Rows[i]["TO_STOP"];
            dv_Time.InnerHtml = dv_Time.InnerHtml + "<br />" +
                dt.Rows[i]["ORIGIN_STOP_TIME"];
        }

ViewState["Index"] = index + 2;

}

proctected void page_load()
{  
 if(!IsPostBack())
  {
   GetDatafortable1();
   UpdateHtml();
  }
}
 proctected void timer1_tick()
{
UpdateHtml();
}

答案 1 :(得分:0)

您可以将数据库分页与oracle的ROW_NUMBER函数一起使用:

string sql = @"
  select col1, col2, ...
  from (
      select col1, col2, ...,
      row_number()
      over (order by OrderColumn asc) rn
      from TableName
  )
  where rn between 3 and 4
";

现在您只需要参数化最后一个最小行值,例如在会话中。