Real Estate builders & developers: Arun Dev Builders Delhi NCR
Session 8 connect your universal application with database .. builders & developers workshop MSTC15
-
Upload
moatasim-magdy -
Category
Education
-
view
221 -
download
0
Transcript of Session 8 connect your universal application with database .. builders & developers workshop MSTC15
Create Table CREATE TABLE table_name ( Column_name1 data_type(Size), Column_name2 data_type(Size), Column_name3 data_type(Size), ……. );
Insert Query INSERT INTO table_name VALUES (value1, value2 , value3 …);
OR
INSERT INTO table_name (column1, column2, column3 ….) VALUES (value1, value2 , value3 …);
Select query SELECT column_name, column_name2 FROM table_name WHERE column_name operator value;
OR SELECT * FROM table_name WHERE column_name operator value;
Operators = Equal <> Not equal > Greater than < Less than >= Greater than <= Less than LIKE Search for pattern
Delete Query DELETE FROM table_name WHERE some_column = some_value;
Drop Table DROP TABLE table_name
Step 1 First press tools then open extensions and updates, make sure these 2 extensions are available.
Step 2 Then from solution explorer right click on references from windows 8.1 part then select add reference.
Step 4 Then from solution explorer right click on references from windows 8.1 part then select add reference.
Step 7 To solve this warnings open configuration manager from the Debug in the top bar of visual studio.
Step 8 Right click on the project from solution and explorer then open “manage nugget packages” and search for sqlite-net then install.
Step 9To make sure every thing is going right you`ll notice appearance of this 2 classes in windows and windows phone part.
Connect your database with C# Create Database. Check existence of database. Create Table , Create using database connection. Add record(s) to your table Select record from your table Remove record from your table Update record from your table Drop table
Table We start defining the class that represent the table row -> Then add the table to the database
Select record from your table SQLiteAsyncConnection conn = new SQLiteAsyncConnection("people.db");
var cityUsers = await conn.QueryAsync<User>(
"SELECT * FROM Users WHERE Name = ?", new object[] { textbox1.Text });
foreach (var user in cityUsers)
{
UserList.ItemsSource = cityUsers;
}
Select all records SQLiteAsyncConnection conn = new SQLiteAsyncConnection("people.db");
bool dbExist = await CheckDbAsync("people.db");
if (!dbExist)
{
await CreateDataBase();
}
var query = conn.Table<User>();
users = await query.ToListAsync();
UserList.ItemsSource = users;
Delete Record private void Button_Click_5(object sender, RoutedEventArgs e)
{
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "people.db");
using (var db = new SQLite.SQLiteConnection(dbPath))
{ try
{ db.Execute("DELETE FROM Users WHERE Name = ?", textbox1.Text); }
catch (Exception ex)
{ // EXCEPTION HANDLING }}}
Update Record
private void Button_Click_6(object sender, RoutedEventArgs e)
{
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "people.db");
using (var db = new SQLite.SQLiteConnection(dbPath))
{ try
{ db.Execute("UPDATE Users SET Name = ? WHERE Name = ?", textbox2.Text,textbox1.Text); }
catch (Exception ex)
{ // EXCEPTION HANDLING }}}
XAML: How to do list ? <ListView x:Name="UserList" Margin="671,53,20,20">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding City}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>