C# Android Azure删除所有行
我正在使用Azure移动服务构建一个带有Azure数据库(在Xamarin中)的Android应用程序。 我想从表的记录中清除表。C# Android Azure删除所有行,c#,android,azure,xamarin,azure-mobile-services,C#,Android,Azure,Xamarin,Azure Mobile Services,我正在使用Azure移动服务构建一个带有Azure数据库(在Xamarin中)的Android应用程序。 我想从表的记录中清除表。 虽然有一个“table.RemoveAsync”,但我不确定如何选择所有行。 是用“选择”还是“在哪里”? 我非常感谢你的帮助 这是我的密码: //Mobile Service Client reference private MobileServiceClient client; //Mobile Service sync table u
虽然有一个“table.RemoveAsync”,但我不确定如何选择所有行。
是用“选择”还是“在哪里”? 我非常感谢你的帮助 这是我的密码:
//Mobile Service Client reference
private MobileServiceClient client;
//Mobile Service sync table used to access data
private IMobileServiceSyncTable<Mashlim> mashlimTable;
const string applicationURL = @"http://blahblah.azurewebsites.net/";
public override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
CurrentPlatform.Init();
// Create the Mobile Service Client instance, using the provided
// Mobile Service URL
client = new MobileServiceClient(applicationURL);
await InitLocalStoreAsync();
// Get the Mobile Service sync table instance to use
mashlimTable = client.GetSyncTable<Mashlim>();
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.ShabbatMinyan, container, false);
….
OnRefreshItemsSelected();
return view;
}
private async Task InitLocalStoreAsync()
{
// new code to initialize the SQLite store
string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), localDbFilename);
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
var store = new MobileServiceSQLiteStore(path);
store.DefineTable<Mashlim>();
// Uses the default conflict handler, which fails on conflict
// To use a different conflict handler, pass a parameter to InitializeAsync. For more details, see http://go.microsoft.com/fwlink/?LinkId=521416
await client.SyncContext.InitializeAsync(store);
}
private async Task SyncAsync()
{
try
{
await client.SyncContext.PushAsync();
await mashlimTable.PullAsync("allMashlims", mashlimTable.CreateQuery()); // query ID is used for incremental sync
}
catch (Java.Net.MalformedURLException)
{
CreateAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
}
catch (Exception e)
{
CreateAndShowDialog(e, "Error");
}
}
// Called when the refresh menu option is selected
private async void OnRefreshItemsSelected()
{
await SyncAsync(); // get changes from the mobile service
await RefreshItemsFromTableAsync(); // refresh view using local database
}
//Refresh the list with the items in the local database
private async Task RefreshItemsFromTableAsync()
{
try
{
// Get the items that were marked as mashlim and add them to list
var list = await mashlimTable.Where(item => item.IsMashlim == true).ToListAsync();
mashlimim = 0;
foreach (Mashlim current in list)
mashlimim++;
mashlimimNumText.Text = mashlimim.ToString();
}
catch (Exception e)
{
CreateAndShowDialog(e, "Error");
}
}
[Java.Interop.Export()]
public async void AddItem()
{
if (client == null)
{
return;
}
if(Settings.MashlimId==string.Empty)
{
// Create a new item
item = new Mashlim
{
Name = nameText.Text,
PhoneNumber = phoneText.Text,
IsMashlim = true
};
try
{
await mashlimTable.InsertAsync(item); // insert the new item into the local database
await SyncAsync(); // send changes to the mobile service
await RefreshItemsFromTableAsync();
Settings.MashlimId = item.Id;
Settings.MashlimName = item.Name;
Settings.IsMashlim = true;
Settings.MashlimPhone = item.PhoneNumber;
}
catch (Exception e)
{
CreateAndShowDialog(e, "Error");
}
}
else
{
Settings.IsMashlim = true;
item = new Mashlim
{
Id = Settings.MashlimId,
Name = Settings.MashlimName,
IsMashlim = Settings.IsMashlim,
PhoneNumber = Settings.MashlimPhone
};
try
{
await mashlimTable.UpdateAsync(item); // insert the new item into the local database
await SyncAsync(); // send changes to the mobile service
await RefreshItemsFromTableAsync();
mashlim = true;
}
catch (Exception e)
{
CreateAndShowDialog(e, "Error");
}
}
}
}
}
//移动服务客户端参考
私人移动服务客户端;
//用于访问数据的移动服务同步表
私有IMobileServiceSyncTable mashlimTable;
常量字符串applicationURL=@”http://blahblah.azurewebsites.net/";
创建时公共重写异步void(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
CurrentPlatform.Init();
//使用提供的
//移动服务URL
客户端=新的MobileServiceClient(applicationURL);
等待InitLocalStoreAsync();
//获取要使用的移动服务同步表实例
mashlimTable=client.GetSyncTable();
}
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
var视图=充气机。充气(Resource.Layout.ShabbatMinyan,container,false);
….
OnRefreshItemsSelected();
返回视图;
}
私有异步任务InitLocalStoreAsync()
{
//初始化SQLite存储的新代码
string path=path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),localDbFilename);
如果(!File.Exists(path))
{
File.Create(path.Dispose();
}
var store=新的MobileServiceSQLiteStore(路径);
store.DefineTable();
//使用默认冲突处理程序,该处理程序在发生冲突时失败
//若要使用其他冲突处理程序,请将参数传递给InitializeAsync。有关更多详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=521416
等待client.SyncContext.InitializeAsync(存储);
}
专用异步任务SyncAsync()
{
尝试
{
等待client.SyncContext.PushAsync();
等待mashlimTable.PullAsync(“allMashlims”,mashlimTable.CreateQuery());//查询ID用于增量同步
}
catch(Java.Net.MalformedURLException)
{
CreateAndShowDialog(新异常(“创建移动服务时出错。请验证URL”),“错误”);
}
捕获(例外e)
{
CreateAndShowDialog(e,“错误”);
}
}
//选择“刷新”菜单选项时调用
私有异步void OnRefreshItemsSelected()
{
wait SyncAsync();//从移动服务获取更改
等待RefreshItemsFromTableAsync();//使用本地数据库刷新视图
}
//使用本地数据库中的项目刷新列表
专用异步任务RefreshItemsFromTableAsync()
{
尝试
{
//获取标记为mashlim的项目并将其添加到列表中
var list=await mashlimTable.Where(item=>item.IsMashlim==true.toListSync();
mashlim=0;
foreach(列表中当前的Mashlim)
mashlimm++;
mashlimnumtext.Text=mashlimm.ToString();
}
捕获(例外e)
{
CreateAndShowDialog(e,“错误”);
}
}
[Java.Interop.Export()]
公共异步void AddItem()
{
if(客户端==null)
{
返回;
}
if(Settings.MashlimId==string.Empty)
{
//创建一个新项目
项目=新Mashlim
{
Name=nameText.Text,
PhoneNumber=phoneText.Text,
IsMashlim=true
};
尝试
{
wait mashlimTable.InsertAsync(item);//将新项插入本地数据库
wait SyncAsync();//将更改发送到移动服务
等待RefreshItemsFromTableAsync();
Settings.MashlimId=item.Id;
Settings.MashlimName=item.Name;
Settings.IsMashlim=true;
Settings.MashlimPhone=item.PhoneNumber;
}
捕获(例外e)
{
CreateAndShowDialog(e,“错误”);
}
}
其他的
{
Settings.IsMashlim=true;
项目=新Mashlim
{
Id=Settings.MashlimId,
Name=Settings.MashlimName,
IsMashlim=Settings.IsMashlim,
PhoneNumber=Settings.MashlimPhone
};
尝试
{
wait mashlimTable.UpdateAsync(item);//将新项插入本地数据库
wait SyncAsync();//将更改发送到移动服务
等待RefreshItemsFromTableAsync();
mashlim=true;
}
捕获(例外e)
{
CreateAndShowDialog(e,“错误”);
}
}
}
}
}
谢谢。在客户端SDK中,您不能在本地SQLite存储上运行任意SQL语句。因此,您必须选择所有行并循环通过它们进行删除。(使用ToListSync将所有内容加载到内存时要小心,因为这样可能会耗尽设备上的内存。)请注意,如果有X行,这将导致X服务器请求,这可能会非常慢。有关如何在客户端上执行请求的信息,请参阅本文: 如果要删除与特定条件匹配的所有行,最好在服务器上编写一个自定义API,让客户端通过调用API只发送一个请求。在服务器上,您可以使用SQL或LINQ,具体取决于您使用的是.NET还是Node.js 例如,使用.NET后端,您可以使用创建自定义API 马上
using (var context = new YourContext())
{
context.ExecuteStoreCommand("DELETE FROM YOURTABLE WHERE Condition = value");
}