views:

171

answers:

3

Hi everyone!

Is it possible to make LinQ to SQL read and understand a pure SQL query sucha as "SELECT * from myTable"??

My boss asked me to write directly to database the pure SQL querys in a certain table and this table will have a field named "typeOfSelect", for example.

And, in my application, I will have to read this field "typeOfSelect" and make LinQ understands what that means.

Thanks!

+2  A: 

If you want to write SQL it is easy to just create some wrapper classes so you want write something like this.

DataTable result = DBManager.Query("SELECT * FROM Table");

But I don't think this is generally considered good practice any more and would defeat the purpose of Linq.

Craig
The easiest way to do like this by using sqlcommand object.
Soul_Master
+3  A: 

You can use the ExecuteCommand and ExecuteQuery methods on the DataContext.

var db = new MyDataContext();
IEnumerable<Table1> result = db.ExecuteQuery<Table1>("SELECT * FROM Table1");
db.ExecuteCommand("UPDATE Tabel1 SET Column1 = 'Foo'");

ExecuteQuery will work with any object that has property names that match the column names of the table. ExecuteCommand is used for SQL commands that don't return a result set.

Another way would be to use the Connection.CreateCommand() method on a DataContext which will return a DbCommand object. Then you simply set the CommandText property with the SQL you want to execute.

DbCommand command = myDataContext.Connection.CreateCommand();
command.CommandText = "SELECT * FROM Table1";

try {
    command.Connection.Open();
    DbDataReader reader = command.ExecuteReader();
    // do something usefull with the reader like creating a DataTable
} finally {
    command.Connection.Close();
    command.Dispose();
}

You can also look at this MSDN link for some other examples.

Thanks William! I'll check it out what you've told me!
AndreMiranda
+2  A: 

LINQ is the means by which Microsoft differentiates their server access technology from more generic, portable techniques. By making it easier for programmers to do it the "Microsoft Way", they gain competitive advantage. Once you're convinced that LINQ is the universal querying syntax, with applicability beyond relational databases (which is the primary destination, and where the competition is very thin), they have a strong interest in maintaining full control of the language (especially since it will work best with the rest of their infrastructure).

This is where we'll find out how well the "Leaky Abstraction" concept plays out. Microsoft certainly has what I think are the best minds in the industry committed to it, and a lot of people are betting pretty heavily on it. But it's a 100% commitment - no looking back.

It's a lot like asking whether you can use SQL to express ISAM queries. And at one time it was IBM driving the SQL query infrastructure, in an equally semi-proprietary way, as I've heard it told.

le dorfier
Isn't this the opposite? The question is asking how to use SQL instead of proprietary linq.
Andomar
He says he wants to use LINQ as the means to execute SQL, not use SQL instead of LINQ.
le dorfier