我一直在我的 MAUI 项目中使用 sqlite-net-pcl nuget。我想获取作为对象列表的返回类型。我不知道结果是什么模型。我想要的结果就像数据集之类的对象列表。有没有办法通过在 MAUI 中使用相同的 nuget 来完成此操作? 我试过了,
public partial class DatabaseContext
{
private const string DbName = "MycustomSqlite.s3db";
private static string DbPath => Path.Combine(FileSystem.AppDataDirectory, DbName);
private SQLiteAsyncConnection _connection = null!;
private SQLiteAsyncConnection Database =>
(_connection ??= new SQLiteAsyncConnection(DbPath,
SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.SharedCache));
public async Task<List<object>> GetParamList()
{
try
{
List<object> LstParams = new List<object>();
string sqlquery = "select ParamID,ParamValue,DefaultValue, ParamName from Parametername";
LstParams = await Database.QueryAsync<object>(sqlquery);
return LstParams;
}
catch (Exception ex)
{
return null;
}
}
}
MycustomSqlite.s3db是我的自定义sqlite数据,具有参数名称表。 从这段代码我得到了这个错误,
System.ArgumentNullException: Value cannot be null. (Parameter 'type')
at System.ArgumentNullException.Throw(String paramName)
at System.ArgumentNullException.ThrowIfNull(Object argument, String paramName)
at System.Reflection.IntrospectionExtensions.GetTypeInfo(Type type)
at SQLite.TableMapping.GetPublicMembers(Type type)
at SQLite.TableMapping..ctor(Type type, CreateFlags createFlags)
at SQLite.SQLiteConnection.GetMapping(Type type, CreateFlags createFlags)
at SQLite.SQLiteCommand.ExecuteQuery[Object]()
at SQLite.SQLiteConnection.Query[Object](String query, Object[] args)
您需要为您的查询类型创建一个模型。您不能只使用
object
。
例如:
public class ParamList
{
public int ParamID { get; set: }
public int ParamValue { get; set; }
public int DefaultValue { get; set; }
public string ParamName { get; set; }
}
请注意,我对数据类型做了一些猜测。相应地修复。
然后更新您的列表数据类型和方法返回:
public async Task<List<ParamList>> GetParamList()
{
try
{
var lstParams = new List<ParamList>();
string sqlquery = "select ParamID,ParamValue,DefaultValue, ParamName from Parametername";
lstParams = await Database.QueryAsync<ParamList>(sqlquery);
return lstParams;
}
catch (Exception ex)
{
return null;
}
}
Query 方法的通用参数指定对象的类型 为每一行创建。它可以是您的表类之一,或任何 其公共属性与返回的列匹配的其他类 查询