.net - Is it possible to make LinQ "understand" a classic SQL query? -


Is it possible for LinQ to read SQL and understand the pure SQL query such as the following? My boss has asked me to write pure SQL queries directly into the database in a specific table and a field will be named in this table. For example, "typeOfSelect".

And, in my application, I have to read this field "typeOfSelect" and understand the meaning of LinQ.

Thank you!

You can use ExecuteCommand and ExecuteQuery methods on DataContext.

  var db = new MyDataContext (); IEnumerable & LT; Table1 & gt; Results = db.ExecuteQuery & lt; Table1 & gt; ("Select * FROM Table1"); Db.ExecuteCommand ("Update Tabel1 SET column1 = 'Foo'");  

ExecuteQuery will work with any object that has the name of the property that matches the column name of the table. ExecuteCommand is used for the SQL command which does not return the results.

Another way would be to use a connection to a datacontext. Credit Commands () method that will return a DbCommand object. After that you can only set the command text property with SQL which you want to execute.

  DbCommand command = myDataContext.Connection.CreateCommand (); Command.CommandText = "Select Table 1"; Try {command.Connection.Open (); DbDataReader Reader = command.ExecuteReader (); // something useful with a reader like creating a datatylet) {command.Connection.Close (); Command.Dispose (); }  

You can also see this link for some other examples.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -