【问题标题】:How to read data from an azure database table如何从 azure 数据库表中读取数据
【发布时间】:2018-03-21 06:57:50
【问题描述】:

您好,我正在尝试读取表中名字为“John”的用户。

 async private void GetUsers()
        {
            Initialize();
            SyncCoffee();
            try
            {
              await coffeeTable.PullAsync("user", coffeeTable.Where(ct => ct.FirstName == "John" ));
            }

        catch (Exception er)
        {
            await DisplayAlert("Alert", "the error: "+er, "ok");
        }

    }

初始化方法

public async Task Initialize()
    {
            var mainPage = new MainPage();
            if (isInitialised)
            {
                return;
            }

            MobileServiceClient client = new MobileServiceClient("database link");

            const string path = "syncstore.db";

        var store = new MobileServiceSQLiteStore(path);
        store.DefineTable<User>();
        await MobileService.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());

        coffeeTable = MobileService.GetSyncTable<User>();
            isInitialised = true;

       }

同步方法

public async Task SyncCoffee()
        {
            try
            {
                await coffeeTable.PullAsync("allusers", coffeeTable.CreateQuery());
                await MobileService.SyncContext.PushAsync();


            }
            catch (Exception ex)
            {
                Debug.WriteLine("Unable to sync coffees, that is alright as we have offline capabilities: " + ex);
            }
        } 

我不断收到的错误是

System.NullReferenceException:对象引用未设置为对象的实例。

欢迎任何帮助,因为我对此很陌生。

【问题讨论】:

  • 嗨@JohnDoe - 您可以在此处提供更多信息。哪一行代码触发了空引用异常?
  • Initialize() 和 SyncCoffee() 都是异步的,但都不是使用 await 调用的。这可能会带来问题
  • @Jason andrew 询问确切的异常点,而不是函数名称
  • 我是在指出 OP,没有回答 Andrew 的问题
  • @AndrewShepherd 错误似乎在线 await coffeeTable.PullAsync("user", coffeeTable.Where(ct => ct.FirstName == "John" ));

标签: azure xamarin xamarin.forms xamarin.android azure-mobile-services


【解决方案1】:

await coffeeTable.PullAsync("user", coffeeTable.Where(ct => ct.FirstName == "John" ));

您对特定用户的拉取操作是正确的。正如 Jason 所说,您需要在 GetUsers 方法中等待 Initialize()SyncCoffee()coffeeTable 对象可能是 nullcoffeeTable.PullAsyncInitialize 执行之前会失败。对于类似的问题,您可以通过 VS 进行调试并在本地单步执行代码以缩小范围。此外,我建议你阅读阿德里安霍尔关于An Offline ClientAn Online Client 的书。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多