Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Azure > Windows Azure (Mobile Services) and Windows Phone (WP8) > ตอนที่ 5 : Widows Phone สร้าง Table บน Mobile Services และการ Insert ข้อมูล



Clound SSD Virtual Server

ตอนที่ 5 : Widows Phone สร้าง Table บน Mobile Services และการ Insert ข้อมูล

ตอนที่ 5 : Windows Phone สร้าง Table บน Mobile Services และการ Insert ข้อมูล บทความนี้ขอต่อจากตอนที่แล้ว ซึ่งจะเป็นการสร้าง Table ไว้สำหรับจัดเก็บข้อมูลบน Mobile Services และการเขียน Windows Phone App เพื่อสร้าง Column หรือฟิวด์ พร้อม ๆ กับการส่งข้อมูลจาก Windows Phone แล้วนำไปจัดเก็บ Insert ไว้ใน Table ของ Mobile Services บน Windows Azure

Windows Phone(WP) Mobile Services Create Table

ตอนนี้เรามี Mobile Services อยู่ 1 ตัว

Windows Phone(WP) Mobile Services Create Table

ในหน้า Get Started จะแสดงรายละเอียดของการเชื่อมต่อ และได้อธิบายไว้ในบทความก่อน ๆ หน้านี้แล้ว

ขั้นตอนการสร้าง Table หรือตาราง

Windows Phone(WP) Mobile Services Create Table

คลิกที่ DATA และเลือก CREATE

Windows Phone(WP) Mobile Services Create Table

ใส่ชื่อตารางในที่นี้จะใส่เป็น MyMember








Windows Phone(WP) Mobile Services Create Table

ได้ตารางขึ้นมา 1 รายการชื่อว่า MyMember

Windows Phone(WP) Mobile Services Create Table

ให้คลิกเข้าไปใน Table (ตาราง) ซึ่งตอนนี้ยังไม่มี Column และ Rows

Windows Phone(WP) Mobile Services Create Table

เมนู SCRIPT เป็นพวก Script ที่ไว้ทำหน้าที่รับข้อมูลจาก Windows Phone แล้ว Insert ลงใน Table การทำงานคล้าย ๆ กับ Stored Procedure ซึ่งเราสามารถเขียน Script เพิ่มเติมได้ แต่ตอนนี้แนะนำให้กำหนดเป็นค่า Default ซะก่อน

Windows Phone(WP) Mobile Services Create Table

หลัก ๆ จะมีอยู่ 4 ตัวคือ Insert , Update , Delete , Read

Windows Phone(WP) Mobile Services Create Table

กลับมาบน Project ของ Windows Phone บน Visual Studio

App.xaml.cs

Windows Phone(WP) Mobile Services Create Table

ในไฟล์ App.xaml.cs ให้เพิ่ม Url และ Key สำหรับการเชื่อมต่อไปยัง Mobile Services


MainPage.xaml.cs

Windows Phone(WP) Mobile Services Create Table

ให้ทำการ Import ตัว Library ของ Mobile Services

Windows Phone(WP) Mobile Services Create Table

ประกาศ Class ชื่อว่า MyMember ซึ่งเป็นชื่อเดียวกับ Table โดยรายการ Property ต่าง ๆ จะเปรียบเหสมือนชื่อฟิวด์

โดยมีเงื่อนไขว่า
1. ฟิวด์แรกจะต้องชื่อว่า id เป็นชนิดแบบ int
2. การสร้าง Column จะเรียกใช้งานทุก ๆ ครั้งที่ App ทำงาน โดยที่โปรแกรมจะตรวจสอบก่อนว่ามี Column แล้วหรือยัง ถ้ามีแล้วจะไม่สร้างเพิ่ม
3. ในกรณีที่มี Column ใหม่ สามารถมาสร้างเพิ่มได้ทันที เพราะเมื่อโปรแกรมทำงานใหม่อีกครั้ง Column ใหม่ก็จะถูกสร้างขึ้นมาใหม่

Windows Phone(WP) Mobile Services Create Table

จากนั้นประกาศตัวแปรสำหรับเรียกใช้ Library

Windows Phone(WP) Mobile Services Create Table

สร้าง Method ทีชื่อว่า InsertMemberItem เพื่อเป็นการ Insert ข้อมูล โดยคำสั่งมีง่าย ๆ เพียง

                items = await memberTable.ToCollectionAsync();
                var insertItem = new MyMember { Name = "Win", Email = "[email protected]" };
                await memberTable.InsertAsync(insertItem);
                items.Add(insertItem);
                lblStatus.Text = "Insert Data Successfully.";

เป็นการ Insert ฟิวด์ Name='Win' และ Email='[email protected]'

Windows Phone(WP) Mobile Services Create Table

เรียกใช้ Method นี้ที่ OnNavigatedTo() // เป็น Method ที่ทำงานหลังจาก Page โหลดสมบูรณ์และทำงานอยู่ในสถานะปัจจุบันคล้าย ๆ กับ OnLoad()

Windows Phone(WP) Mobile Services Create Table

จากนั้นทดสอบผ่าน Emulator

Windows Phone(WP) Mobile Services Create Table

แสดงข้อความ Insert Data Successfully. บนหน้า Page

Windows Phone(WP) Mobile Services Create Table

เมื่อกลับไปดูที่ Mobile Services บน Windows Azure ข้อมูลก็จะถูก Insert เข้าไปในตาราง Table

Code ทั้งหมด ของ C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using myPhoneApp.Resources;

using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;

namespace myPhoneApp
{

    public class MyMember
    {
        public int Id { get; set; }

        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "email")]
        public string Email { get; set; }
    }

    public partial class MainPage : PhoneApplicationPage
    {

        private MobileServiceCollection<MyMember, MyMember> items;
        private IMobileServiceTable<MyMember> memberTable = App.MobileService.GetTable<MyMember>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private async void InsertMemberItem()
        {
            try
            {
                items = await memberTable.ToCollectionAsync();
                var insertItem = new MyMember { Name = "Win", Email = "[email protected]" };
                await memberTable.InsertAsync(insertItem);
                items.Add(insertItem);
                lblStatus.Text = "Insert Data Successfully.";
            }
            catch (MobileServiceInvalidOperationException e)
            {
                lblStatus.Text = "Insert Data Failed! Error " + e.Message;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            InsertMemberItem();
        }


    }
}










บทความถัดไปที่แนะนำให้อ่าน


บทความที่เกี่ยวข้อง


   
Share

Property & Method (Others Related)

How to read data in Mobile Services - Windows Phone (Windows Azure)
How to insert data to Mobile Services - Windows Phone (Windows Azure)
How to update data to Mobile Services - Windows Phone (Windows Azure)
How to delete data in Mobile Services - Windows Phone (Windows Azure)

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-11 21:15:21 / 2017-03-24 11:30:51
  Download : Download  ตอนที่ 5 : Widows Phone สร้าง Table บน Mobile Services และการ Insert ข้อมูล
 Sponsored Links / Related

 
ตอนที่ 1 : Windows Phone(WP) กับ Mobile Services บน Windows Azure คืออะไร
Rating :

 
ตอนที่ 2 : เตรียม Windows Phone(WP) ก่อนที่จะเขียนบน Azure Mobile Services
Rating :

 
ตอนที่ 3 : การสร้าง Windows Phone(WP) กับ Mobile Services และการเรียกใช้งาน
Rating :

 
ตอนที่ 4 : สร้าง Project Windows Phone(WP) และการเชื่อมต่อกับ Mobile Services
Rating :

 
ตอนที่ 6 : Windows Phone (WP) อ่าน Data จาก Table ของ Azure Mobile Service
Rating :

 
ตอนที่ 7 : อ่านข้อมูล Mobile Services การใช้ Where แสดงผลบน Windows Phone
Rating :

 
ตอนที่ 8 : การทำ Authentication in Azure Mobile Services ด้วย Windows Phone
Rating :

 
ตอนที่ 9 : การทำ Push Notifications in Mobile Services ด้วย Windows Phone
Rating :

 
ตอนที่ 10 : Validate และ Modify data in Mobile Services บน Windows Phone
Rating :

 
ตอนที่ 11 : สร้าง Refine Mobile Services queries with paging บน Windows Phone
Rating :

 
ตอนที่ 12 : Scripts to authorize users in Mobile Services บน Windows Phone
Rating :

 
ตอนที่ 13 : Show Case 1 : Register Form (WP and Mobile Services)
Rating :

 
ตอนที่ 14 : Show Case 2 : Login User Password (WP and Mobile Services)
Rating :

 
ตอนที่ 15 : Show Case 3 : Update Data (WP and Mobile Services)
Rating :

 
ตอนที่ 16 : Show Case 4 : Delete Data (WP and Mobile Services)
Rating :

 
ตอนที่ 17 : บทความอื่น ๆ เกี่ยวกับ Windows Phone กับ Azure Mobile Services
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่