Shot in the Dark

  • Thread starter Thread starter tomb
  • Start date Start date
T

tomb

Is it possible to populate a datatable with the contents of a string
variable? It's not in xml format, but instead it is the contents of a
csv. I realize I'm grasping, and I did several searches online and
found nothing, but I'm thinking there must be some way to combine things
like streamreaders with data adapters, or some such thing. Thanks for
anything you can suggest.

Tom
 
Tom,

Your shot in the dark hit me. OUCH !! :)

Okay, yes this is very very possible.

The easiest way to do this, is to do it right through MS Excel - Create a
Data Query, and copy paste, hit the Save button and Bingo.
Another way - create a linked table in MS-Access, and copy paste from Excel
to Access.

Now the above sound like inelegant one-time solutions. But they are the
least pain to setup.

If you want a more elegant solution, you can very easily use DTS/SSIS to
push CSVs into a database table.

All of these are merely point and click solutions, google + try them out and
I think you should be able to figure these out.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
__________________________________________________________
 
I appreciate the quick response, but I must reiterate, I am not using a
file, but rather a string variable that already has the contents of the
file. And I would like to do this programmatically.

Tom
 
I appreciate the quick response, but I must reiterate, I am not using a
file, but rather a string variable that already has the contents of the
file. And I would like to do this programmatically.

Tom
 
I appreciate the quick response, but I must reiterate, I am not using a
file, but rather a string variable that already has the contents of the
file. And I would like to do this programmatically.

Tom
[snip]

OK... Does the string have delimiters for each field and delimiters to indicate
the end of each record?

If it does, then use the string split method to first split the string into
records by splitting it on the record delimiter characters. After doing that
step through the array of records and split each element into fields by
splitting on the field delimiters. While doing that fill the parameters of an
sqlCommand object, open a connection on the database and insert the data and
close the connection. Repeat the process for each record element in the array.
Simple, basic programming practice. Preceded by analysis of the problem at
hand.

Of course you *could* do the parsing steps above, then convert the string to xml
that has 60% more content than you started with and read the xml before doing
the insert. Doing so would be more fashionable ;o).


Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
If you're doing .NET 2.0, then you can use SqlBulkCopy to stream the
contents in.

- Sahil Malik
 
The CSV parser I sell, http://www.csvreader.com , has a static factory
method called Parse that will consume a string variable and create a
parser instance from it. Once you have the instance created, you can
call the ReadToEnd method that will return a DataTable. This should
only take you a couple lines of code.

Bruce Dunwiddie
 
The CSV parser I sell, http://www.csvreader.com , has a static factory
method called Parse that will consume a string variable and create a
parser instance from it. Once you have the instance created, you can
call the ReadToEnd method that will return a DataTable. This should
only take you a couple lines of code.

Bruce Dunwiddie

But, that's for the folks who don't want to write the 20 - 30 lines of code
required to do it on their own.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
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.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
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
 
Thanks everyone for your input. I have resigned myself to splitting the
string variable into record strings, and splitting the record string
into an array which is then added to the datatable.

I was really hoping there was a way to use a string variable kind of
like a memory file, and just query against it somehow. Wishful
thinking, I guess.

T
 
Back
Top