简体   繁体   English

如何将N行数的多列数据表作为CSV传递?

[英]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. 假设这是4年,假设它在4年中总共开设了50门课程。 Assume I need to save those 50 courses along with the academic program name, definitely I need to use collections. 假设我需要保存那50门课程以及学术课程名称,当然我需要使用收藏集。

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 因此,在数据库中有两个表Academics表和Courses表,在这种情况下,一行将被保存到Academics而50行将被保存在Courses

But assume there is scenario that I was told to use CSV instead of collections. 但是,假设有人告诉我使用CSV而不是集合。 so the stored procedure has an input variable of type varchar(max) to accept csv from client. 因此, stored procedure的输入变量类型为varchar(max)以接受来自客户端的csv

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 ) 假设我需要在每一行数据的csv string包括这些列(假设datagridview上当前显示的50门课程)

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? 如何将这些列和行放入一个csv字符串中?
  2. Is it common practice to use CSV string to save colllections like above (multi-columned collections)? 使用CSV字符串保存像上面那样的集合(多列集合)是否是常见的做法?
  3. I was told developers used CSV strings to store collections, is that a reality? 有人告诉我开发人员使用CSV字符串存储集合,这是现实吗?

thanks 谢谢

1) the general solution to put columns in a csv string is this: 1)将列放入csv字符串的一般解决方案是:

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

2) It is common practice, not sure it's always a good one though 2)这是很普遍的做法,但不确定是否总是好的

3) same as 2 3)与2相同

You should rather go for XML if you are working with collections. 如果要使用集合,则应该选择XML。 XMLs are easy to create, parse and maintain. XML易于创建,解析和维护。 See one of the samples here . 请参阅此处的示例之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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