Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Check out this method. First you can create a class that inherits DataContext. And then you can use methods of DataContext like to ExecuteQuery&lt;> to convert your results in to objects. With this method you can directly use your queries you wrote earlier. Also, I feel that this way is a lot better than maintaining a .dbml file, because of the issues associated to synchronization with the actual database schema.</p> <p>Consider the following example. First you need to define a class for interacting with your database</p> <pre><code>public class DBManagerDataContext : DataContext { private static string connectionString = ""; // Your connection string public static DBManagerDataContext CreateInstance() { return new DBManagerDataContext(connectionString); } protected DBManagerDataContext(string connectionString) : base(connectionString, new AttributeMappingSource()) { } } </code></pre> <p>Then you can use this context to execute queries and convert them in to objects as shown below:</p> <pre><code>public class Report { public int ReportID; public string ReportTitle; public DateTime ReportDate; private static string query = "select ReportID, ReportTitle, ReportDate from dbo.Reports"; // Your query public static List&lt;Report&gt; GetReportList() { DBManagerDataContext context = DBManagerDataContext.CreateInstance(); return context.ExecuteQuery&lt;Report&gt;(query).ToList(); } } </code></pre> <p>You can use the method "GetReportList()" given above like this for example:</p> <pre><code>List&lt;Report&gt; reports = Report.GetReportList(); </code></pre> <p>In the case of updates, the method suggested by "jdandison" is a nice option, apart from using the data context as above. In the case of updates it would be "ExecuteCommand" though. Please explore the DataContext class for more information.</p> <p>Edit: Please note that the query column names should match the definition in the object</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload