Convert datatable to datareader - c#

For Performance improvement I want to convert datatable to datareader. I can not do that through query. So is there any other way to do so?

I know this is old, but the answers here seem to have missed the point of the OPs question.
DataTables have a method called CreateDataReader which will allow you to convert a DataTable to a DbDataReader object. In this case a DataTableReader.
DataTable table = new DataTable();
//Fill table with data
//table = YourGetDataMethod();
DataTableReader reader = table.CreateDataReader();
I should point out that this will not increase performance since you should be using one or the other.
Here are some more resources on the matter:
DataReader Vs DataTable
Is datareader quicker than dataset when populating a datatable?

For example
public DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
try
{
foreach (DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = myRow.GetType();
myDataColumn.ColumnName = myRow[0].ToString();
datatable.Columns.Add(myDataColumn);
}
while (dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for (int i = 0; i < schemaTable.Rows.Count; i++)
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
return datatable;
}
catch (Exception ex)
{
Error.Log(ex.ToString());
return datatable;
}
}

Use DataTable constructor,
DataTable table = new DataTable();
//Fill table with data
DataTableReader reader = new DataTableReader(table);
Good Look!

public DataTable GetTable(IDataReader _reader)
{
DataTable dataTable1 = _reader.GetSchemaTable();
DataTable dataTable2 = new DataTable();
string[] arrayList = new string[dataTable1.Rows.Count];
for (int i = 0; i < dataTable1.Rows.Count; i++)
{
DataColumn dataColumn = new DataColumn();
if (!dataTable2.Columns.Contains(dataTable1.Rows[i]["ColumnName "].ToString()))
{
dataColumn.ColumnName = dataTable1.Rows[i]["ColumnName "].ToString();
dataColumn.Unique = Convert.ToBoolean(dataTable1.Rows[i]["IsUnique "]);
dataColumn.AllowDBNull = Convert.ToBoolean(dataTable1.Rows[i]["AllowDBNull "]);
dataColumn.ReadOnly = Convert.ToBoolean(dataTable1.Rows[i]["IsReadOnly "]);
dataColumn.DataType = (Type)dataTable1.Rows[i]["DataType "];
arrayList[i] = dataColumn.ColumnName;
dataTable2.Columns.Add(dataColumn);
}
}
dataTable2.BeginLoadData();
while (_reader.Read())
{
DataRow dataRow = dataTable2.NewRow();
for (int j = 0; j < arrayList.Length; j++)
{
dataRow[arrayList[j]] = _reader[arrayList[j]];
}
dataTable2.Rows.Add(dataRow);
}
_reader.Close();
dataTable2.EndLoadData();
return dataTable2;
}

Related

'row' argument cannot be null.Parameter name: row

I am trying to load back values from a CSV to my class. Then display the values to my datatable. However, I get the error even after my values have been loaded into the class and placed inside the intended columns (See Figure 1). The error occurred at dt.Rows.Add(dr);. Below is my code:
public Newdatagrid()
{
InitializeComponent();
//Do datatable
ds = new DataSet();
dt = new DataTable();
dt.Columns.Add("Bus Model", typeof(string));//0
dt.Columns.Add("Bus Type", typeof(string));//1
dt.Columns.Add("Mileage", typeof(string));//2
if (Savestate.vehnochange_list.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
dr["Bus Model"] = Savestate.busmodel_list[Savestate.busmodel_list.Count];//0
dr["Bus Type"] = Savestate.bustype_list[Savestate.bustype_list.Count];//1
dr["Mileage"] = Savestate.busmileage_list[Savestate.busmileage_list.Count];//2
}
dt.Rows.Add(dr);
this.dataGridView2.DataSource = dt;
}
}
I think you want something like this:
public Newdatagrid()
{
InitializeComponent();
//Do datatable
ds = new DataSet();
dt = new DataTable();
dt.Columns.Add("Bus Model", typeof(string));//0
dt.Columns.Add("Bus Type", typeof(string));//1
dt.Columns.Add("Mileage", typeof(string));//2
if (Savestate.vehnochange_list.Count > 0)
{
for (int i=0; i < Savestate.vehnochange_list.Count; ++i)
{
DataRow dr = dt.NewRow();
dr["Bus Model"] = Savestate.busmodel_list[i];//0
dr["Bus Type"] = Savestate.bustype_list[i];//1
dr["Mileage"] = Savestate.busmileage_list[i];//2
dt.Rows.Add(dr);
}
this.dataGridView2.DataSource = dt;
}
}

Why does SqlDataAdapter's fill not allow me to add a row with the same its own rows' value as appearance?

Why does SqlDataAdapter's Fill method not allow me to add a row with the same its own rows' value as appearance? I could not success to provide a row's value that appears at the same row with filled one in DataTable.
Check this out:
using (SqlDataAdapter a = new SqlDataAdapter("SELECT SIPNO, SERINO, TARIH FROM SNOHAREKETLER WHERE Cast(TARIH as DATE) BETWEEN '2015/03/19' AND '2015/03/20' AND (TEZNO = 'T23' OR TEZNO = 'T31') AND CIKTI is null", c))
{
// 3
// Use DataAdapter to fill DataTable
DataTable t = new DataTable();
a.Fill(t);
t.Columns.Add("MyColumn", typeof(string));
DataRow workRow;
int iGetCount = t.Rows.Count;
for (int i = 0; i <= iGetCount - 1; i++)
{
workRow = t.NewRow();
workRow["MyColumn"] = i;
t.Rows.Add(workRow);
}
// 4
// Render data onto the screen
dataGridView1.DataSource = t;
}
Here is the solution of my issue. I searched and consulted someone. My issue was trying to add a NewRow and this causes the issue.
DataTable t = new DataTable();
a.Fill(t);
DataColumn newCol = new DataColumn("NewColumn", typeof(string));
newCol.AllowDBNull = true;
t.Columns.Add(newCol);
foreach (DataRow row in t.Rows)
{
row["NewColumn"] = "With String";
}
dataGridView1.DataSource = t;

How to Send bytes array in dataset and read from it

I want to send bytes in a data set and I have a column of data type varbinary(MAX). When i try to send a byte array, instead of showing a byte array in the data set it shows the below data. I don't know if it is a pure byte array or if it is showing bytes. What I was thinking the "ProjectIcons" column should return was a value like {bytes[1305]}....
1)If it is bytes then how can i read the bytes from dataset.
Please let me know where I am wrong. I am using below code:
public DataSet GetAllProjectStandardIcons2()
{
var images = (from p in dbModel.tbl_STANDARDPROJECTICONS select new ProjectDetails1
{
id = p.id,
ProjectIcons = (Byte[])(p.ProjectIcons)
}).ToList();
DataTable dt = new DataTable();
if (images.Count > 0)
{
Byte[] ProjectIcons;
DataColumn dc = new DataColumn("id");
DataColumn dc1 = new DataColumn("ProjectIcons");
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
for (var i = 0; i < images.Count(); i++)
{
DataRow row = dt.NewRow();
row["id"] = images[i].id;
ProjectIcons = (Byte[])images[i].ProjectIcons;
row["ProjectIcons"] = ProjectIcons;
dt.Rows.Add(row);
}
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
please try to Save image in Base64 Format and retrieve in same format, after that try to convert it in bytes...try following code
for (var i = 0; i < images.Count(); i++)
{
DataRow row = dt.NewRow();
row["id"] = images[i].id;
row["ProjectIcons"] = Convert.ToBase64String(images[i].ProjectIcons);//convert here byte array to base64
dt.Rows.Add(row);
}
I have found the solution Need to convert bytearray to base64 array below is the code that is working for me
public DataSet GetAllProjectStandardIcons2()
{
var images = (from p in dbModel.tbl_STANDARDPROJECTICONS
select new ProjectDetails1
{
id = p.id,
ProjectIcons = (Byte[])(p.ProjectIcons)
}).ToList();
DataTable dt = new DataTable();
if (images.Count > 0)
{
Byte[] ProjectIcons;
DataColumn dc = new DataColumn("id");
DataColumn dc1 = new DataColumn("ProjectIcons");
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
for (var i = 0; i < images.Count(); i++)
{
DataRow row = dt.NewRow();
row["id"] = images[i].id;
row["ProjectIcons"] = Convert.ToBase64String(images[i].ProjectIcons);//convert here byte array to base64
dt.Rows.Add(row);
}
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}

how to store multiple datatable into a single dataset

I have multiple datatable. I want to show all the datatable rows into a single gridview.
How can I do that?
DataTable dtbag101 = (DataTable)Session["bag101"];
DataTable dtwallet111 = (DataTable)Session["wallet111"];
DataSet ds= new DataSet();
ds.Tables.Add(dtbag101);
ds.Tables.Add(dtwallet111);
GridView1.DataSource= ds;
GridView1.DataBind();
The column names for both datatable are the same.
Here I was trying to use dataset but only first DataTable i.e. datbag101 was showing in the gridview.
How can I show all the values in one gridview?
Provided that your two data tables have the same columns, you can UNION them with some handy LINQ.
DataTable dtbag101 = (DataTable)Session["bag101"];
DataTable dtwallet111 = (DataTable)Session["wallet111"];
var result = dtbag101.AsEnumerable().Union(dtwallet111.AsEnumerable());
GridView1.DataSource = result;
GridView1.DataBind();
Otherwise try use DataTable.Merge:
DataTable dtbag101 = (DataTable)Session["bag101"];
DataTable dtwallet111 = (DataTable)Session["wallet111"];
dtbag101.Merge(dtwallet111, true);
GridView1.DataSource = dtbag101;
GridView1.DataBind();
I'm not sure why this isn't working for you. Try this method (grabbed from here):
public static DataTable Union(DataTable First, DataTable Second)
{
//Result table
DataTable table = new DataTable("Union");
//Build new columns
DataColumn[] newcolumns = new DataColumn[First.Columns.Count];
for(int i=0; i < First.Columns.Count; i++)
{
newcolumns[i] = new DataColumn(
First.Columns[i].ColumnName, First.Columns[i].DataType);
}
table.Columns.AddRange(newcolumns);
table.BeginLoadData();
foreach(DataRow row in First.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
foreach(DataRow row in Second.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
table.EndLoadData();
return table;
}
Call the method with your two datatables:
GridView1.DataSource = Union(dtbag101, dtwallet111);
You can simply use DataTable.Merge method
DataTable dtbag101 = (DataTable)Session["bag101"];
DataTable dtwallet111 = (DataTable)Session["wallet111"];
dtbag101.Merge(dtwallet111); //Merge action
GridView1.DataSource= dtbag101;
GridView1.DataBind();
I dont know much about this. Just referred it now.
or else
try for loop
DataSet ds = new DataSet();
addTables(dtbag101);
addTables(dtwallet111); //ds will be merge of both tables here
private void addTables(DataTable dt)
{
for(int intCount = ds.Tables[0].Rows.Count; intCount < dt.Rows.Count;intCount++)
{
for(int intSubCount = 0;intSubCount < dt.Columns.Count; intSubCount++)
{
ds.Tables[0].Rows[intCount][intSubCount] = dt.Rows[intCount][intSubCount];
}
}
}
You may need to use DataTable.Merge
DataTable dtAll = new DataTable();
dtAll = dtbag101 .Copy();
dtAll.Merge(dtwallet111, true);
GridView1.DataSource= dtAll;
GridView1.DataBind();
Edit to show how it should work
private void BindGridWithMergeTables()
{
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("ID");
dt2.Columns.Add("ID");
DataRow dr1 = dt1.NewRow();
DataRow dr2 = dt2.NewRow();
dr1["ID"] = "1";
dr2["ID"] = "2";
dt1.Rows.Add(dr1);
dt2.Rows.Add(dr2);
DataTable dtAll = new DataTable();
dtAll = dt1.Copy();
dtAll.Merge(dt2, true);
dataGridView1.DataSource = dtAll;
dataGridView1.DataBind();
}

GetSchemaTable Columns Missing?

I am using this code to get data from a dataReader into a DataTable which can then be serialised.
However, it looks like any column with a null value isnt being written to the xml.
I cant see the issue.
This is my entire class, and im calling this method
Process(IDataReader data, string filePath)
Im certain that this works, because i have used it to serialise dataTables before
Process(DataTable table, string filePath)
So i think it must be in the "GetDataTableFromSqlDataReader" method??
public class DataSerialisation
{
public static DataRow GetFirstDataRow(string xmlFilePath)
{
return GetDataTable(xmlFilePath).Rows[0];
}
public static DataTable GetDataTable(string xmlFilePath)
{
DataSet ds = new DataSet();
ds.ReadXml(xmlFilePath);
return ds.Tables[0];
}
private static DataTable GetDataTableFromSqlDataReader(IDataReader dr)
{
DataTable dtSchema = dr.GetSchemaTable();
DataTable dt = new DataTable();
ArrayList listCols = new ArrayList();
if (dtSchema != null)
{
foreach (DataRow drow in dtSchema.Rows)
{
string columnName = Convert.ToString(drow["columnName"]); //drow["columnName"].ToString();
DataColumn column = new DataColumn(columnName, (Type) (drow["DataType"]));
//column.ColumnName = columnName;
//column.Unique = (bool) (drow["IsUnique"]);
column.AllowDBNull = (bool) (drow["AllowDBNull"]);
//column.AutoIncrement = (bool) (drow["IsAutoIncrement"]);
//column.AutoIncrement = (bool) (drow["IsAutoIncrement"]);
listCols.Add(column);
dt.Columns.Add(column);
}
while (dr.Read())
{
DataRow dataRow = dt.NewRow();
for (int i = 0; i < listCols.Count; i++)
dataRow[((DataColumn) listCols[i])] = dr[i];
dt.Rows.Add(dataRow);
}
}
return dt;
}
public static void Process(IDataReader data, string filePath)
{
Process(GetDataTableFromSqlDataReader(data), filePath);
}
public static void Process(DataTable table, string filePath)
{
DataSet ds = new DataSet();
ds.Tables.Add(table.Clone());
foreach (DataRow row in table.Rows)
{
DataRow newRow = ds.Tables[0].NewRow();
for (int col = 0; col < ds.Tables[0].Columns.Count; col++)
newRow[col] = row[col];
ds.Tables[0].Rows.Add(newRow);
}
ds.WriteXml(new StreamWriter(filePath));
}
}

Categories

Resources