如何从 Azure 数据库中读取数据 table

How to read data from an azure database table

您好,我正在尝试读取 table 中名字为 "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: 对象引用未设置为对象的实例。

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

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

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