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

Re: Shot in the Dark by Sahil

Sahil
Sun May 07 23:29:31 CDT 2006

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
__________________________________________________________


"tomb" <tomb@technetcenter.com> wrote in message
news:xsy7g.15865$Sl4.15110@bignews1.bellsouth.net...
> 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



Re: Shot in the Dark by Cor

Cor
Mon May 08 00:12:25 CDT 2006

Tomb,

Strange that you did not find it, Paul Clement and as well me, have given
endless samples about the use of OleDb for this in these newsgroups.

A simple one.

http://www.vb-tips.com/default.aspx?ID=1b644f6b-aa01-49f6-bc1f-212f9e0de193

I hope this helps,

Cor

"tomb" <tomb@technetcenter.com> schreef in bericht
news:xsy7g.15865$Sl4.15110@bignews1.bellsouth.net...
> 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



Re: Shot in the Dark by tomb

tomb
Mon May 08 13:30:25 CDT 2006

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

Sahil Malik [MVP C#] wrote:

>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
>__________________________________________________________
>
>
>"tomb" <tomb@technetcenter.com> wrote in message
>news:xsy7g.15865$Sl4.15110@bignews1.bellsouth.net...
>
>
>>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
>>
>>
>
>
>
>

Re: Shot in the Dark by tomb

tomb
Mon May 08 13:30:42 CDT 2006

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

Cor Ligthert [MVP] wrote:

>Tomb,
>
>Strange that you did not find it, Paul Clement and as well me, have given
>endless samples about the use of OleDb for this in these newsgroups.
>
>A simple one.
>
>http://www.vb-tips.com/default.aspx?ID=1b644f6b-aa01-49f6-bc1f-212f9e0de193
>
>I hope this helps,
>
>Cor
>
>"tomb" <tomb@technetcenter.com> schreef in bericht
>news:xsy7g.15865$Sl4.15110@bignews1.bellsouth.net...
>
>
>>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
>>
>>
>
>
>
>

Re: Shot in the Dark by Otis

Otis
Tue May 09 06:38:45 CDT 2006

On Mon, 08 May 2006 14:28:49 -0400, tomb <tomb@technetcenter.com> wrote:

>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

Re: Shot in the Dark by Sahil

Sahil
Tue May 09 08:18:40 CDT 2006

If you're doing .NET 2.0, then you can use SqlBulkCopy to stream the
contents in.

- Sahil Malik


"tomb" <tomb@technetcenter.com> wrote in message
news:57M7g.16157$Sl4.5334@bignews1.bellsouth.net...
>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
>
> Sahil Malik [MVP C#] wrote:
>
>>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
>>__________________________________________________________
>>
>>
>>"tomb" <tomb@technetcenter.com> wrote in message
>>news:xsy7g.15865$Sl4.15110@bignews1.bellsouth.net...
>>
>>>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
>>
>>
>>



Re: Shot in the Dark by shriop

shriop
Tue May 09 11:03:43 CDT 2006

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


Re: Shot in the Dark by Otis

Otis
Tue May 09 17:26:56 CDT 2006

On 9 May 2006 09:03:43 -0700, "shriop" <shriop@hotmail.com> wrote:

>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

Re: Shot in the Dark by Cowboy

Cowboy
Tue May 09 21:40:48 CDT 2006

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!
*************************************************
"tomb" <tomb@technetcenter.com> wrote in message
news:xsy7g.15865$Sl4.15110@bignews1.bellsouth.net...
> 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



Re: Shot in the Dark by Otis

Otis
Wed May 10 06:50:24 CDT 2006

On Tue, 9 May 2006 21:40:48 -0500, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbworld@comcast.netNoSpamM> wrote:

>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[i].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

Re: Shot in the Dark by tomb

tomb
Wed May 10 10:28:15 CDT 2006

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


tomb wrote:

> 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