如何在C#中将datarow转换为datatable或dataset。 [英] How to convert datarow to datatable or dataset in C#.
本文介绍了如何在C#中将datarow转换为datatable或dataset。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个任务要完成,即将datarow转换为datatable。
Ex。我有一个数据表,其中包含10个记录。每当我读取datarow时,每一行都应转换为数据表。
我使用下面的代码,但它不起作用。
dstable.importRow(dtrow)
I have one task to complete that is to convert datarow to datatable.
Ex. I have one datatable which contain 10 records.everytime when i read datarow, each row should be converted into datatable.
I used the below code but it is not working.
dstable.importRow(dtrow)
推荐答案
/// <summary>
/// Take each row from a source table and copy it to
/// a separate table. Return the new tables so created in a dataset.
/// </summary>
/// <param name="source">A datatable</param>
/// <returns></returns>
private DataSet SplitRows(DataTable source)
{
DataSet ds = new DataSet();
foreach(DataRow r in source.Rows)
{
DataTable copy = source.Clone();
copy.LoadDataRow(r.ItemArray, true);
ds.Tables.Add(copy);
}
return ds;
}
在做你想要完成的事情之前,你必须知道它们之间的区别。 DataSet包含DataTables,不包含实际数据。 Datatable保存基本上包含DataRows的数据。
说完了,并回答你的问题,你不能将DataRow转换为DataTable或DataSet。但是,您可以将DataRows添加到DataTable。以下是如何将DataRow添加到新DataTable的快速示例:
Before doing what you are trying to accomplish is you must know the difference between them. A DataSet contains DataTables and doesn't contain the actual data. A Datatable holds the data which basically contains DataRows.
Having that said, and to answer your question, you can't covert a DataRow to a DataTable or DataSet. You can however add DataRows to your DataTable. Here's a quick example how to add a DataRow to a new DataTable:
using System;
using System.Data;
namespace WebFormDemo
{
public partial class DataTable : System.Web.UI.Page
{
private DataTable CreateTable() {
//this is just for demo.
//You may have to fill your datatable with data from database in real scenario
DataTable dt = new DataTable();
DataRow dr = null;
//define the columns
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
dt.Columns.Add(new DataColumn("LastName", typeof(string)));
//create new row
dr = dt.NewRow();
//add values to each rows
dr["ID"] = 1;
dr["FirstName"] = "Vincent";
dr["LastName"] = "Durano";
//add the row to DataTable
dt.Rows.Add(dr);
//add new row
dr = dt.NewRow();
//add values to each rows
dr["ID"] = 2;
dr["FirstName"] = "Vianne";
dr["LastName"] = "Durano";
//add the row to DataTable
dt.Rows.Add(dr);
//add new row
dr = dt.NewRow();
//add values to each rows
dr["ID"] = 3;
dr["FirstName"] = "Slash";
dr["LastName"] = "TheMan";
//add the row to DataTable
dt.Rows.Add(dr);
return dt;
}
private void FilterData(string filterValue) {
DataTable dt = CreateTable(); //returns 3 rows
DataTable dtTemp = CreateTable().Clone(); //copy the schema structure
DataRow[] findRow = dt.Select(string.Format("LastName='{0}'",filterValue));
foreach (DataRow row in findRow) {
AddToTempTable(dtTemp, row);
}
//print the result
foreach (DataRow row in dtTemp.Rows) {
Response.Write(string.Format("Name: {0} {1} <br />", row["FirstName"],row["LastName"]));
}
}
private DataTable AddToTempTable(DataTable source, DataRow row) {
DataRow newRow = source.NewRow();
newRow[0] = row[0]; //ID
newRow[1] = row[1]; //FirstName
newRow[2] = row[2]; //LastName
source.Rows.Add(newRow);
return source;
}
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
FilterData("Durano");
}
}
}
这将在页面中输出以下内容:
姓名:Vincent Durano
姓名:Vianne Durano
This would output the following in the page:
Name: Vincent Durano
Name: Vianne Durano
Si tienes solo un DataRow como Origen y desdeas insertarlo en unDataTablevacío,simplementepodríashaceresto:
Si tienes solo un DataRow como Origen y desdeas insertarlo en un DataTable vacío, simplemente podrías hacer esto:
yourEmptyDataTable= yourDataRowSource.Table.Clone();
yourEmptyDataTable.ImportRow(yourDataRowSource);
这篇关于如何在C#中将datarow转换为datatable或dataset。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文