-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SqlDb: added function QueryDictionary
- Loading branch information
1 parent
e76d884
commit 777b31b
Showing
5 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Linq; | ||
|
||
namespace ACUtils | ||
{ | ||
public static class SqlDb_QueryDictionary | ||
{ | ||
#region static without params | ||
public static Dictionary<KT,VT> QueryDictionary<KT, VT>(SqlConnection connection, string queryString, string keyField = "key", string valueField = "value") | ||
{ | ||
return QueryDictionary<KT, VT>(connection, queryString, keyField, valueField, new KeyValuePair<string, object>[0]); | ||
} | ||
#endregion | ||
#region static with simple params | ||
public static Dictionary<KT, VT> QueryDictionary<KT, VT>(SqlConnection connection, string queryString, string keyField = "key", string valueField = "value", params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
return _return<KT, VT>( | ||
SqlDb_QueryDataTable.QueryDataTable(connection, queryString, queryParams), | ||
keyField, | ||
valueField | ||
); | ||
} | ||
#endregion | ||
#region static with typed params | ||
public static Dictionary<KT, VT> QueryDictionary<KT, VT>(SqlConnection connection, string queryString, string keyField = "key", string valueField = "value", params KeyValuePair<string, KeyValuePair<SqlDbType, object>>[] queryParams) | ||
{ | ||
return _return<KT, VT>( | ||
SqlDb_QueryDataTable.QueryDataTable(connection, queryString, queryParams), | ||
keyField, | ||
valueField | ||
); | ||
} | ||
#endregion | ||
|
||
#region without params | ||
public static Dictionary<KT, VT> QueryDictionary<KT, VT>(this SqlDb self, string queryString, string keyField = "key", string valueField = "value") | ||
{ | ||
return self.QueryDictionary<KT, VT>(queryString, keyField, valueField, new KeyValuePair<string, object>[0]); | ||
} | ||
#endregion | ||
#region with simple params | ||
public static Dictionary<KT, VT> QueryDictionary<KT, VT>(this SqlDb self, string queryString, string keyField = "key", string valueField = "value", params KeyValuePair<string, object>[] queryParams) | ||
{ | ||
return _return<KT, VT>( | ||
self.QueryDataTable(queryString, queryParams), | ||
keyField, | ||
valueField | ||
); | ||
} | ||
#endregion | ||
#region whit typed params | ||
public static Dictionary<KT, VT> QueryDictionary<KT, VT>(this SqlDb self, string queryString, string keyField = "key", string valueField = "value", params KeyValuePair<string, KeyValuePair<SqlDbType, object>>[] queryParams) | ||
{ | ||
return _return<KT, VT>( | ||
self.QueryDataTable(queryString, queryParams), | ||
keyField, | ||
valueField | ||
); | ||
} | ||
#endregion | ||
|
||
private static Dictionary<KT, VT> _return<KT, VT>(DataTable dt, string keyField, string valueField) | ||
{ | ||
return dt.AsEnumerable().ToDictionary(d => d.Field<KT>(keyField), d => d.Field<VT>(valueField)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters