运算符'!='不能应用于类型'Task'和'int'的操作数 [英] Operator '!=' cannot be applied to operands of type 'Task' and 'int'

查看:73
本文介绍了运算符'!='不能应用于类型'Task'和'int'的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我开始使用Xamarin创建Android应用程序.我尝试使用SQLite创建一个小型本地数据库.我使用了 Xamarin文档网站中的以下教程./p>

不幸的是,我得到一个错误:

错误CS0019:运算符'!='无法应用于类型'任务'和'整数'(CS0019)的操作数

我的代码如下:

 私有字符串createDatabase(字符串路径){尝试{var connection = new SQLiteAsyncConnection(path); {connection.CreateTableAsync< Person>();返回数据库已创建";}}捕获(SQLiteException ex){返回ex.Message;}}私有字符串insertUpdateData(人员数据,字符串路径){尝试{var db = new SQLiteAsyncConnection(path);if(db.InsertAsync(data)!= 0)db.UpdateAsync(data);返回单个数据文件已插入或更新";}赶上(SQLiteException ex){返回ex.Message;}}private int findNumberRecords(字符串路径){尝试{var db = new SQLiteConnection(path);//这会计算数据库中的所有记录,根据数据库的大小,它可能会变慢var count = db.ExecuteScalar< int>("SELECT Count(*)FROM Person");//用于无参数的查询//var count = db.ExecuteScalar< int>("SELECT Count(*)FROM Person WHERE FirstName =" Amy);返回计数;}捕获(SQLiteException ex){返回-1;}} 

Person类如下:

 使用系统;使用SQLite;命名空间HelloWorld {公共类人{[PrimaryKey,自动递增]public int ID {get;放;}公共字符串FullName {get;放;}公共字符串CarLicense {get;放;}公共重写字符串ToString(){返回string.Format("[人:ID = {0},FullName = {1},CarLicense = {2}]",ID,FullName,CarLicense);}}} 

有人可以帮助我解决这个错误吗?

解决方案

您可以这样做:

  if((等待db.InsertAsync(data))!= 0) 

另请参阅此答案,原因是为什么 await 常常比使用结果.

Recently I started creating Android applications with Xamarin. I try to create a small local database with SQLite. I used the following tutorial from the Xamarin documentation website.

Unfortunately I get an error:

Error CS0019: Operator '!=' cannot be applied to operands of type 'Task' and 'int' (CS0019)

My code is the following:

private string createDatabase(string path)
    {
        try
        {
            var connection = new SQLiteAsyncConnection(path);{
                connection.CreateTableAsync<Person>();
                return "Database created";
            }
        }
        catch (SQLiteException ex)
        {
            return ex.Message;
        }
    }

    private string insertUpdateData(Person data, string path)
    {
        try
        {
            var db = new SQLiteAsyncConnection(path);
            if ( db.InsertAsync(data) != 0)
                db.UpdateAsync(data);
            return "Single data file inserted or updated";
        }
        catch (SQLiteException ex)
        {
            return ex.Message;
        }
    }

    private int findNumberRecords(string path)
    {
        try
        {
            var db = new SQLiteConnection(path);
            // this counts all records in the database, it can be slow depending on the size of the database
            var count = db.ExecuteScalar<int>("SELECT Count(*) FROM Person");

            // for a non-parameterless query
            // var count = db.ExecuteScalar<int>("SELECT Count(*) FROM Person WHERE FirstName="Amy");

            return count;
        }
        catch (SQLiteException ex)
        {
            return -1;
        }
    }

And the Person class is the following:

using System;
using SQLite;

namespace HelloWorld {
public class Person
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    public string FullName { get; set; }

    public string CarLicense { get; set; }

    public override string ToString()
    {
        return string.Format("[Person: ID={0}, FullName={1}, CarLicense={2}]", ID, FullName, CarLicense);
    }
}
}

Can anybody help me with this error?

解决方案

You can do it like this:

if ( (await db.InsertAsync(data)) != 0)

Also see this answer for reasons why await is often preferred over using .Result.

这篇关于运算符'!='不能应用于类型'Task'和'int'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆