467,869 Members | 1,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,869 developers. It's quick & easy.

Datatable to string array.

hey, I have a datatable here, but I need to make it into a string array. how
do I do that?
Mar 12 '07 #1
  • viewed: 59988
Share:
15 Replies
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
Mar 12 '07 #2
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.

Mar 12 '07 #3
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());
Mar 12 '07 #4
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());
}
Mar 12 '07 #5
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" <ro*****@discussions.microsoft.comschreef in bericht
news:FA**********************************@microsof t.com...
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());

Mar 13 '07 #6
yeah I know. but I realized that I need something dynamic because I will
never know how big to make them
Mar 13 '07 #7
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

Mar 13 '07 #8
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
Mar 13 '07 #9
I'm not positive I beleive you dog

-Todos


On Mar 13, 1:30 pm, Göran Andersson <g...@guffa.comwrote:
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

Mar 13 '07 #10
C# doesn't support strings. sorry

it's just old-fashioned; when we invented C# we wanted it to be
REVOLUTIONARY
so we called them all cSharpStrings

-Todos
On Mar 12, 3:34 pm, roger_27 <roge...@discussions.microsoft.com>
wrote:
hey, I have a datatable here, but I need to make it into a string array. how
do I do that?

Mar 13 '07 #11
I am not positive that I care if you "beleive" me or not.
Todos Menos [MSFT] wrote:
I'm not positive I beleive you dog

-Todos


On Mar 13, 1:30 pm, Göran Andersson <g...@guffa.comwrote:
>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

--
Göran Andersson
_____
http://www.guffa.com
Mar 13 '07 #12
ok, smarty pants

what defines a new connection

because from where im standing; they have the same SPID; so thus they
are the same connection

On Mar 13, 3:11 pm, Göran Andersson <g...@guffa.comwrote:
I am not positive that I care if you "beleive" me or not.

Todos Menos [MSFT] wrote:
I'm not positive I beleive you dog
-Todos
On Mar 13, 1:30 pm, Göran Andersson <g...@guffa.comwrote:
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

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

Mar 13 '07 #13
Hello Todos,

Let me tell you that you're a funny kind of idiot. Let me also say, to be
clear, that I'd much prefer it if you took your crap elsewhere.
Oliver Sturm
--
http://www.sturmnet.org/blog
Mar 14 '07 #14
I like his postings

at least Todos doesn't block people that disagree with him.
that is called CENSORSHIP and the USA should not stand for it


On Mar 14, 2:26 pm, "Oliver Sturm" <oli...@sturmnet.orgwrote:
Hello Todos,

Let me tell you that you're a funny kind of idiot. Let me also say, to be
clear, that I'd much prefer it if you took your crap elsewhere.

Oliver Sturm
--http://www.sturmnet.org/blog

Mar 15 '07 #15
"Oliver Sturm" <ol****@sturmnet.orgwrote in message
news:xn****************@msnews.microsoft.com...
Hello Todos,

Let me tell you that you're a funny kind of idiot. Let me also say, to be
clear, that I'd much prefer it if you took your crap elsewhere.
Oliver Sturm
--
http://www.sturmnet.org/blog
He's a troll. This is a new alias for him. He trolls over in the VB group
as aaronkempf, susiedba, dbahooker, larrylinson, and those are just the
ones I can remember.

Robin S.
Mar 16 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Jeff Johnson | last post: by
4 posts views Thread by pagates | last post: by
11 posts views Thread by Zordiac | last post: by
5 posts views Thread by Paulers | last post: by
2 posts views Thread by dllhell | last post: by
5 posts views Thread by twq | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.