将原始sql作为字典返回(return raw sql as dictionary)

编程入门 行业动态 更新时间:2024-10-23 13:23:55
将原始sql作为字典返回(return raw sql as dictionary)

各位大家好我正在尝试编写可以使用(字符串)tableName获取选择表信息的代码,但是当我尝试将值放到Dictionary时出现错误。 PS:我已经生成了EF DB模型。

public Dictionary<string, List<object>> GetTableInformation(string tableName, FinkonaDatabaseType type) { Dictionary<string, List<object>> _returnableDictionary = new Dictionary<string, List<object>>(); PropertyInfo prop = optimumEntities.GetType().GetProperty(tableName); Type tableType = prop.PropertyType.GenericTypeArguments[0]; var items = optimumEntities.Database.SqlQuery(tableType, "SELECT * FROM " + tableName); foreach (var item in items) { foreach (PropertyInfo info in item.GetType().GetProperties()) { if (!_returnableDictionary.ContainsKey(info.Name)) { _returnableDictionary.Add(info.Name, new List<object>()); } _returnableDictionary[info.Name].Add(info.GetValue(info, null)); // System.Reflection.TargetException, Object does not match target type. } } return _returnableDictionary; }

Hello everyone I'm trying to write code that can get select table information with (string)tableName, but i get error when i try to put value to Dictionary. P.S : I have generated EF DB model.

public Dictionary<string, List<object>> GetTableInformation(string tableName, FinkonaDatabaseType type) { Dictionary<string, List<object>> _returnableDictionary = new Dictionary<string, List<object>>(); PropertyInfo prop = optimumEntities.GetType().GetProperty(tableName); Type tableType = prop.PropertyType.GenericTypeArguments[0]; var items = optimumEntities.Database.SqlQuery(tableType, "SELECT * FROM " + tableName); foreach (var item in items) { foreach (PropertyInfo info in item.GetType().GetProperties()) { if (!_returnableDictionary.ContainsKey(info.Name)) { _returnableDictionary.Add(info.Name, new List<object>()); } _returnableDictionary[info.Name].Add(info.GetValue(info, null)); // System.Reflection.TargetException, Object does not match target type. } } return _returnableDictionary; }

最满意答案

使用ADO.NET DataTables在这里会更容易,因为EF用于强类型数据。 由于您不太担心返回的数据类型,因此DataTable将更易于导航。

以下是此示例:

public Dictionary<string, List<object>> GetTableInformation(string tableName, FinkonaDatabaseType type) { var sqlText = "SELECT * from " + tableName; DataTable dt = new DataTable(); // Use DataTables to extract the whole table in one hit using(SqlDataAdapter da = new SqlDataAdapter(sqlText, optimumEntities.Database.ConnectionString) { da.Fill(dt); } var tableData = new Dictionary<string, List<object>>(); // Go through all columns, retrieving their names and populating the rows foreach(DataColumn dc in dt.Columns) { string columnName = dc.Name; rowData = new List<object>(); tableData.Add(columnName, rowData); foreach(DataRow dr in dt.Rows) { rowData.Add(dr[columnName]); } } return tableData; }

Using ADO.NET DataTables will be easier here, as EF is used for strongly typing data. Since you are not too worried about the data types coming back, a DataTable will be easier to navigate.

Here is an examples of this:

public Dictionary<string, List<object>> GetTableInformation(string tableName, FinkonaDatabaseType type) { var sqlText = "SELECT * from " + tableName; DataTable dt = new DataTable(); // Use DataTables to extract the whole table in one hit using(SqlDataAdapter da = new SqlDataAdapter(sqlText, optimumEntities.Database.ConnectionString) { da.Fill(dt); } var tableData = new Dictionary<string, List<object>>(); // Go through all columns, retrieving their names and populating the rows foreach(DataColumn dc in dt.Columns) { string columnName = dc.Name; rowData = new List<object>(); tableData.Add(columnName, rowData); foreach(DataRow dr in dt.Rows) { rowData.Add(dr[columnName]); } } return tableData; }

更多推荐

本文发布于:2023-07-31 21:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   原始   sql   dictionary   return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!