创建 DataTable

记录一下 基础知识

None.gif         DataTable dt  =   new  DataTable();
None.gif        dt.Columns.Add(
" Name " );
None.gif        dt.Columns.Add(
" Value " );
None.gif        DataRow dr;
None.gif        dr 
=  dt.NewRow();
None.gif        dr[
" Name " =   " Ntext2 " ;
None.gif        dr[
" Value " =   " TesrNtext2 " ;
None.gif        dt.Rows.Add(dr);

None.gif private   void  MakeDataTableAndDisplay()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// Create new DataTable.
InBlock.gif
    DataTable table = new DataTable("table");
InBlock.gif
InBlock.gif    
// Declare DataColumn and DataRow variables.
InBlock.gif
    DataColumn column;
InBlock.gif    DataRow row;
InBlock.gif 
InBlock.gif    
// Create new DataColumn, set DataType, 
InBlock.gif    
// ColumnName and add to DataTable.    
InBlock.gif
    column = new DataColumn();
InBlock.gif    column.DataType 
= System.Type.GetType("System.Int32");
InBlock.gif    column.ColumnName 
= "id";
InBlock.gif    table.Columns.Add(column);
InBlock.gif 
InBlock.gif    
// Create second column.
InBlock.gif
    column = new DataColumn();
InBlock.gif    column.DataType 
= Type.GetType("System.String");
InBlock.gif    column.ColumnName 
= "item";
InBlock.gif    table.Columns.Add(column);
InBlock.gif 
InBlock.gif    
// Create new DataRow objects and add to DataTable.    
InBlock.gif
    for(int i = 0; i < 10; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        row 
= table.NewRow();
InBlock.gif        row[
"id"= i;
InBlock.gif        row[
"item"= "item " + i;
InBlock.gif        table.Rows.Add(row);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// Set to DataGrid.DataSource property to the table.
InBlock.gif
    dataGrid1.DataSource = table;
ExpandedBlockEnd.gif}

None.gif 将 DataTable 保存到 DataSet 中所需的 C# 代码: void  DataTableToDataSet(DataTable dt)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
// Duplicate the table and add it to a DataSet
InBlock.gif
   DataSet dsTmp = new DataSet();
InBlock.gif   DataTable dtTmp 
= dt.Copy();
InBlock.gif   dsTmp.Tables.Add(dtTmp);
ExpandedBlockEnd.gif  }

None.gif
None.gif将DataView保存到DataSet中:
void  DataViewToDataSet(DataView dv)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
// Clone the structure of the table behind the view
InBlock.gif
DataTable dtTemp = dv.Table.Clone();
InBlock.gif   dtTemp.TableName 
= "Row";  
InBlock.gif
InBlock.gif   
// Populate the table with rows in the view
InBlock.gif
   foreach(DataRowView drv in dv)
InBlock.gif      dtTemp.ImportRow(drv.Row);
InBlock.gif
InBlock.gifDataSet dsTemp 
= new DataSet(dv.Table.TableName);   
InBlock.gif
InBlock.gif   
// Add the new table to a DataSet
InBlock.gif
   dsTemp.Tables.Add(dtTemp);
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xgpapa/archive/2007/08/18/860465.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值