How to apply SQL query to C # DataTable / Dataset?

I have an application in which users can enter an SQL query as text, and I need to run it with C # DataTable / Dataset inside my application. Can this be done?

EDIT: Based on answers and some other research, this cannot be done - there is no way to apply an SQL query to a table that you have already read in your application. See answers to possible workarounds.

EDIT 2: The final solution in my case was to implement a simple analyzer using ANTLR. This allowed users to enter simple queries using the keywords AND, OR, and parentheses. The ANTLR generated class would convert this into a set of instructions that I could use to query a C # dataset.

+5
source share
2 answers

If your users enter nothing but the simplest selection operators, you will have a very difficult time for this. I suppose it would be too costly for your project to write a complete SQL parser, but that is essentially what you are talking about.

ORM, , , SQL- -, DataTable.Select, where SqlParameters.

, , , :

Linqer ( SQL LINQ), LINQ to DataSet

Linqer.

, , , . , , . , , , , . ( ) , , , .

@JoshC Sqlite, SQL Server 2012 LocalDB, , , , .net.

+5

# datatable/dataset

select.

myDataTable.Select("columnName1 like '%" + value + "%'");

+4

All Articles