数据表到字符串数组。 [英] Datatable to string array.

查看:86
本文介绍了数据表到字符串数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我这里有一个数据表,但我需要把它变成一个字符串数组。怎么

我这样做?

解决方案

roger_27写道:


嘿,这里有一个数据表,但我需要把它变成一个字符串数组。怎么

我这样做?



循环记录并将记录中每个字段的值转换为字符串。

-

G ??跑了Andersson

_____
http://www.guffa.com


也许这样的代码对你有用。 (你可能需要检查

for DBNulls或在你的表中为null)。

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。


出色。感谢帮助人员。


这是我的解决方案


string connectionString ="把连接字符串放在这里;


// make datatable

DataTable tables = new DataTable(" Tables");


//此代码获取一个列表数据库中的所有表格并将它们放在

DataTable中。


使用(SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand command = connection.CreateCommand();

command.CommandText =" select table_name as Name from

INFORMATION_SCHEMA。 TABLE_TYPE =''BASE TABLE''" ;;

connection.Open();

tables.Load(command.ExecuteReader(CommandBehavior。CloseConnection)); < br $>
}


//使数组列表使用

ArrayList arr = new ArrayList();


//将每行中的所有项目放入arraylist中的新项目。

//因为dataTable是每行1个项目,所以不能这样做

foreach(Table.Rows中的DataRow博士)

{

arr.Add(dr);

}


现在我的数据库中的每个表都可以通过arraylist中的idex号访问


MessageBox.show("这是我在数据库中的第3个表格) + arr [2] .ToString());


hey, I have a datatable here, but I need to make it into a string array. how
do I do that?

解决方案

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


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());


这篇关于数据表到字符串数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆