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 > Mobile > [iOS/iPhone] Tutorials - สอนเขียน iPhone App ฟรี เขียน iPad App เรียน iPhone เขียนโปรแกรม iPhone > ตอนที่ 3 : iOS C# (Xamarin.iOS) สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล



Clound SSD Virtual Server

ตอนที่ 3 : iOS C# (Xamarin.iOS) สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล

ตอนที่ 3 : iOS C# (Xamarin.iOS) สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล บทความ Xamarin.iOS บน Visual Studio นี้ขอต่อจากตอนที่แล้ว โดยจะยกตัวอย่างในกรณีที่เรามี iOS App หรือ Project อยู่แล้ว แต่ต้องการที่จะเรียกใช้ Mobile Services ของ Windows Azure ซึ่งโดยพื้นฐานแล้วจะเป็นการสร้าง Table ไว้สำหรับจัดเก็บข้อมูลบน Mobile Services การเขียนคำสั่งในฝั่งของ iOS App ด้วย C# เพื่อสร้าง Column หรือฟิวด์ พร้อม ๆ กับการส่งข้อมูลจาก iOS ที่ทำหน้าที่เป็น Client แล้วนำไปจัดเก็บ Insert ไว้ใน Table ของ Mobile Services บน Windows Azure

ในการเขียน iOS C# ด้วย Xamarin.iOS เพื่อติดต่อกับ Azure Mobile Services เราจะต้องทำการเรียกใช้ Library ที่ได้ถูกออกแบบมาใช้สำหรับการเขียนเพื่อติดต่อกับ Azure Mobile Services โดยเฉพาะ โดยสามารถติดตั้งได้จาก NuGet Package ที่อยู่บน Visual Studio โดยรองรับตั้งแต่ Visual Studio 2010 Professional หรือสูงขึ้นไป

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

กลับมายัง Project ของ iOS C# บน Visual Studio

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

ตอนนี้หน้าจอ Viewของเราที่อยู่บน Storyboard ยังเป็นหน้าจอว่าง

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

สร้าง Label บนหน้าจอ View พร้อมทั้งกำหนด ID/Name ดังรูป

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

ให้กลับไปยัง Azure Mobile Services บนหน้าขอ Dashboard ซึ่งเราจะได้คำแนะนำในการเรียกใช้งาน ด้วย iOS (C#) กับ Xamarin.iOS โดยคำแนะนำให้ติดตั้ง Library ผ่าน NuGet Package








การติดตั้ง Library ของ Mobile Services บน Visual Studio 2010

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

คลิกขวาที่ Preferences เลือก Add Library Package Preferences

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

ค้นหาคำว่า "Azure Mobile Services" เลือก "Windows Azure Mobile Services"

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

เลือกเพื่อ Install

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

กรณีที่ Error

Error
'Newtonsoft.Json (? 6.0.4)' not installed. Attempting to retrieve dependency from source...
Done.
The schema version of 'WindowsAzure.MobileServices' is incompatible with version 1.2.20325.9034 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.

กรณีที่ Error นี้ให้ Upgrade ตัว NuGet ให้เป็น Version 2.8.1 หรือมากกว่า

https://nuget.codeplex.com/releases/view/118318


iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

หลังจากที่ติดตั้งเรียบร้อยแล้วรายการ Library ต่าง ๆ จะถูก Include เข้ามาใน Preferences


การติดตั้ง Library ของ Mobile Services บน Visual Studio 2012

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

คลิกขวาที่ Preferences เลือก Manage NuGet Package

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

ค้นหาคำว่า "Azure Mobile Services" เลือก "Windows Azure Mobile Services"

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

เลือกเพื่อ Install

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

กรณีที่ Error ซึ่งจะเป็น Error เดียวกับ VS 2010

Error
The 'Microsoft.Bcl 1.1.9' package requires NuGet client version '2.8.1' or above, but the current NuGet version is '2.5.40416.9020'

กรณีที่ Error นี้ให้ Upgrade ตัว NuGet ให้เป็น Version 2.8.1 หรือมากกว่า

https://nuget.codeplex.com/releases/view/118318

ทำการ Update ตัว NuGet ซะก่อน

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

หลังจากที่ติดตั้งเรียบร้อยแล้วรายการ Library ต่าง ๆ จะถูก Include เข้ามาใน Preferences

หลังจากที่ทำการ Include ไฟล์ Library ต่าง ๆ ซึ่งอาจจะมี Library หลาย ๆ ตัวที่ถูก Include เข้ามาด้วย และต่อไปนี้เราก็พร้อมที่จะทำการเรียกใช้งาน Azure Mobile Services ด้วย iOS C# แล้ว

ขั้นตอนการสร้าง Table หรือตารางบน Azure Mobile Services

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

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

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

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

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

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








เพิ่มเติม
จะสังเกตุว่าไม่มีเครื่องมือสำหรับการสร้าง Table และการ Insert ข้อมูล (มีแต่ลบ Column และบน Rows ข้อมูล) แต่ทั้งนี้เราสามารถที่จะสร้าง Column และ Insert ข้อมูลได้จากการเขียน App บน iOS

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

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

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

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

การเรียกใช้งานด้วย iOS C# (Xamarin.iOS)

เรียกใช้ Library ของ Mobile Sevices และ Json
using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;

สร้าง Class สำหรับ Mapping ตัว Table ซึ่ง Property ต่าง ๆ จะได้เป็นชื่อ Column ของ Table
    public class MyMember
    {
        public int Id { get; set; }

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

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

สร้าง URL และ Key ในการเชื่อมต่อ
public const string ApplicationURL = @"https://thaicreate.azure-mobile.net/";
public const string ApplicationKey = @"IqeWShAjBflTUrTaaGUNJRyZDpcyeh72";

เชื่อมต่อไปยัง Mobile Services
private MobileServiceClient client; // Mobile Service Client references
client = new MobileServiceClient(ApplicationURL, ApplicationKey);

เรียกใช้และ Mapping ตัว Table
private IMobileServiceTable<MyMember> memberTable; // Mobile Service Table used to access data   
memberTable = client.GetTable<MyMember>();

การ Insert ข้อมูล
var item = new MyMember { Name = "Win", Email = "[email protected]" };
memberTable.InsertAsync(item);


Code เต็ม ๆ

MainActivity.cs
using System;
using System.Drawing;

using Foundation;
using UIKit;

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

namespace iOSApp
{
    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 RootViewController : UIViewController
    {

        public const string ApplicationURL = @"https://thaicreate.azure-mobile.net/";
        public const string ApplicationKey = @"IqeWShAjBflTUrTaaGUNJRyZDpcyeh72";

        private MobileServiceClient client; // Mobile Service Client references
        private IMobileServiceTable<MyMember> memberTable; // Mobile Service Table used to access data  

        public RootViewController(IntPtr handle)
            : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            this.AddData();
        }

        public void AddData()
        {
            try
            {
                client = new MobileServiceClient(ApplicationURL, ApplicationKey);

                memberTable = client.GetTable<MyMember>();

                var item = new MyMember { Name = "Win", Email = "[email protected]" };
                memberTable.InsertAsync(item);

                this.lblResult.Text = "Insert Data Successfully.";
            }
            catch (Exception ex)
            {
                this.lblResult.Text = "Insert Data Failed! Error " + ex.Message;
            }

        }

    }
}

ทดสอบการทำงาน

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

แสดงผลบนหน้าจอ App ของ iOS/b]

iOS C# (Xamarin.iOS)  Table and Mobile Services Insert

เมื่อกลับไปดูบน [b]Portal Management
ของ Mobile Services จะเห็นว่า Column ถูกสร้าง และ Rows ถูก Insert และจะเห็นว่าบาง Column จะมีการสร้างให้อัตโนมัติเช่น _createAt , _updateAt



อ่านเพิ่มเติม

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-10-24 14:01:33 / 2017-03-26 08:43:31
  Download : No files
 Sponsored Links / Related

 
ตอนที่ 1 : รู้จัก iOS C# (Xamarin.iOS) Mobile Services บน Windows Azure คืออะไร
Rating :

 
ตอนที่ 2 : การสร้าง iOS C# (Xamarin.iOS) Mobile Services และการเรียกใช้งานแบบง่าย ๆ
Rating :

 
ตอนที่ 4 : iOS C# (Xamarin.iOS) อ่าน Data จาก Table ของ Mobile Services และแสดงผลบน App
Rating :

 
ตอนที่ 5 : Show Case 1 : Register Form (iOS C# (Xamarin.iOS) and Mobile Services)
Rating :

 
ตอนที่ 6 : Show Case 2 : Login User Password (iOS C# (Xamarin.iOS) and Mobile Services)
Rating :

 
ตอนที่ 7 : Show Case 3 : Update Data (iOS C# (Xamarin.iOS) and Mobile Services)
Rating :

 
ตอนที่ 8 : Show Case 4 : Delete Data (iOS C# (Xamarin.iOS) and 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 อัตราราคา คลิกที่นี่