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#) > SQLite : Windows Store Apps ติดต่อกลับ SQLite Database (C#)



Clound SSD Virtual Server

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

SQLite : Windows Store Apps ติดต่อกลับ SQLite Database (C#) SQLite เป็น Database ตัวหนึ่งที่ได้รับความนิยมค่อนข้างมาก ที่จะนำมาใช้กับ Application ประเภท Mobile เช่น Android , iOS และ Windows Phone และทั้งก็ยังสามารถใช้บน Windows Store Apps เพื่อจัดเก็บข้อมูลบน Apps ได้เช่นเดียวกัน เหตุผลก็คือ SQLite เป็น Database ขนาดเล็ก เป็น Open Source ใช้งานฟรี ใช้ Resource หรือทรัยพากรของเครืองน้อยมาก และหลาย ๆ OS บน อุปกรณ์ประเภท Mobile ก็ออกแบบ API ที่รองรับและสามารถใช้งาน SQLite บน Apps ได้ เพียงแค่ติดตั้ง Library หรือเรียกใช้ Library จากเครื่องมือที่มีให้เลือกใช้ และเมื่อนำไปใช้งาน SQLite สามารถรวมเป็น Package ไปกับ Apps เพื่อนำไปติดตั้งที่เครื่องปลายทางได้ทันที โดยไม่ต้องติดตั้งเพิ่มหรือ Config ค่าอื่น ๆ เพิ่มเติม

การแนะนำ SQLite เพื่อการทำงานที่ได้ประสิทธิภาพสูงสุด บนอุปกรณ์ประเภท Mobile จะไม่หมาะสำหรับเก็บข้อมูลในปริมาณมาก ซึ่งในความเป็นจริง การเก็บข้อมูลเยอะก็จะทำงานเยอะและหนักไปด้วย ดังนั้นแนะนำให้เก็บแค่หลัก สิบ MB เท่านั้น เพราะถ้าถึงหลัก ร้อย MB เมื่อไหร่ การทำงานของ Apps ก็จะช้าตามไปด้วย ทำให้ Apps ทำงานช้า ใช้ Resource สูง เช่นใช้ RAM / CPU และก็ Battery ของอุปกรณ์สูงไปด้วย

สำหรับ SQLite บน Windows Store Apps ถ้าพัฒนาบน Visual Studio 2012/2013 หรือ Version ที่สูงกว่านี้ สามารถทำการติดตั้ง Package ของ SQLite ผ่าน NuGet Manager Package ได้ทันที

Windows Store Apps SQLite

ให้คลิกขาที่ Reference เลือก Manage NuGet Package...








หลังจากนั้นให้เราติดตั้ง Package ขึ้นมา 2 ตัวคือ

Windows Store Apps SQLite

SQLite for Windows Runtime หรือจะติดตั้งผ่าน

http://sqlite.org/download.html


Windows Store Apps SQLite

และ sqlite-net

หลังจากที่ติดตั้ง Package ของ SQLite for Windows Runtime และ sqlite-net เรียบร้อยแล้ว เราก็จะสามารถเรียกใช้งาน SQLite ได้ทันที

การเริ่มต้น SQLite จะต้องทำการเรียกใช้ Library ของ SQLite ซะก่อน

using SQLite;

จากนั้นเราสามารถทำตามขั้นตอนต่าง ๆ ของการสร้าง Database ทั่ว ๆ ไป คือ Database -> Table -> (Insert/Update/Delete)

ตัวอย่างการสร้าง Database ซึ่งจะจัดเก็บไว้ใน Local Data
                var uri = new Uri("ms-appx:///data.db3"); //in application folder 
                var file = await StorageFile.GetFileFromApplicationUriAsync(uri); 
 
                var destinationFolder = ApplicationData.Current.LocalFolder;//local appdata dir 
 
 
                // var f = await destinationFolder.GetFileAsync("data.db3"); 
 
                await file.CopyAsync(destinationFolder); //copied application local  folder}} 


ตัวอย่างการสร้าง Table ซึ่งจะจัดเก็บไว้ใน data.db3
        public class person 
        { 
 
            [MaxLength(5), PrimaryKey] 
            
            public String name { get; set; } 
            [MaxLength(255)] 
            public String address { get; set; } 
            [MaxLength(11)] 
            public Double phone { get; set; } 
        } 

        private async void createtable(object sender, RoutedEventArgs e) 
        { 
            try 
            { 
                var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3"); 
                using (var db = new SQLite.SQLiteConnection(dbpath)) 
                { 
                    // Create the tables if they don't exist 
                    db.CreateTable<person>(); 
                    db.Commit(); 
 
                    db.Dispose(); 
                    db.Close(); 
                } 
                var line = new MessageDialog("Table Created"); 
                await line.ShowAsync(); 
            } 
            catch 
            { 
 
            } 
        } 


ตัวอย่างการ Insert ข้อมูล
    var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3"); 
    using (var db = new SQLite.SQLiteConnection(dbpath)) 
    { 
	// Create the tables if they don't exist 
	db.Insert(new person() 
	{ 

	    name = txt1.Text.ToString(), 
	    address = txt2.Text.ToString(), 
	    phone = Convert.ToDouble(txt3.Text.ToString()), 
	} 
	); 


	db.Commit(); 
	db.Dispose(); 
	db.Close(); 
	var line = new MessageDialog("Records Inserted"); 
	await line.ShowAsync(); 
    } 


ตัวอย่างการ Delete ข้อมูล
	var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3"); 
	using (var db = new SQLite.SQLiteConnection(dbpath)) 
	{ 
	    db.Delete<person>(list1.SelectedItem.ToString());//selected item 

	    var d = from x in db.Table<person>() select x; 
	    list1.Items.Clear(); 
	    foreach (var sd in d) 
	    { 
		list1.Items.Add(sd.name.ToString()); 
		//list1.Items.Add(sd.address.ToString()); 
		//list1.Items.Add(sd.phone.ToString()); 
	    } 
	    db.Dispose(); 
	    db.Close(); 
	} 


ตัวอย่างการ อ่าน ข้อมูล
	var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3"); 
	using (var db = new SQLite.SQLiteConnection(dbpath)) 
	{ 
	    var d = from x in db.Table<person>() select x; 
	    list1.Items.Clear(); 
	    foreach (var sd in d) 
	    { 
		 
		list1.Items.Add(sd.name.ToString()); 
		//list1.Items.Add(sd.address.ToString()); 
		//list1.Items.Add(sd.phone.ToString()); 
	    } 
	    db.Dispose(); 
	    db.Close(); 


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








.

   
Share


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


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


   


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

 
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 :

 
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 03
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 อัตราราคา คลิกที่นี่