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 ตอนที่ 3 : Windows Store Apps กับ MySQL ข้าม Host หรือ Server (C#)



Clound SSD Virtual Server

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

MySQL ตอนที่ 3 : Windows Store Apps กับ MySQL ข้าม Host หรือ Server (C#) การเขียน Windows Store Apps ติดต่อกับ MySQL แบบข้ามเครื่องหรือข้าม Server/Host เป็นเทางเลือกหนึ่งในการสร้าง Application ที่ติดต่อกับ Database ซึ่งสามารถข้าม Server ได้ทั้ง Network ที่อยู่ใน Intranet Local (Lan/Wan) หรือ WWW (Internet) ได้ แต่มีข้อจำกัดว่าจะต้องมีการอนุญาติให้เชื่อมต่อผ่าน Port : 3306 และฝั่งของ MySQL จะต้องเพิ่ม IP ของเครื่องที่จะเชื่อมต่อ หรือจะใช้ % สำหรับการอนุญาติให้ทุก ๆ เครื่อง สามารถติดต่อได้อย่างอิสระ

ข้อกำหนดในการเชื่อมต่อ MySQL ระหว่าง Server
  • เครื่องที่ติดตั้งหรือรัน App และ MySQL Server สามารถติดต่อสื่อสารกันได้ผ่านระบบ Lan หรือ Internet
  • MySQL Database เครื่องที่เป็น Server จะต้อง Allow Port 3306 และ Network ของ Apps ก็ 3306 เช่นเดียวกัน
  • กำหนด Host สำหรับ User เป็น IP ของเครื่อง Client หรือจะใช้ % สำหรับเครื่องใด ๆ ก็ได้ (ปกติเป็น localhost)


สรุปก็คือ MySQL ทำงานใน Port Default เป็น 3306 และ User สำหรับเชื่อมต่อจะต้องกำหนดเป็น IP หรือ %

Windows Store Apps MySQL Host Server

นส่วนของ Host ให้เปลี่ยนเป็น IP ของ Client ที่จะเชื่อมต่อ หรือจะใช้เป็น % (ปกติจะเป็น localhost) และสามารถเพิ่มได้หลาย ๆ IP

ตัวอย่างการ Config บน Server จริง

โครงสร้างของ Table และ Data
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 MySQL Host Server

ข้อมูลใน Table








การปรับแต่ง Config เพื่อเขียน Windows Store Apps และติดต่อกับ MySQL Database บน Host Server (Direct Admin)

Windows Store Apps MySQL Host Server

บน Direct Admin ให้เลือก Create new Database

Windows Store Apps MySQL Host Server

หลังจากได้ Database ให้คลิกที่ Database นั้น ๆ

Windows Store Apps MySQL Host Server

จะเห็นว่าตอนนี้มี User ชื่อว่า thaiwin_onlinedb ซึ่งจะ Connect ภายใต้ localhost คือเรียกได้เฉพาะในเครื่องเท่านั้น เราสามารถเพิ่ม IP ของ Client ที่ต้องการจะเชื่อมต่อได้

Windows Store Apps MySQL Host Server

ตัวอย่างการเพิ่ม % คือไม่เจาะลงว่าจะเป็น Client ไหนก็ได้

Windows Store Apps MySQL Host Server

ได้ User ชื่อว่า % ซึ่งต่อไปนี้เราจะเรียกจากที่ไหนก็ได้ ซึ่งเราจะได้คำสั่ง Connection String เป็น

server=[IP/Server];database=[Database-Name];uid=[User];password=[Password];


Windows Store Apps MySQL Host Server

หลังจากนั้นให้เปิด phpMyAdmin เพื่อ dump ตัว Table และ Data ให้เรียบร้อย

Windows Store Apps MySQL Host Server

กลับมาที่ Project ของ Windows Store Apps บน Visual Studio ให้เรา Add Reference ของ MySql.Data.RT.dll ให้เรียบร้อย จากนั้นเขียน 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=122.155.17.251;database=thaiwin_onlinedb;uid=thaiwin_onlinedb;password=0bHmoCs3;";

            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 MySQL Host Server

แสดงข้อมูลของ MySQL บน Windows Store Apps ที่อยู่บน Host และ Server ผ่านระบบ Internet







.

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-03-17 12:21:06 / 2017-03-19 14:52:28
  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 ตอนที่ 1 : Windows Store Apps ติดต่อกับ MySQL Database (C#)
Rating :

 
MySQL ตอนที่ 2 : Windows Store Apps ทำการ Insert , Update , Delete (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 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 อัตราราคา คลิกที่นี่