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 Store App > ตอนที่ 3 : การบันทึกข้อมูลจาก Windows Store App ลงใน Azure Mobile Services



Clound SSD Virtual Server

ตอนที่ 3 : การบันทึกข้อมูลจาก Windows Store App ลงใน Azure Mobile Services

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

กลับมายังหน้า Dashboard ของ Azure Mobile Services

Windows Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

คลิกที่ Windows Store

Windows Store App Azure Mobile Services

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


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

Windows Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

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








Windows Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

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


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

Windows Store App Azure Mobile Services

โครงสร้างไฟล์

Windows Store App Azure Mobile Services

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

    sealed partial class App : Application
    {

        public static MobileServiceClient MobileService = new MobileServiceClient(
            "https://thaicreate.azure-mobile.net/",
            "uJPAhkAkcTBuTyCNxaOSnDKFzkoYqB49"
        );


Windows Store App Azure Mobile Services

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

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

Windows Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

สร้าง 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 Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

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

Windows Store App Azure Mobile Services

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

Code ทั้งหมด (MainPage.xaml.cs)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

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

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace myApp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 
    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 sealed partial class MainPage : Page
    {

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

        public MainPage()
        {
            this.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;
            }
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            InsertMemberItem();
        }
    }
}









   
Share

Property & Method (Others Related)

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

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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-08-26 06:13:09 / 2017-03-24 12:51:02
  Download : Download  ตอนที่ 3 : การบันทึกข้อมูลจาก Windows Store App ลงใน Azure Mobile Services
 Sponsored Links / Related

 
ตอนที่ 1 : การสร้าง Azure Mobile Services เพื่อใช้งานร่วมกับ Windows Store App
Rating :

 
ตอนที่ 2 : การปรับแต่ง Windows Store App เพื่อทำงานร่วมกับ Azure Mobile Services
Rating :

 
ตอนที่ 4 : การนำข้อมูลจาก Azure Mobile Services มาแสดงบน Windows Store App
Rating :

 
ตอนที่ 5 : Windows Store App : Register Form (Azure Mobile Services)
Rating :

 
ตอนที่ 6 : Windows Store App : Login User Password (Azure Mobile Services)
Rating :

 
ตอนที่ 7 : Windows Store App : Update Data (Azure Mobile Services)
Rating :

 
ตอนที่ 8 : Windows Store App : Delete Data (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 00
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 อัตราราคา คลิกที่นี่