我有这个代码:
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文件中显示第二列.我该怎么做?谢谢
也许在这种情况下最好的选择是首先不要选择它.
如果您只是不想导出第二列,则不需要从表中删除该列,您可以使用DataColumn.ColumnName
或DataColumn.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);