Datatable to string array.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey, I have a datatable here, but I need to make it into a string array. how
do I do that?
 
roger_27 said:
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.
 
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.
 
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());
 
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());
}
 
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
 
yeah I know. but I realized that I need something dynamic because I will
never know how big to make them
 
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
 
PFC said:
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.
 
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
 
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
 
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
 
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
 
Oliver Sturm said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top