如何将查询结果附加到一个datatable c#

时间:2021-11-11 21:00:02

I need to append query result to a datatable each time the method is called with different parameters. But now each time it overwrite the contents of datatable rather than appending.

每次使用不同的参数调用方法时,我需要将查询结果追加到一个datatable中。但是现在每次它都覆盖datatable的内容,而不是追加。

Is it possible to append? Please help me...

有可能追加吗?请帮我…

Protected void chkrow_CheckedChanged(object sender, EventArgs e) 
{
    DataTable dtble = new DataTable();
    CheckBox chkrow = (CheckBox)sender;
    gridViewRow gvrow = chkrow.Parent.Parent;   
    GridView gvemp = (GridView) grdemp.Rows[gvrow.RowIndex].Cells[2].FindControl("gvemp");
    string emp = grdemp.DataKeys[gvrow.RowIndex].Values[2].ToString();
    if(gvemp.Checked == true)
    {
        string sqls = "select * from emptable where empid= ' " + emp.ToString()  + "' " ;
        OracleCommand cmd = new OracleCommand(sqls, cn) ;
        OracleDataAdapter da = new OracleDataAdapter(cmd) ;
        da.Fill(dtble);
        //here I need to append result to dtble rather than overwriting when each checkbox is checked
    } 

} 

1 个解决方案

#1


1  

Define the variable dtble outside your function to make it accessible globally, Populate it on page load, load your grid view and Then -

在函数外部定义变量dtble,使其具有全局可访问性,在页面加载中填充它,加载网格视图,然后-

Protected void chkrow_CheckedChanged(object sender,      EventArgs e) 
{
DataTable dtble_inner=new DataTable() ;
CheckBox chkrow=(CheckBox)sender;
gridViewRow gvrow=chkrow.Parent.Parent; 
GridView gvemp=(GridView) grdemp.                   Rows[gvrow.RowIndex].Cells[2].FindControl("gvemp");
string emp=grdemp. DataKeys[gvrow.RowIndex].Values[2].ToString();
if(gvemp.Checked==true)
{
    string sqls="select * from emptable where empid= ' " + emp. ToString()  +"' " ;
    OracleCommand cmd=new OracleCommand(sqls, cn) ;
    OracleDataAdapter da=new OracleDataAdapter(cmd) ;
    da. Fill(dtble_inner);
    if(dtble_inner.tables.count > 0 && dtble_inner.tables.rows.count >0)
     { 
         //something like below
         dtble.rows.add(dtble_inner.tables.rows(0));

     }
     // rebind your gridview here with dtble

} 

}

}

#1


1  

Define the variable dtble outside your function to make it accessible globally, Populate it on page load, load your grid view and Then -

在函数外部定义变量dtble,使其具有全局可访问性,在页面加载中填充它,加载网格视图,然后-

Protected void chkrow_CheckedChanged(object sender,      EventArgs e) 
{
DataTable dtble_inner=new DataTable() ;
CheckBox chkrow=(CheckBox)sender;
gridViewRow gvrow=chkrow.Parent.Parent; 
GridView gvemp=(GridView) grdemp.                   Rows[gvrow.RowIndex].Cells[2].FindControl("gvemp");
string emp=grdemp. DataKeys[gvrow.RowIndex].Values[2].ToString();
if(gvemp.Checked==true)
{
    string sqls="select * from emptable where empid= ' " + emp. ToString()  +"' " ;
    OracleCommand cmd=new OracleCommand(sqls, cn) ;
    OracleDataAdapter da=new OracleDataAdapter(cmd) ;
    da. Fill(dtble_inner);
    if(dtble_inner.tables.count > 0 && dtble_inner.tables.rows.count >0)
     { 
         //something like below
         dtble.rows.add(dtble_inner.tables.rows(0));

     }
     // rebind your gridview here with dtble

} 

}

}