Asp.net core 是否可以返回字典数组?
我编写了一个函数,它将返回一条sql记录作为字典。是否可以返回一个字典数组,以便以这种方式返回多个记录Asp.net core 是否可以返回字典数组?,asp.net-core,Asp.net Core,我编写了一个函数,它将返回一条sql记录作为字典。是否可以返回一个字典数组,以便以这种方式返回多个记录 public static async Task<Dictionary<string, string>> SQLMultiRecordToDictionary(string TableName, string SearchField, string SearchValue) { Dictionary<string, string> Q
public static async Task<Dictionary<string, string>> SQLMultiRecordToDictionary(string TableName, string SearchField, string SearchValue)
{
Dictionary<string, string> QueryResult = new Dictionary<string, string>();
// is TableName sane
if (!IsTextSane(TableName)) { return QueryResult; }
//
await using (var connection = new SqliteConnection("Data Source=" + dbFullPathName))
{
connection.Open();
SqliteCommand sqlcmd = connection.CreateCommand();
sqlcmd.CommandText = "SELECT * FROM " + TableName + " WHERE " + SearchField + "=@SearchValue";
sqlcmd.Parameters.AddWithValue("@SearchValue", SearchValue);
SqliteDataReader sqlreader = sqlcmd.ExecuteReader();
// generate dictionary keys with blank values
// this prevents key not existing issues when no record is returned
// i prefer no/blank values in the keys when no record returned for this project
for (int i = 0; i < sqlreader.FieldCount; i++)
{
QueryResult.Add(sqlreader.GetName(i), ""); // blank value
}
// add the values to the keys
while (sqlreader.Read())
{
for (int i = 0; i <= sqlreader.FieldCount - 1; i++)
{
QueryResult[sqlreader.GetName(i)] = sqlreader.GetString(i);
}
}
return QueryResult;
}
}
公共静态异步任务SQLMultiRecordToDictionary(字符串表名、字符串搜索字段、字符串搜索值)
{
字典查询结果=新建字典();
//TableName正常吗
如果(!IsTextSane(TableName)){return QueryResult;}
//
等待使用(var connection=new SqliteConnection(“数据源=“+dbFullPathName))
{
connection.Open();
SqliteCommand sqlcmd=connection.CreateCommand();
sqlcmd.CommandText=“从“+TableName+”中选择*,其中“+SearchField+”=@SearchValue”;
sqlcmd.Parameters.AddWithValue(“@SearchValue”,SearchValue);
SqliteDataReader sqlreader=sqlcmd.ExecuteReader();
//生成具有空值的字典键
//这可以防止在未返回任何记录时出现键不存在的问题
//如果此项目没有返回记录,我更喜欢键中的no/blank值
对于(int i=0;i 对于(int i=0;i由于Tisa,工作最终结果:
public static async Task<List<Dictionary<string, string>>> SQLMultiRecordToDictionaryList(string TableName, string SearchField, string SearchValue)
{
List<Dictionary<string, string>> QueryResult = new List<Dictionary<string, string>>();
Dictionary<string, string> SQLRecord = new Dictionary<string, string>();
//
// is TableName sane, if not return nothing
if (!IsTextSane(TableName)) { return QueryResult; }
//
await using (var connection = new SqliteConnection("Data Source=" + dbFullPathName))
{
connection.Open();
SqliteCommand sqlcmd = connection.CreateCommand();
sqlcmd.CommandText = "SELECT * FROM " + TableName + " WHERE " + SearchField + "=@SearchValue";
sqlcmd.Parameters.AddWithValue("@SearchValue", SearchValue);
SqliteDataReader sqlreader = sqlcmd.ExecuteReader();
// generate dictionary keys with blank values if no rows
// this prevents key not existing issues when no record is returned
// i prefer no/blank values in the keys when no record returned for this project
if (!sqlreader.HasRows)
{
for (int i = 0; i < sqlreader.FieldCount; i++)
{
SQLRecord.Add(sqlreader.GetName(i), ""); // blank value
}
QueryResult.Add(SQLRecord);
}
//
// add the values to the keys if there are rows (this doesn't run if no rows returned)
while (sqlreader.Read())
{
SQLRecord = new Dictionary<string, string>();
for (int i = 0; i <= sqlreader.FieldCount - 1; i++)
{
SQLRecord.Add(sqlreader.GetName(i), sqlreader.GetString(i));
}
QueryResult.Add(SQLRecord);
}
return QueryResult;
}
}
公共静态异步任务SQLMultiRecordToDictionaryList(字符串表名、字符串搜索字段、字符串搜索值)
{
List QueryResult=新列表();
Dictionary SQLRecord=newdictionary();
//
//TableName是否正常,如果不返回任何内容
如果(!IsTextSane(TableName)){return QueryResult;}
//
等待使用(var connection=new SqliteConnection(“数据源=“+dbFullPathName))
{
connection.Open();
SqliteCommand sqlcmd=connection.CreateCommand();
sqlcmd.CommandText=“从“+TableName+”中选择*,其中“+SearchField+”=@SearchValue”;
sqlcmd.Parameters.AddWithValue(“@SearchValue”,SearchValue);
SqliteDataReader sqlreader=sqlcmd.ExecuteReader();
//如果没有行,则生成具有空值的字典键
//这可以防止在未返回任何记录时出现键不存在的问题
//如果此项目没有返回记录,我更喜欢键中的no/blank值
如果(!sqlreader.HasRows)
{
对于(int i=0;i 对于(int i=0;i)你应该使用列表哦,我喜欢这个主意!我很高兴看到你已经解决了它,你可以接受你的答案作为最佳答案,那么它将帮助更多的人