简体   繁体   中英

How do i pass a muti-column datatable with N number of rows as a CSV?

Assume there is an academic program called Bachelors of Computer Science. Assume it is 4 years assume it has a total of 50 courses spread across 4 years. Assume I need to save those 50 courses along with the academic program name, definitely I need to use collections.

so in the DB there are two tables Academics table and Courses table, in this case one row will be saved to Academics and 50 rows will be saved in Courses

But assume there is scenario that I was told to use CSV instead of collections. so the stored procedure has an input variable of type varchar(max) to accept csv from client.

Assume I need to include these column in csv string for each row of data (assume the 50 courses currently on display on a datagridview )

CourseID
YearNo --> when the student has the do this course in 1st year, 2nd year etc.
IsCompulsory  --> any course a student select can be either compulsory or an elective
  1. How do I put those column and rows in one csv string?
  2. Is it common practice to use CSV string to save colllections like above (multi-columned collections)?
  3. I was told developers used CSV strings to store collections, is that a reality?

thanks

1) the general solution to put columns in a csv string is this:

SELECT column + ',' FROM table FOR XML PATH('') WHERE condition

2) It is common practice, not sure it's always a good one though

3) same as 2

You should rather go for XML if you are working with collections. XMLs are easy to create, parse and maintain. See one of the samples here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM