473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

c# program to export data to .csv on specfic column only

Hi,
i have program that retrieves data from database and writes to a .csv using c#.The test.csv has 7 columns.How to export data to specific ordinal columns only .I need to update only colums2,3,5and 6.


here's snippet code:


Expand|Select|Wrap|Line Numbers
  1.             using (SqlConnection con1 = new SqlConnection(ConnectionString))
  2.             {
  3.                 con1.Open();
  4.                 SqlDataAdapter da =
  5.                     new SqlDataAdapter(
  6.                         "select Name,Speed,Month,ModelNo,Number from Test_MT " , con1);
  7.  
  8.  
  9.  
  10.                 DataTable dt = new DataTable();
  11.                 da.Fill(dt);
  12.  
  13.  
  14.  
  15.  
  16.  
  17.                 if (dt.Columns.Count != 0)
  18.                 {
  19.                     for (int i = 0; i < dt.Columns.Count; i++)
  20.                     {
  21.                         sb.Append((string) dt.Columns[2].ColumnName);
  22.                         sb.Append(i == dt.Columns.Count - 1 ? "\n" : ",");
  23.                     }
  24.                     foreach (DataRow row in dt.Rows)
  25.                     {
  26.                         for (int i = 0; i < dt.Columns.Count; i++)
  27.                         {
  28.                             sb.Append(row[i].ToString().Replace("\r\n", "").Replace(",", ""));
  29.                             sb.Append(i == dt.Columns.Count - 1 ? "\n" : ",");
  30.                         }
  31.  
  32.                     }
  33.                 }
  34.  
  35.  
  36.  
  37.  
  38.                 File.WriteAllText(@fileLocation, sb.ToString(), Encoding.UTF8);
Jun 2 '15 #1
1 2174
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

Your thread has been moved to the C# forum.

I'm not really sure what you're asking.

If you want to export only certain columns, then only select those columns in your select query.

If you want to export create blank columns in the csv, then either replicate the blank columns in your select query, or append the blank columns in your string that you write to the csv.
Jun 2 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: eddiekwang | last post by:
Hello, when I export data from a table to a text file, I get trailing spaces if the data type in char. (This dosen't happen if the data type is varchar). I can get rid of the spaces by using the...
4
by: Paolo | last post by:
Hello, I am trying to create a procedure to export my Access data to a word template. I would like to export data from two tables to a word file at the same time. My two tables are named CASES...
4
by: Tomek | last post by:
Hi, How could I export data from table or query to excel file in VB.NET? Thanks in advance, Tomek
7
by: Vanessa | last post by:
hi Everyone, I have two questions on exporting data to Excel using ASP (w/o converting formatted excel file into web page and then plug in the dynamic data): 1. Can we export data into...
2
by: Designing Solutions WD | last post by:
Hello, I have some questions on my options available. I have to export some tables to csv files to enable another department to process the files. What I need is a way to do this in ms sql...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
0
by: irslan rafique | last post by:
hi, I managed to export whole table using following code. Now I want to export data to excel using from and to date or using any date picker. <?php //include 'connectdb.php'; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.