I am adding data to a dataset using the below code
cmdExcel.CommandText = "SELECT * FROM Table11"
dr = cmdExcel.ExecuteReader();
DataTable dtExcel = new DataTable("WK11");
dtExcel.Load(dr);
ds.Tables.Add(dtExcel);
Now i want to add the content from Table2 to the same datatable WK11. Is it possible?
cmdExcel.CommandText = "SELECT * FROM Table12"
dr = cmdExcel.ExecuteReader();
dtExcel.Append(.....
you can use the
dtExcel.ImportRow()
function which will append new row to the dtExcel Table.
Related
I have 2 different databases - SQLite and PostgreSQL and i trying to make tiny math on table from this databases.
Both tables contains columns nr_serii and ilosc, SQLite:
And Postgres:
I established a connection to both databases and populate dataset. Maybe i should use different place to store the data?
I need to substraction column ilosc: (sqlite-postgres), but not know how to do that.
For example, for each nr_serii make substraction column ilosc:
nr_serii:222222
ilosc:15-7=8
Finally i want to show output data to datagridview.
When i use messagebox i can see the data in dataset. Here is my part of code:
string cs = #"URI = file:" + Sdatabase;
string csP = conParam;
string sqlP = "select nr_serii, ilosc from stany";
string sql = "select nr_serii, ilosc from przychod";
using var con = new SQLiteConnection(cs);
con.Open();
using var cmd = new SQLiteCommand(sql, con);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
using var conP = new NpgsqlConnection(csP);
conP.Open();
NpgsqlCommand cmdP = new NpgsqlCommand(sqlP, conP);
NpgsqlDataAdapter DA = new NpgsqlDataAdapter(cmdP);
DataSet dsP = new DataSet();
DA.Fill(dsP);
//----------test-----------
foreach (DataRow row in dsP.Tables[0].Rows)
{
var nr_serii = row["nr_serii"];
var ilosc = row["ilosc"];
MessageBox.Show(nr_serii +","+ ilosc);
}
//--------------------------
For example, you can browse data table from first datasource ds and search for a matching row by value of nr_serii column for each row in the datatable in second datasource dsP, and if found, add a new row with the calculation result to the new third result table.
Then you don't forget to solve the problem of what to do with records that are only in the first ds or only in the second dsP datasource, depending on the value of the nr_serii column.
Program code example:
//prepare result third DataTable
DataTable resultDt = new DataTable();
resultDt.Columns.Add("nr_serii");
resultDt.Columns.Add("ilosc");
//add content to result DataTable
foreach (DataRow row in ds.Tables[0].Rows)
{
var nr_serii = row["nr_serii"];
var ilosc = row["ilosc"];
DataRow drP = null;
foreach (DataRow dataRow in dsP.Tables[0].Rows)
{
if (nr_serii.ToString() == (string)dataRow["nr_serii"])
{
drP = dataRow;
break;
}
}
if (drP != null)
{
var dr = resultDt.NewRow();
dr["nr_serii"] = nr_serii;
dr["ilosc"] = (int)ilosc - (int)drP["ilosc"];
resultDt.Rows.Add(dr);
}
}
I have a datagridview and I filled with data.
DataTable table = new DataTable();
dataGridView1.DataSource = table;
con = new SqlDataAdapter("SELECT * FROM TABLE "'", con);
ds = new System.Data.DataSet();
con .Fill(ds, "TABLE");
My problem is I have to add rows manually like the code below but it is just add one row.But what I need foreach's count row.
foreach (var a in names.Split(new char[] { ';' }))
{
DataRow newRow = table.NewRow();
table.Rows.Add(newRow);
dataGridView2.Rows[i + 1].Cells[3].Value = a.ToString();
i = i +1;
}
Try to use
DataTable dataTable = (DataTable)dataGridView2.DataSource;
DataRow drToAdd = dataTable.NewRow();
drToAdd[3] = a.ToString();
dataTable.Rows.Add(drToAdd);
SqlConnection con = new SqlConnection(connectionString: ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
String cquery = "SELECT cart.ProductID, ProName, Size, Colour, Price FROM cart, Products WHERE Custid=" + Session[custid] + "AND Products.ProductID = cart.ProductID";
SqlCommand ccmd = new SqlCommand(cquery, con);
DataTable dataTable = new DataTable();
CRepeater.DataSource = ccmd.ExecuteReader();
CRepeater.DataBind();
con.Close();
DataRow[] dr = dataTable.Select("SUM(Price)");
Label3.Text = Convert.ToString(dr[0]); ;
I am using a repeater to display data from database and i cant seem to figure out how to get the sum using repeater
Best approach to calculate the sum of a column in a DataTable use the DataTable.Compute method.
// Declare an object variable.
object sumObject;
sumObject = table.Compute("Sum(Price)", "");
Display the result in your Total Amount Label like so:
Label3.Text = sumObject.ToString();
Update
your missing to load the DataTable,try this
var dataReader = ccmd.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(dataReader);
CRepeater.DataSource = dataTable;
CRepeater.DataBind();
I have a question about DataTable. I retrieve a DataTable from the database, and one of these columns contains either a 1 or a 0. Now I want to retrieve only the rows with a 1 value of 1 in that column.
The name of the column is ACTIVATE.
Here is my DataTable:
DataTable table = new DataTable(TABLE);
//How can I filter here so ACTIVATE == 1?
adapter.Fill(table);
connection.Open();
selectcommand.ExecuteReader();
return table;
Via SQL (preferred)
SELECT * FROM dbo.Table WHERE ACTIVATE = 1
Via Linq-To-Datatable (in memory):
DataTable tblFiltered = table.AsEnumerable()
.Where(r => r.Field<int>("ACTIVATE") == 1)
.CopyToDataTable();
If you're still on .NET 2, you can use DataTable.Select:
DataRow[] filteredRows = table.Select("ACTIVATE = 1");
Apart from that, you don't need selectcommand.ExecuteReader() to fill the table.
DataTable table = new Datatable();
using(var con = new SqlConnection(connectionString))
using(var cmd = new SqlCommand("SELECT * FROM dbo.Table WHERE ACTIVATE = 1", con))
using(var da = new SqlDataAdapter(cmd))
{
da.Fill( table );
}
DataTable results = table.Select("ACTIVATE = 1").CopyToDataTable();
That should achieve what you want, basically you can query data tables much like SQL.
You simply use DataTable.Select like this:
foundRows = table.Select("ACTIVATE = '1'");
It returns an array of DataRow objects.
return table;
DataTable results = table.Select("ACTIVATE = 1").CopyToDataTable();
How do I delete the empty rows in my dataset?
I am reading data from an excel spreadsheet that has a few empty rows at the bottom.
Here is my code so far:
ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", VariableFile);
OleDbConnection objConn = new OleDbConnection(ConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Requirements$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1);
objConn.Close();
Why not just modify your query to pull only the non-empty data.
Every solution I have found told me to modify the Excel query like you have it. So that isn't much help. You could just create a DataView from your Table that would look at the non-blank rows. Do you know what the columns are beforehand? Even if you don't you could still loop over the column names and build a filter string for the DataView.
string filter = "";
foreach (DataColumn dc in dt.Columns)
{
filter += dc.ColumnName + " <> '' ";
if (dt.Columns[dt.Columns.Count-1].ColumnName != dc.ColumnName)
{
filter += " AND ";
}
}
DataView view = new DataView(dt);
view.RowFilter = filter;
dt = view.ToTable();
Use Linq to include only rows that have a non-null/blank value in any of its columns.
var filteredData = sourceDataTable.AsEnumerable()
.Where(row => row.ItemArray.Any(col => !String.IsNullOrWhiteSpace(col.ToString())))
.ToArray();