I have a grid bound from a mysql db table via c#. Is there any way to get the displayed items in an insert statement?
For ex: If i bind a grid with 20 rows, i need to get all of them in a insert statements, which i can save as a .sql file and run it in another db.
Your thoughs will be highly helpful.
You COULD do it by looping through the grid rows and getting the values of each cell but that would be the hard way to do it. You'd be better off just having the DB do the work by having the DB perform the insert statement using a select statement directly as outlined here:
http://www.sqlteam.com/article/using-select-to-insert-records
This is the normal way to take the results of a select statement and insert into a different table.
Edit My co-worker, who is much smarter than me, figured out how to get the underlying DataTable from the Viewstate and use it as follows (in VB - you will need to translate it to C#):
Dim tblRanked AS System.Data.DataTable = ViewState("tblRanked")
For Each row As DataRow In tblRanked.Rows
db.ExecuteNonQuery("usp_AddRankingForUser", loginId, row("RequestID"), count)
'updates the depthead field for later use
Next
Edit - added
Here's another option that's similar to what it looks like you're asking, and the code snippet definitely shows how to loop through the rows in a datagrid to get values...
http://www.eggheadcafe.com/articles/20060513.asp
Related
I am using a dataset and a table adapter to populate a datagridview. In my SQL statement I am using the RTrim function for two of the columns. For both of them, I am setting the result variable to the same name as the original column name.
This works, but then I cannot update the data using the dataset, because the trimmed values are read-only.
What I want is to fill a datagridview with trimmed values, and then be able to update using the same dataset. This seems simple, yet it will not allow me to do this. Everything updates except the two columns that I used Trim on.
Here is the SQL statement I am using.
SELECT
PK, RTRIM(Description) AS Description, ContractNumber,
RTRIM(Status) AS Status, Active
FROM
ConstructionProjects
ORDER BY
CASE WHEN ContractNumber > 0
THEN ContractNumber
ELSE 99999
END
I know I can easily trim the cells on the client side in the Windows App., but I was looking for a way to do this at the SQL side, at the query. Is there an easy way to do this, and still be able to call the Update method?
Thanks,
Matt Fomich
A possible workaround could be tried loading the column Description and Status without the trimming operation,then hide them in your gridview. When you update your trimmed (and visible) column copy the value back to the untrimmed (and hidden) column in the same row. Then the update should work as usual.
Perhaps you should change the name of the column. (the trimmed Description and Status columns)
I want to total the values of 2 columns (MilesLadenToll, MilesLadenNonToll) from a table (FuelTaxTripSummary) based on my query (see select statement below).
I am new to C# and have only been able to display query results in DataGridView and get a total for some columns and then display the total in a text box. I would like to simply display the totals without having to list all the query results in a DataGrid View. What is the best way of doing this?
I would really appreciate examples of working code, since I new to C# and SQL.
My connection string is stored in a global variable:
dbSettings.dbConnString
Example of my select statement is:
select MilesLadenToll, MilesLadenNonToll
from FuelTaxTripSummary
where WorkMonth >= '10/01/2011' and WorkMonth < '01/01/2012'
Thanks :-)
You can take advantage of SQL aggregate functions. The SUM function can be particularly useful:
SELECT
SUM(MilesLadenToll) as MilesLadenTollTotal,
SUM(MilesLadenNonToll) as MilesLadenNonTollTotal
FROM FuelTaxTripSummary
WHERE WorkMonth >= '10/01/2011' and WorkMonth < '01/01/2012'
This query will provide a single result row with both totals.
As to how to populate the Label with the results of the query, I'm completely unfamiliar to C#, but take a look at How to add text to a WPF Label in code? and at the accepted answer to the following almost duplicate question C# SQL SUM value to a label.
You should probably be using executeReader instead of executeScalar in order to get each one of the totals, as suggested in How to populate more than one column using executescalar?
I have two mirrored datatables (same structure with two primary keys) :
DataTable_A ---> bound to a datagridView
DataTable_B ---> filled from a database
Since DataTable_B is filled by a query into database every 2 seconds, I need to mirror the DataTable_A like DataTable_B avoiding filling directly DataTable_A. When a record disappears from DataTable_B i need to delete the record also from DataTable_A. What is the best way to do this ?
Right now I am doing a "for cycle" on each row of DataTable_B and if the row doesn't exist on DataTable_A, I delete it.
Is there a better way to do it ?
The best way may be not to have a TableA at all but use a DataView on TableB. That would solve all problems at once. Can you elaborate on why you need the copy?
But otherwise you would want to handle the RowChanged and TableNewRow RowDeleted event of TableB
A more general idea, after seeing your comments: If it is possible to add a Timestamp column to the table in the database you can run a much more efficient query. And the DataTable.Merge method would do the rest.
What is the best method for saving thousands of rows and after doing something, updating them.
Currently, I use a datatable, filling it, when done inserting by
MyDataAdapter.Update(MyDataTable)
After doing some change on MyDataTable, I again use MyDataAdapter.Update(MyDataTable) method.
Edit:
I am sorry for not providing more info.
There may be up to 200.000 rows which will be created from an XML file. There rows will be saved to the database. After than there will be some process for each row. And I will need to update each row in database.
Instead of updating row by row, I decided to update the datatable and using the same dataadapter to update the rows.
This is the best of me.
I think that there may be a smarter approach.
In Reacting to your comments:
An DataAdapter.Update() will Udate (and Insert/Delete) row by row. If you have individual changes there really is no faster way. If you have systematic changes, like SET Price = Price+ 2 WHERE SelByDate < '1/1/2010' you are better of by running a DbCommand against the database.
But maybe you should worry about transactions and error handling before performance.
If I understand correctly you are doing two separate operations: loading rows to a database, and then updating those rows.
If the rows you are inserting come from another ADO.NET supported datasource then you can use SqlBulkCopy to insert the rows in batches, which will be more efficient than using a datatable.
Once the rows are in the database I would assume you would be better off executing a SQLCommand to modify their values.
If you can provide more details about what--and why--you're asking the question then perhaps we can better tailor an answer for it.
I want to take a table as represented by a multidimensional string array (column names in another array) and use a SQL SELECT statement to get a subset of rows.
The Catch:
the table is input by the user (different table each time)
the SQL is also input by the user
Do I need to:
Create a table in SQL
Populate the table in SQL
Query the table
or is the a simpler solution? E.g. converting the multidimensional array to a DataTable and then running the SQL on that object?
I think you would be able to use DataTable for this. It's normally used to store data retrieved from a database but you can populate it manually. The best part is the DataTable.Select() method, which allows you to write just the WHERE clause of a query and it will return the matching rows.
You can create your own expression tree representing the query the user enters. This is how Linq works, under the hood. If you could give an example of what your trying to achieve it may help also what your going to write the application in c# for web for example.
For instance if you letting your users enter new rows, in some kind of GUI to a table then you could, do this in a datagrid and enable column filter to achieve the result mentioned above?
In a web app you could have an input box above each column users can enter data to filter that column on.