I've got some code below that would allow me to access a .csv file on the server and pull the contents into a datatable which enables me to iterate the rows easily. For somethine completely seperate I have the contents of a csv in a string variable and I'd like to be able to pull that string into a datatable also. How can I do this without saving the string to a file and then pulling it back with the code below?
System.Data.Odbc.OdbcConnection conn;
DataTable dt = new DataTable();
System.Data.Odbc.OdbcDataAdapter da;
string connectionString;
string importFolder;
string fileName;
importFolder = @"c:\";
//or pass it in -this is the folder in which the csv file is in
fileName = "sold.csv";
//or pass it in -this is the csv file to be imported
connectionString = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + importFolder + ";";
conn = new System.Data.Odbc.OdbcConnection(connectionString);
//we only pass it the folder. The csv file import is in the query which follows
da = new System.Data.Odbc.OdbcDataAdapter("select * from [" + fileName + "]", conn);
da.Fill(dt);
//Your table is filled and uses the first row of the csv file as the column headings.
StringBuilder sb = new StringBuilder();
foreach (DataRow row in dt.Rows)
{
sb.Append(row[0] + ": ");
sb.Append(row["buyer_name"].ToString() + " <br/>");
}
string s = sb.ToString();
return s;