当前位置:  开发笔记 > 编程语言 > 正文

如何从C#中的数据表中删除一列?

如何解决《如何从C#中的数据表中删除一列?》经验,为你挑选了1个好方法。

我有这个代码:

public ActionResult RcColdis() {
    string constr = @"Data Source=192.168.52.197,1433;Network Library=DBMSSOCN;Initial Catalog=DB_ADRIAN;User ID=adrian;Password=password;";

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("select a.secCode, b.[Haircut (%)],c.[Absolute IDR (Miliar)] from openquery(PDC_MYAPPS,'select secCode from mii.secReq')a left join (select * from RC_Saham)b on a.secCode=b.kode left join (select * from Absolute_IDR)c on b.kode=c.[Kode Saham]where b.[Haircut (%)] is not null union select a.code_Ic,a. PERC_HRCT, case  a. PERC_HRCT when 100 then 20 end from openquery(MANTARAY,'select b.code_ic, a.PERC_HRCT from cminstruments a, instrument_codes b where a.ID_CMI_CAPCO = b.INS_ID_INS_CAPCO')a left join (select * from Absolute_IDR)b on a.code_Ic = b.[Kode Saham]where code_Ic in (select a.secCode from openquery(PDC_MYAPPS,'select secCode from mii.secReq')a left join (select * from RC_Saham )b on a.secCode=b.kode left join (select * from Absolute_IDR)c on b.kode = c.[Kode Saham] where c.[Absolute IDR (Miliar)] is null )"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;

                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);

                    //Build the Text file data.
                    string txt = string.Empty;

                    //Add new line.
                    foreach (DataRow row in dt.Rows)
                    {
                         foreach (DataColumn column in dt.Columns)
                         {
                             //Add the Data rows.
                             txt += row[column.ColumnName].ToString() + "|";
                         }

                         //Add new line.
                         txt += "\r\n";
                    }

                    //Download the Text file.
                    Response.Clear();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", "attachment;filename=SqlExport.txt");
                    Response.Charset = "";
                    Response.ContentType = "application/text";
                    Response.Output.Write(txt);
                    Response.Flush();
                    Response.End();
                }
           }
        }
    }  

    return View("Index");
}

它会将数据从数据库导出到datatable,然后导出到txt文件.我得到的结果包括三列:

asdas |2312|12312|
asdas |2312|12312|
sdgs  |2312|12312| 
assdfs|2312|12312|
assdfs|2312|12312|

我不想在txt文件中显示第二列.我该怎么做?谢谢



1> Tim Schmelte..:

也许在这种情况下最好的选择是首先不要选择它.

如果您只是不想导出第二列,则不需要从表中删除该列,您可以使用DataColumn.ColumnNameDataColumn.Ordinal(索引):

foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn column in dt.Columns)
    {
        if (column.Ordinal != 1)
        {
            //Add the Data rows.
            txt += row[column.ColumnName].ToString() + "|";
        }
    }
    //...
}

但是,如果你想要删除它:

dt.Columns.RemoveAt(1);

推荐阅读
mobiledu2402852357
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有