查詢方法(Query)是IDbConnection的擴展方法, 它可以用來執行查詢(select)並映射結果到C#實體(Model、Entity)類
查詢結果可以映射成如下類型:
Anonymous 匿名類型Strongly Typed 強類型Multi-Mapping (One to One) 多映射 一對一Multi-Mapping (One to Many) 多映射 一對多Multi-Type 多類型參數下面表格中顯示了Query方法的不同參數
名稱描述 sql要執行的sql語句文本
paramcommand的參數
transaction事務
bufferedTrue to buffer readeing the results of the query (default = true).
翻譯不來。 。 。
commandTimeoutcommand超時時間
commandTypecommand類型
示例 - 匿名查詢Raw SQL query can be executed using Query method and map the result to a dynamic list.
直接執行SQL語句字串, 然後將結果映射成 dynamic類型的List中
string sql = "SELECT * FROM Invoice;";using (var connection = My.ConnectionFactory){ connection.Open; var invoices = connection.Query(sql).ToList; My.Result.Show(invoices);}示例 - 強類型查詢(最常用)直接執行SQL語句字串, 然後將結果映射成強類型類型的List中
string sql = "SELECT * FROM Invoice;";using (var connection = My.ConnectionFactory){ connection.Open; var invoices = connection.QueryRaw SQL query can be executed using Query method and map the result to a strongly typed list with a one to one relation.(沒太理解)
直接執行SQL語句字串, 然後將結果用一對一的關係映射成強類型類型的List中
Raw SQL query can be executed using Query method and map the result to a strongly typed list with a one to many relations.
直接執行SQL語句字串, 然後將結果用一對多的關係映射成強類型類型的List中
string sql = "SELECT * FROM Invoice AS A INNER JOIN InvoiceItem AS B ON A.InvoiceID = B.InvoiceID;";using (var connection = My.ConnectionFactory){ connection.Open; var invoiceDictionary = new DictionaryRaw SQL query can be executed using Query method and map the result to a list of different types.
string sql = "SELECT * FROM Invoice;";using (var connection = My.ConnectionFactory){ connection.Open; var invoices = new List