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 ตอนที่ 2 : Windows Store Apps ทำการ Insert , Update , Delete (C#)



Clound SSD Virtual Server

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

MySQL ตอนที่ 2 : Windows Store Apps ทำการ Insert , Update , Delete (C#) ในตอนที่ 1 เราได้เรียนรู้วิธีการปรับแต่งติดตั้งชุด MySQL.Data.RT เพื่อใช้สำหรับเขียนติดต่อกับ MySQL Database แล้ว ในตอนที่ 2 ของบทความ Windows Store Apps กับ MySQL จะขอขยายความเกี่ยวกับการทำงานร่วมกับ MySQL มากขึ้น เช่น Insert (การเพิ่มข้อมูล) , Update (แก้ไขข้อมูล) , Delete (ลบข้อมูล) ซึ่งถ้าเคยเขียน.NET ติดต่อกับ MySQL ผ่าน MySql.Data.MySqClient มาแล้ว ต้องบอกว่า ไม่แตกต่างกัน สามารถใช้รูปแบบการเขียนได้เหมือนกันทุกประการ ทั้งรูปแบบ DataReder , DataSet , DataTable หรือการ Query ต่าง ๆ

Example ตัวอย่างการเขียน Windows Store Apps เพื่อ Insert / Update / Delete ข้อมูล ใน MySQL Database

โครงสร้างของ 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 Insert , Update , Delete

ข้อมูลในตาราง

Windows Store Apps Insert , Update , Delete

บนโปรเจคของ Windows Store Apps ให้ทำการ Add Reference ของ MySql.Data.RT.dll ให้เรียบร้อย








Insert Syntax รูปแบบการ Insert ลงใน MySQL Database บน Windows Store Apps

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();

                //*** Example 1 ***//
                string sql1 = "INSERT INTO member (Name) VALUES ('Adisorn Nitipakron')";
                MySqlCommand cmd1 = new MySqlCommand(sql1, connection);
                cmd1.ExecuteNonQuery();

                //*** Example 2 ***//
                string sql2 = "INSERT INTO member (Name) VALUES (?sName)";
                MySqlCommand cmd2 = new MySqlCommand(sql2, connection);
                cmd2.Parameters.Add("?sName", MySqlDbType.VarChar).Value = "Surapong Junsiriphun";
                cmd2.ExecuteNonQuery();

                this.lblResult.Text = "Record has been added.";
            }
        }
       
    }
}



Update Syntax รูปแบบการ Update ใน MySQL Database บน Windows Store Apps

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();

                //*** Example 1 ***//
                string sql1 = "UPDATE member SET Name = 'Adisorn Junsiriphun' WHERE ID = '3'";
                MySqlCommand cmd1 = new MySqlCommand(sql1, connection);
                cmd1.ExecuteNonQuery();
                
                //*** Example 2 ***//
                string sql2 = "UPDATE member SET Name = sName WHERE ID = sID";
                MySqlCommand cmd2 = new MySqlCommand(sql2, connection);
                cmd2.Parameters.Add("?sName", MySqlDbType.VarChar).Value = "Surapong Nitipakron";
                cmd2.Parameters.Add("?sID", MySqlDbType.Int32).Value = 4;
                cmd2.ExecuteNonQuery();

                this.lblResult.Text = "Record has been updated.";
            }
        }
       
    }
}



Delete Syntax รูปแบบการ Update ใน MySQL Database บน Windows Store Apps

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();

                //*** Example 1 ***//
                string sql1 = "DELETE FROM member WHERE ID = '3'";
                MySqlCommand cmd1 = new MySqlCommand(sql1, connection);
                cmd1.ExecuteNonQuery();
                
                //*** Example 2 ***//
                string sql2 = "DELETE FROM member WHERE ID = sID";
                MySqlCommand cmd2 = new MySqlCommand(sql2, connection);
                cmd2.Parameters.Add("?sID", MySqlDbType.Int32).Value = 4;
                cmd2.ExecuteNonQuery();

                this.lblResult.Text = "Record has been updated.";
            }
        }
       
    }
}


Windows Store Apps Insert , Update , Delete

คัวอย่างข้อมูลที่ถูก Insert ลงใน MySQL Database

สำหรับรายละเอียดอื่น ๆ สามารถอ่านเพิ่มเติมได้จากบทความที่เกี่ยวข้องกับ .NET และ MySQL

ASP.NET MySQL (MySql.Data.MySqlClient)

ASP.NET & MySql.Data.MySqlClient








.

   
Share


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


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


   


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