ตัวอย่างการสร้าง Database ซึ่งจะจัดเก็บไว้ใน Local Data
var uri = new Uri("ms-appx:///data.db3"); //in application folder
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
var destinationFolder = ApplicationData.Current.LocalFolder;//local appdata dir
// var f = await destinationFolder.GetFileAsync("data.db3");
await file.CopyAsync(destinationFolder); //copied application local folder}}
public class person
{
[MaxLength(5), PrimaryKey]
public String name { get; set; }
[MaxLength(255)]
public String address { get; set; }
[MaxLength(11)]
public Double phone { get; set; }
}
private async void createtable(object sender, RoutedEventArgs e)
{
try
{
var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
using (var db = new SQLite.SQLiteConnection(dbpath))
{
// Create the tables if they don't exist
db.CreateTable<person>();
db.Commit();
db.Dispose();
db.Close();
}
var line = new MessageDialog("Table Created");
await line.ShowAsync();
}
catch
{
}
}
ตัวอย่างการ Insert ข้อมูล
var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
using (var db = new SQLite.SQLiteConnection(dbpath))
{
// Create the tables if they don't exist
db.Insert(new person()
{
name = txt1.Text.ToString(),
address = txt2.Text.ToString(),
phone = Convert.ToDouble(txt3.Text.ToString()),
}
);
db.Commit();
db.Dispose();
db.Close();
var line = new MessageDialog("Records Inserted");
await line.ShowAsync();
}
ตัวอย่างการ Delete ข้อมูล
var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
using (var db = new SQLite.SQLiteConnection(dbpath))
{
db.Delete<person>(list1.SelectedItem.ToString());//selected item
var d = from x in db.Table<person>() select x;
list1.Items.Clear();
foreach (var sd in d)
{
list1.Items.Add(sd.name.ToString());
//list1.Items.Add(sd.address.ToString());
//list1.Items.Add(sd.phone.ToString());
}
db.Dispose();
db.Close();
}
ตัวอย่างการ อ่าน ข้อมูล
var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
using (var db = new SQLite.SQLiteConnection(dbpath))
{
var d = from x in db.Table<person>() select x;
list1.Items.Clear();
foreach (var sd in d)
{
list1.Items.Add(sd.name.ToString());
//list1.Items.Add(sd.address.ToString());
//list1.Items.Add(sd.phone.ToString());
}
db.Dispose();
db.Close();