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 Store Apps > Windows Store and Storage / Data (C#) > MySQL ตอนที่ 1 : Windows Store Apps ติดต่อกับ MySQL Database (C#)



Clound SSD Virtual Server

MySQL ตอนที่ 1 : Windows Store Apps ติดต่อกับ MySQL Database (C#)

MySQL ตอนที่ 1 : Windows Store Apps ติดต่อกับ MySQL Database (C#) รูปแบบการทำงานของ Windows Store Apps จะแตกต่างกับ Desktop Application หรือ Application อื่นทั่ว ๆ ไปที่เขียนด้วยชุดคำสั่ง .NET Framework โดยใน Windows Store Apps จะมีการใช้ชุด API ของ WinRT (Desktop App จะใช้ Win 32 API) ซึ่งเป็น Platform ใหม่ของ Application ที่จะทำงานในระบบปฏิบัติการ Windows 8 เป็นต้นไป ซึ่งรองรับได้ทั้ง PC / Desktop ที่ติดตั้ง Windows 8 และอุปกรณ์ประเภท Tablet ด้วยเหตุผลนี้เอง Application ที่พัฒนาด้วย Windows Store Apps ซึ่งใช้ชุดคำสั่งของ WinRT API จะมีความกระทัดรัด ไฟล์ Source ต่าง ๆ ของโปรแกรม จะถูกรวมเป็น Package เดียวที่สามารถติดตั้งง่าย และเสร็จสิ้นภายในตัวเอง โดยไม่ต้องไปอาสัยการติดตั้งโปรแกรมอื่น ๆ มาเกี่ยวข้อง ฉะนั้นการเขียนโปรแกรมที่ต้องอาศัยพวก Database ต่าง ๆ จะถูกตัดออก ถ้าให้เข้าใจง่าย ๆ คือ NameSpace ของ System.Data และชุดคำสั่งของ ADO.Net ที่จะใช้ติดต่อกับ Database จะไม่มีอีกแล้ว ซึ่งจะเป็นเหตุผลที่เราจะต้องเปลี่ยนรูปแบบการใช้ Database มาเป็นแบบ Database ที่เป็น Online กันมากขึ้น โดยอาจจะอาศัยการติดต่อผ่านพวก Web Service API , หรือ REST API เป็นต้น

แต่ก็ใช่ว่าจะไม่สามารถเขียนติดต่อกับ Database ได้เลยเพราะ MySQL ได้มีการพัฒนาชุดคำสั่ง API ไว้รองรับ WinRT โดยเฉพาะ ชื่อว่า MySql.Data.RT ซึ่งสามารถเขียน Windows Store Apps ติดต่อกับ Database ทั้งที่อยู่บน Local หรือจะติดต่อกับ MySQL Database แบบ Online ที่เชื่อมต่อผ่านระบบ Internet ก็ได้เช่นเดียวกัน

สิ่งที่จำเป็นต้องมีสำหรับการเขียน Windows Store Apps เพื่อติดต่อกับ MySQL Database
  • MySQL Connector/NET 6.7 or later
  • MySql Server 5.1 or above

ในขั้นแรกให้ติดตั้ง MySQL Database และ MySQL Connector ให้เรียบร้อยซะก่อน หรือกรณีที่มี MySQL อยู่แล้วก็ไม่จำเป็น ให้ติดตั้งเฉพาะ MySQL Connector/NET

Windows Store Apps Storage MySQL Database

Download Connector/Net
http://dev.mysql.com/downloads/connector/net/


จากนั้นไปที่โปรแกรม Visual Studio บน Windows Store Apps โปรเจคที่สร้างขึ้น

Windows Store Apps Storage MySQL Database

ให้ทำการ Add Reference โดยเลือกไฟล์ MySql.Data.RT.dll ซึ่งจะอยู่ที่

C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\RT

Path อาจจะเปลี่ยนแปลงบ้างตาม Version ที่ติดตั้ง

Windows Store Apps Storage MySQL Database

เลือก Library ชอง MySql.Data.RT.dll

Windows Store Apps Storage MySQL Database

ตอนนี้เรามีชุดคำสั่งของ MySql.Data.RT เรียบร้อยแล้ว ซึ่งต่อไปนี้เราก็สามารถเขียนติดต่อกับ MySQL ได้ทันที

ทดสอบการสร้าง Table บน MySQL แบบง่าย ๆ ซึ่งเราจะดึงข้อมูลนี้มาแสดง
CREATE TABLE `member` (
  `ID` int(2) NOT NULL auto_increment,
  `Name` varchar(50) NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

INSERT INTO `member` VALUES (1, 'Weerachai Nukitram');
INSERT INTO `member` VALUES (2, 'Adisorn Boonsong');
INSERT INTO `member` VALUES (3, 'Surachai Sirisart');


Windows Store Apps Storage MySQL Database

ข้อมูลที่อยู่บน Database








รูปแบบการเชื่อมต่อกับ MySQL Database บน Windows Store Apps

Windows Store Apps Storage MySQL Database

จะเหมือนกับ .NET Application คือจะต้องเรียกใช้ using MySql.Data.MySqlClient; ซึ่งเป็นการเรียกใช้ NameSpace และชุดคำสั่งของ MySQL.Data.RT ในการติดต่อกับ MySQL Database

string strConnection = "server=localhost;database=mydatabase;uid=root;password=root;";
MySqlConnection connection = new MySqlConnection(strConnection)
connection.Open();

ชุดคำสั่งสำหรับการเชื่อมต่อรวมทั้ง Connection String จะไม่ต่างอะไรกับ .NET Application อื่น ๆ ที่เขียนเพื่อติดต่อกับ MySQL Database

มาดู Code เต็ม ๆ

MainPage.xaml
<Page
    x:Class="WindowsStoreApps.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsStoreApps"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">


    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <TextBlock x:Name="lblResult" HorizontalAlignment="Left" Margin="120,122,0,0" TextWrapping="Wrap" Text="Result" VerticalAlignment="Top" FontSize="30"/>

    </Grid>

</Page>

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
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 System.Text;
using MySql.Data.MySqlClient;

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

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

    
            string strConnection = "server=localhost;database=mydatabase;uid=root;password=root;";

            using (MySqlConnection connection = new MySqlConnection(strConnection))
            {
                connection.Open();

                StringBuilder sb = new StringBuilder();

                MySqlCommand cmd = new MySqlCommand("SELECT * FROM member", connection);
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        sb.AppendLine(reader.GetString("Name"));
                    }
                }

                this.lblResult.Text = sb.ToString();
            }
        }
       
    }
}

Screenshot

Windows Store Apps Storage MySQL Database

ตัวอย่างการอ่านข้อมูลจาก MySQL Database มาแสดงบนหน้าจอของ Windows Store Apps







.

   
Share


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


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


   


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

 
Local app data จัดเก็บไฟล์ลง Storage บน Windows Store Apps (C#)
Rating :

 
Roaming app data จัดเก็บไฟล์ลง Storage บน Windows Store Apps (C#)
Rating :

 
Temporary app data จัดเก็บไฟล์ลง Storage บน Windows Store Apps (C#)
Rating :

 
File Dialog และ Save จัดเก็บไฟล์ลง Storage ของ Windows Store Apps (C#)
Rating :

 
Copy ไฟล์ลง Storage แสดงชื่อไฟล์ใน Storage บน Windows Store Apps (C#)
Rating :

 
ดาวน์โหลดจัดเก็บไฟล์ลงบน Storage ของ Windows Store Apps (C#)
Rating :

 
การสร้าง Text file และการจัดเก็บบน Storage ของ Windows Store Apps (C#)
Rating :

 
MySQL ตอนที่ 2 : Windows Store Apps ทำการ Insert , Update , Delete (C#)
Rating :

 
MySQL ตอนที่ 3 : Windows Store Apps กับ MySQL ข้าม Host หรือ Server (C#)
Rating :

 
Windows Store Apps DataBinding - ListView / Database Binding (C#)
Rating :

 
Windows Store Apps DataBinding - ListBox / Database Binding (C#)
Rating :

 
Windows Store Apps Databinding - Retrieve Data Master and Detail (C#)
Rating :

 
Windows Store Apps DataBinding - GridView / Database Binding (C#)
Rating :

 
SQLite : Windows Store Apps ติดต่อกลับ SQLite Database (C#)
Rating :

 
Windows Store Apps การอ่าน Text file และ CSV และการ Data Binding(C#)
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 อัตราราคา คลิกที่นี่