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.
Related
I have a data table
and it contains some int type columns, some type double columns, some date type columns
what i am trying to do is,
i want to do double.TryParse for double column, and if there is any value with it then it will store dbnull value in corresponding rows,
same thing i will do for date, int
since my data table could have 100000 records so i don't to run loop for each row
is it possible through linq or with any method
Thank You
LINQ is not good for batch operations. You should create a stored procedure in your DB and import it in your model (If you are using EF that is import function, if using LINQ to SQL then a simple drag and drop will do it).
LINQ is no silver bullet for all problems where you need to loop over a (maybe very large) set of data. So if you want to go over each data set and change the values depending on some condition, a foreach loop is your friend.
LINQ is a query language to retrieve data and not some kind of super-fast way to alter large lists or other enumerable objects. It comes in handy if you want to get data from a given object applying some conditions or doing a GroupBy without ending up in a 20-lines unreadable mash of foreach-loops and if-statements.
It doesn't matter if you'll do it in a loop or with linq, you'll still need to iterate over the entire data table ...
there's no silver bullet that will save you from doing the checks and inserts i'm afraid
I have a column in my table which contains values as
"FilterA:123,234,34;FilterB:12,23;FilterC:;FilterD:45;"
Filters are separated by ';' and the values of each filter are separated by ','. There is a ':' in between a Filter's name and it's values.
Now, can I do anything which could fetch out the values part only? Like "123,234,34" for "FilterA". Or can I give it a number like "234" to search in the value part of "FilterA" and/or "54" in the value part of "FilterB"? I know it is possible with using regex, i guess, but I have no idea how.
You can't use regular expressions in Linq to Entities queries, because they cannot be translated into SQL. You even can't use String.Split to split your filters by ;. You have two options here:
Change database table structure. E.g. create table Foo_Filter which will link your entities to filters. And then create table Filters which will contain filters data.
Execute query in memory and use Linq to Objects. This option will be slow, because you have to fetch all data from database to memory
If your underlying database provider is SQL Server then you could make use of SqlMethods.Like to filter the database result set down to a manageable subset of data that can then be analysed locally with RegEx
+1 for #lazyberezovsky, you can't do regex in Linq to Entities because it can't translate that into SQL. You could pull all records back into memory then do Linq to Objects on it (do a .ToList() to a variable then a second linq query for this regex) but that means you'll load every db record into memory to process this query. Same recomendations: change the DB structure such that you don't need to do this.
I guess if you wanted everything to execute on the database server you'd need a SQL function that could parse these filters, perhaps returning a multi-column table. Unfortunately I am not on a machine where I could provide any sample code.
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 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
I have a C# Winforms app that is connecting to a SQL Server 2005 database. The form I am currently working on allows a user to enter a large amount of different types of data into various textboxes, comboboxes, and a DataGridView to insert into the database. It all represents one particular type of machine, and the data is spread out over about nine tables.
The problem I have is that my DataGridView represents a type of data that may or may not be added to the database. Thus, when the DataGridView is created, it is empty and not databound, and so data cannot be entered. My question is, should I create the table with hard-coded field names representing the way that the data looks in the database, or is there a way to simply have the column names populate with no data so that the user can enter it if they like? I don't like the idea of hard-coding them in case there is a change in the database schema, but I'm not sure how else to deal with this problem.
You could query the database for the names of the columns, if you're using a SqlDataReader you can use it's GetSchemaTable. Here's some sample code for doing this:
http://support.microsoft.com/kb/310107