datatable与array相互转换

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.OleDb;
None.gif
None.gif
namespace  pxkt_datatable
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class pxkt
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public pxkt()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] g = new string[10];
InBlock.gif            
for(int i=0;i<10;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif             g[i]
=i.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif        
//    数组 转 数据表DataTable
InBlock.gif
                        
InBlock.gif            DataTable dt 
= new DataTable();
InBlock.gif            dt.Columns.Add(
"name",typeof(string));
InBlock.gif            
for (int k=0;k<g.Length;k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataRow dr 
= dt.NewRow();
InBlock.gif                dr[
"name"]=g[k];
InBlock.gif                dt.Rows.Add(dr);
ExpandedSubBlockEnd.gif            }

InBlock.gif        
//输出数据表
InBlock.gif

InBlock.gif            Console.WriteLine(
"打出数据表!");
InBlock.gif            
for(int j= 0;j<dt.Rows.Count;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataRow dr1 
= dt.Rows[j];
InBlock.gif                Console.WriteLine(dr1[
"name"].ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif        
//数据表DataTable转数组
InBlock.gif
            string[] arrayA = new string[dt.Rows.Count];
InBlock.gif            
for(int x=0;x<dt.Rows.Count;x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataRow dr2 
= dt.Rows[x];
InBlock.gif                arrayA[x]
=Convert.ToString(dr2["name"]);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif        
//输出数组
InBlock.gif
            Console.WriteLine("打出数组!");
InBlock.gif            
for(int y=0;y<arrayA.Length;y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(arrayA[y]);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
foreach(string printarray in arrayA)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(printarray);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static pxkt()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
class test
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            pxkt p 
= new pxkt();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif

转载于:https://www.cnblogs.com/suchenge/articles/888758.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值