inserting DataTable into psql - c#

if I use:
NpgsqlCommand cmd = new NpgsqlCommand("select * from table", conn)
DataTable dt = new DataTable();
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
{
da.Fill(dt);
}
To get data from the server to a DataTable,
Is there a way to similarly put it back after I'm done with the data?
Like emptying the table and inserting the whole DataTable into it.

You could execute a command to drop the table (or just empty all rows if the table structure won't be modified) and iterate through the rows in your DataTable to insert the modified rows from the DataTable into the new table in your Database.

Related

Update DataGridView to DataSet

I'm displaying information from a dataset in a datagridview. How do I update the dataset with the data in the datagridview after it's modified? (automaticly or through button) Using winforms,in visual studio 2010, c#
The first, you should create a new datatable to contains data from Datagridview
After that, in the buttun are:(i'm connecting with database is accsess(sql is the same))
DataTable dt = new DataTable();
dt = (DataTable)dataGridView1.DataSource;
OleDbDataAdapter adp = new OleDbDataAdapter("Your query", con);
OleDbCommandBuilder cmdb = new OleDbCommandBuilder(adp);
adp.Update(dt);
// con: the variable name to connecting with database

c# - fastest way to insert value from DataReader into DataGridView

I need to load the data from SQLite, then show the data in DataTable to display in DataGridView. The current approach I am using is read the data from DataReader, then loop through reader.Read() to create each DataRow.
The performance is slow for above method because I have 600k rows of data row. I had tried to use dataTable.Load(reader), but it is even slow then previous. Is there any fastest way to add the data from SQLite into DataTable.
SQLiteCommand command = new SQLiteCommand(sql, connection);
SQLiteDataReader reader = command.ExecuteReader();
while(reader.Read())
{
DataRow dRow = dataTable.NewRow();
dRow[''] = reader[''].ToString();
......
......
......
dataTable.Rows.Add(dRow);
}
This is just a option which you might not care.
What you're doing now is, one by one, inputing 600,000 value into each column of row(of dataTable) and if columns of new row are completely inserted, the new row is added to the DataTable.
Your code inside while reading loop seems just constructing a new row and adding it to dataTable.
If you don't need to manipulate each value while reading loop which means you only need datas as they're,
SQLiteCommand command = new SQLiteCommand(sql, connection);
SqlDataAdapter da = new SqlDataAdapter(commmand);
da.Fill(dataTable);
dataGridView.ItemsSource= dt.DefaultView;

Return Multiple DataTable in C# 2005

I have table1 and table2 in a class..
public DataTable sampletable (DataTable table1,DataTable table2)
{
// How to return the two table(table1 and table2)
}
Advance thank you
public DataTable[] sampletable (DataTable table1,DataTable table2)
{
return new DataTable[] { table1, table2 };
}
Use an array. And to retrieve a particular table:
DataTable[] dtArray = sampletable (YourFirstDt, YourSecondDt);
DataTable table1 = dtArray[0];
DataTable table2 = dtArray[1];
Assuming they have the same schema, you can use the DataTable.Merge Method
public DataTable sampletable(DataTable table1, DataTable table2)
{
table1.Merge(table2);
return table1;
}
The Merge method is used to merge two DataTable objects that have
largely similar schemas. A merge is typically used on a client
application to incorporate the latest changes from a data source into
an existing DataTable. This allows the client application to have a
refreshed DataTable with the latest data from the data source.
The merge operation takes into account only the original table, and the
table to be merged. Child tables are not affected or included. If a
table has one or more child tables, defined as part of a relationship,
each child table must be merged individually.
When merging a new source DataTable into the target, any source rows
with a DataRowState value of Unchanged, Modified, or Deleted, is
matched to target rows with the same primary key values. Source rows
with a DataRowState value of Added are matched to new target rows with
the same primary key values as the new source rows.
You can use DataSet , Create a new DataSet and Add the multiple tables to it ,
For Eg-
DataSet Ds = new DataSet();
DataTable Dt1= new DataTable();
Ds.Tables.Add(Dt1)
you can add multiple tables and to access the datatable you can use the index ( eg -
Ds.Tables[0])
Hope this answers your question!!.
public DataSet Getdatasettables()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
return ds;
}

How to replace a DataBase Table by a DataTable

I am trying to replace a Table (FooTable) in my DataSource (an SQL CE Database) by a DataTable (which bind to a DataGrid). The reason I want to do this is: after I populate from a Database to a DataSet (which show all the rows on the DataGrid), I might edit, delete, and add multiple rows to the DataTable. Instead of updating my changes to DataSource (SQL CE) each time I modified the DataTable, I want to do it collectively at the end of the session.
My approach is straight-forward:
`DELETE` all data from my DataSource table (FooTable) (I'm using **SQL CE**, so `TRUNCATE` is not available)
INSERT the `DataTable's` data in to the emptied DataSource Table
Following is my C# code
/* Truncate the DataSource Table */
SqlCeCommand delCmd = new SqlCeCommand("DELETE FROM FooTable", conn)
conn.Open();
delCmd.ExecuteNonQuery();
conn.Close();
/* Insert DataTable into an empty DataSource Table */
string ins = #"INSERT INTO FooTable (FooName) values (#fooName)";
SqlCeCommand cmd = new SqlCeCommand(ins, conn);
da.InsertCommand = cmd;
da.Update(ds, "FooTable");
The codes work if I add or delete rows on the DataTable, but when I edit a specific row on the DataTable and Insert the table to my DataSource, I get the following error
"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
I do not understand why I get this error. I have already empty all rows in my DataSource table and my DataSource shouldn't not know there are modified rows but insert all DataTable Rows as new rows.
Or, is there a simple way to "REPLACE" a DataSource table by a DataTable?
[EDITED]
I tried setting the RowState manually like below code
foreach (DataRow row in dt.Rows)
{
row.RowState = DataRowState.Added;
}
but RowState is only read only and cannot be written.
[EDITED - 2nd]
I tried setting it using SetAdded()
foreach (DataRow row in dt.Rows)
{
row.SetAdded();
}
Then I get the following error:
"SetAdded and SetModified can only be called on DataRows with Unchanged DataRowState."
Still not manage to get it to work...
[EDITED - 3rd]
So finally get it to work with the following code:
using (SqlCeCommand insCmd = new SqlCeCommand(#"INSERT INTO FooTable (FooName) VALUES (#fooName)", conn))
{
insCmd.Parameters.Add("#fooName", SqlDbType.NVarChar, 10, "FooName");
dt.AcceptChanges();
foreach (DataRow row in dt.Rows)
{
row.SetAdded();
}
da.InsertCommand = insCmd;
da.Update(ds, "FooTable");
}
The DataTable tracks RowStates internally.
When you modify an existing row, its RowState becomes Modified, so the DataAdapter tries to run an UPDATE command.
You need to call the SetAdded() method on the modified rows to set their RowStates to Added rather than Modified.

Selecting specified columns form DataTable in C#

I have a DataTable object which is read from Excel speradsheet. Since the spread sheet has blank columns. I want to get rid of the blank columns in my DataTable retaining only the columns which have header data. Any better approaches rather than reading though each column?
I use C# 3.5.
You can better use "OleDbConnection" rather than looping through each columns to fetch data.
It will avoid the blank columns
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from YourTable", con);
DataTable dt = new DataTable();
da.Fill(dt);

Categories