You can use a CSV file as a data source in ADO.NET, using OLEDB and the Jet
engine. CSV can be pulled in as if it were an Excel file, as well. As far as
simply putting strings in a DataTable, the answer is no, although you can
read a CSV line by line and split. You can then create individual columns in
a DataTable.
Here is the code for the solution, assuming your string is delimited in such a
way that you can split on row end delimiters and column delimiters. It will run
as is but the insert code is commented out.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Messaging;
using System.IO;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
string data = "col1,col2,col3,col4\ncol1,col2,col3,col4\n" +
"col1,col2,col3,col4";
insertString(data);
}
private static void insertString(string data)
{
string SQL = "Insert into yourtable col1, col2, col3, col4" +
"values(?, ?, ?, ?)";
// add your connection info here
OleDbConnection yourConnection = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand(SQL, yourConnection);
cmd.Parameters.Add("Col1", OleDbType.VarWChar, 50);
cmd.Parameters.Add("Col2", OleDbType.VarWChar, 50);
cmd.Parameters.Add("Col3", OleDbType.VarWChar, 50);
cmd.Parameters.Add("Col4", OleDbType.VarWChar, 50);
string[] row = data.Split(new char[] { '\n' });
for (int i = 0; i < row.Length; i++)
{
string[] col = row
.Split(new char[] { ',' });
for (int j = 0; j < col.Length; j++)
{
cmd.Parameters[j].Value = col[j];
}
//cmd.Connection.Open();
//cmd.ExecuteNonQuery();
//cmd.Connection.Close();
}
}
}
}
By the way, the method code is thirty lines long.
Good luck with your project,
Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com