PC Review


Reply
Thread Tools Rate Thread

Datatable to string array.

 
 
=?Utf-8?B?cm9nZXJfMjc=?=
Guest
Posts: n/a
 
      12th Mar 2007
hey, I have a datatable here, but I need to make it into a string array. how
do I do that?
 
Reply With Quote
 
 
 
 
=?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
Guest
Posts: n/a
 
      12th Mar 2007
roger_27 wrote:
> hey, I have a datatable here, but I need to make it into a string array. how
> do I do that?


You loop through the records and oncvert the values from each field in
the records into a string.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
ClayB
Guest
Posts: n/a
 
      12th Mar 2007
Maybe some code like this will work for you. (You may have to check
for DBNulls or null in your table).


string[,] stringArray = new string[dataTable1.Rows.Count,
dataTable1.Columns.Count];

for(int row = 0; row < dataTable1.Rows.Count; ++row)
{
for(int col = 0; col < datatable1.Columns.Count; col++)
{
stringArray[row, col] = dataTable1.Rows[row][col].ToString();
}
}

===================
Clay Burch
Syncfusion, Inc.

 
Reply With Quote
 
=?Utf-8?B?cm9nZXJfMjc=?=
Guest
Posts: n/a
 
      12th Mar 2007
Excellent. thanks for the help guys.

here is my solution

string connectionString = "Put Connection String Here";

//make datatable
DataTable tables = new DataTable("Tables");

//this code gets a list of all the tables in the database and puts them in a
DataTable.

using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = "select table_name as Name from
INFORMATION_SCHEMA.Tables where TABLE_TYPE = 'BASE TABLE'";
connection.Open();
tables.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
}

//make the array list to use
ArrayList arr = new ArrayList();

//put all the items in each row into a new item in the arraylist.
//since the dataTable is 1 item per row, it cant be done this way
foreach (DataRow dr in tables.Rows)
{
arr.Add(dr);
}

now each table in my database can be accessed by idex number in the arraylist

MessageBox.show("Here is my 3rd table in the database " + arr[2].ToString());
 
Reply With Quote
 
=?Utf-8?B?cm9nZXJfMjc=?=
Guest
Posts: n/a
 
      12th Mar 2007
actually.. heres one that works better. it stores them as straight strings so
you can go
if(arraylist[0].tostring == "wow")
{
code
}


//make data table object called Tables
DataTable tables = new DataTable("Tables");

//this code takes every table in the data base and puts them in a data table
1 column big, with each
//row being a table
using (SqlConnection connection = new SqlConnection(Connection string or
Connection String Variable here!))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = "select table_name as Name from
INFORMATION_SCHEMA.Tables where TABLE_TYPE = 'BASE TABLE'";
connection.Open();
tables.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
}

//make an arraylist
ArrayList arTables = new ArrayList();

//go through each row of data, which is 1 column wide
//and add it to the array list!
foreach (DataRow dr in tables.Rows)
{
arTables.Add(dr.ItemArray[0].ToString());
}
 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      13th Mar 2007
Roger,

Just a peanut, but what you are using is not a string array. It is far away
from that.
It is an arraylist filled with datarows (not even items).

Cor

"roger_27" <(E-Mail Removed)> schreef in bericht
news:FA771EBF-7959-478E-9CBE-(E-Mail Removed)...
> Excellent. thanks for the help guys.
>
> here is my solution
>
> string connectionString = "Put Connection String Here";
>
> //make datatable
> DataTable tables = new DataTable("Tables");
>
> //this code gets a list of all the tables in the database and puts them in
> a
> DataTable.
>
> using (SqlConnection connection = new SqlConnection(connectionString))
> {
> SqlCommand command = connection.CreateCommand();
> command.CommandText = "select table_name as Name from
> INFORMATION_SCHEMA.Tables where TABLE_TYPE = 'BASE TABLE'";
> connection.Open();
> tables.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
> }
>
> //make the array list to use
> ArrayList arr = new ArrayList();
>
> //put all the items in each row into a new item in the arraylist.
> //since the dataTable is 1 item per row, it cant be done this way
> foreach (DataRow dr in tables.Rows)
> {
> arr.Add(dr);
> }
>
> now each table in my database can be accessed by idex number in the
> arraylist
>
> MessageBox.show("Here is my 3rd table in the database " +
> arr[2].ToString());



 
Reply With Quote
 
=?Utf-8?B?cm9nZXJfMjc=?=
Guest
Posts: n/a
 
      13th Mar 2007
yeah I know. but I realized that I need something dynamic because I will
never know how big to make them
 
Reply With Quote
 
PFC Sadr
Guest
Posts: n/a
 
      13th Mar 2007
you know.. if you just used ADO classic instead of this ADO.net _CRAP_
then you could have 2 recordsets open; via the same connection-- at
the same time

ADO.net is crap sorry dog; it's all going to change again this summer
with Visual Fred 3.0




On Mar 13, 8:11 am, roger_27 <roge...@discussions.microsoft.com>
wrote:
> yeah I know. but I realized that I need something dynamic because I will
> never know how big to make them



 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      13th Mar 2007
PFC Sadr wrote:
> you know.. if you just used ADO classic instead of this ADO.net _CRAP_
> then you could have 2 recordsets open; via the same connection-- at
> the same time


Which is keeping two connections open behind the scene.

> ADO.net is crap sorry dog; it's all going to change again this summer
> with Visual Fred 3.0
>
>
>
>
> On Mar 13, 8:11 am, roger_27 <roge...@discussions.microsoft.com>
> wrote:
>> yeah I know. but I realized that I need something dynamic because I will
>> never know how big to make them

>
>



--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Todos Menos [MSFT]
Guest
Posts: n/a
 
      13th Mar 2007
I'm not positive I beleive you dog

-Todos




On Mar 13, 1:30 pm, Göran Andersson <g...@guffa.com> wrote:
> PFC Sadr wrote:
> > you know.. if you just used ADO classic instead of this ADO.net _CRAP_
> > then you could have 2 recordsets open; via the same connection-- at
> > the same time

>
> Which is keeping two connections open behind the scene.
>
> > ADO.net is crap sorry dog; it's all going to change again this summer
> > with Visual Fred 3.0

>
> > On Mar 13, 8:11 am, roger_27 <roge...@discussions.microsoft.com>
> > wrote:
> >> yeah I know. but I realized that I need something dynamic because I will
> >> never know how big to make them

>
> --
> Göran Andersson
> _____http://www.guffa.com



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems Loading Large String Array into Array variable ExcelMonkey Microsoft Excel Programming 6 6th May 2009 11:20 PM
breaking up a String into an array of chars and adding to datatable Paulers Microsoft VB .NET 6 15th Jan 2007 02:31 AM
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= Microsoft ASP .NET 0 4th May 2006 05:29 PM
How can I use real SQL on a DataTable? i.e. not array of rows using a filter... as in DataTable.Select Dan V. Microsoft C# .NET 3 1st Jul 2004 03:06 PM
Cannot create an object of type 'System.String[]' from its string representation 'String[] Array' for the 'Options' property. Hessam Microsoft C# .NET 0 8th Aug 2003 09:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:20 PM.