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,038

HOME > .NET Framework > Forum > VB.Net + Mysql จะเขียนโค้ดติดต่อ VB.Net กับฐานข้อมูล Mysql


 

[.NET] VB.Net + Mysql จะเขียนโค้ดติดต่อ VB.Net กับฐานข้อมูล Mysql

 
Topic : 034606



โพสกระทู้ ( 292 )
บทความ ( 0 )



สถานะออฟไลน์



จะเขียนโค้ดติดต่อ VB.Net กับฐานข้อมูล Mysql
เขียนยังไงอะคับกำลังจะโปรเจคแต่ไมาเคยเรียนเลย
ตายแน่ๆ

ขอบคุณครับ



Tag : - - - -

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2009-11-19 13:48:39 By : adunafah View : 29759 Reply : 8
 

 

No. 1



โพสกระทู้ ( 1 )
บทความ ( 0 )



สถานะออฟไลน์


ต้อง download connector สำหรับ connect ไปยัง Mysql ก่อนครับ
Date : 2009-11-20 15:58:55 By : sarawut_k
 

 

No. 2



โพสกระทู้ ( 482 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ถ้าเคยเขียนกับAccessก็จะเขียนคล้ายๆกันครับเพียงแต่จะต่างกันตรงที่คำสั่งConnectครับ
Date : 2009-11-20 16:55:02 By : babyprogrammer
 

 

No. 3



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Code (VB.NET)
01.<%@ Import Namespace="System.Data"%>
02.<%@ Import Namespace="MySql.Data.MySqlClient"%>
03.<%@ Page Language="VB" %>
04.<script runat="server">
05. 
06.    Dim objConn As MySqlConnection
07.    Dim objCmd As MySqlCommand
08. 
09.    Sub Page_Load(sender As Object, e As EventArgs)
10.        Dim strConnString As String
11.        strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
12.        objConn = New MySqlConnection(strConnString)
13.        objConn.Open()
14. 
15.        BindData()
16.    End Sub
17. 
18.    Sub BindData()
19.        Dim strSQL As String
20.        strSQL = "SELECT * FROM customer"
21. 
22.        Dim dtReader As MySqlDataReader
23.        objCmd = New MySqlCommand(strSQL, objConn)
24.        dtReader = objCmd.ExecuteReader()
25.         
26.        '*** BindData to Repeater ***'
27.        myRepeater.DataSource = dtReader
28.        myRepeater.DataBind()
29. 
30.        dtReader.Close()
31.        dtReader = Nothing
32. 
33.    End Sub
34. 
35.    Sub Page_UnLoad()
36.        objConn.Close()
37.        objConn = Nothing
38.    End Sub
39. 
40.</script>
41.<html>
42.<head>
43.<title>ThaiCreate.Com ASP.NET - MySQL</title>
44.</head>
45.<body>
46.    <form id="form1" runat="server">
47.    <asp:Repeater id="myRepeater" runat="server">
48.    <HeaderTemplate>
49.        <table border="1">
50.            <tr>
51.                <th>CustomerID</th>
52.                <th>Name</th>
53.                <th>Email</th>
54.                <th>CountryCode</th>
55.                <th>Budget</th>
56.                <th>Used</th>
57.            </tr>
58.    </HeaderTemplate>
59.    <ItemTemplate>
60.        <tr>
61.            <td align="center"><asp:Label id="lblCustomerID" runat="server" Text='<%#Container.DataItem("CustomerID") %>'></asp:Label></td>
62.            <td><asp:Label id="lblName" runat="server" Text='<%#Container.DataItem("Name") %>'></asp:Label></td>
63.            <td><asp:Label id="lblEmail" runat="server" Text='<%#Container.DataItem("Email") %>'></asp:Label></td>
64.            <td align="center"><asp:Label id="lblCountryCode" runat="server" Text='<%#Container.DataItem("CountryCode") %>'></asp:Label></td>
65.            <td align="right"><asp:Label id="lblBudget" runat="server" Text='<%#Container.DataItem("Budget") %>'></asp:Label></td>
66.            <td align="right"><asp:Label id="lblUsed" runat="server" Text='<%#Container.DataItem("Used") %>'></asp:Label></td>
67.        </tr>        
68.    </ItemTemplate>
69.    <AlternatingItemTemplate>
70.        <tr bgcolor="#e8e8e8">
71.            <td align="center"><asp:Label id="lblCustomerID" runat="server" Text='<%#Container.DataItem("CustomerID") %>'></asp:Label></td>
72.            <td><asp:Label id="lblName" runat="server" Text='<%#Container.DataItem("Name") %>'></asp:Label></td>
73.            <td><asp:Label id="lblEmail" runat="server" Text='<%#Container.DataItem("Email") %>'></asp:Label></td>
74.            <td align="center"><asp:Label id="lblCountryCode" runat="server" Text='<%#Container.DataItem("CountryCode") %>'></asp:Label></td>
75.            <td align="right"><asp:Label id="lblBudget" runat="server" Text='<%#Container.DataItem("Budget") %>'></asp:Label></td>
76.            <td align="right"><asp:Label id="lblUsed" runat="server" Text='<%#Container.DataItem("Used") %>'></asp:Label></td>
77.        </tr>        
78.    </AlternatingItemTemplate>
79.    </asp:Repeater>
80.    </form>
81.</body>
82.</html>




ASP.NET MySQL (MySql.Data.MySqlClient)
Date : 2009-11-20 16:57:14 By : webmaster
 

 

No. 4



โพสกระทู้ ( 292 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณครับ
Date : 2009-12-03 11:32:50 By : adunafah
 

 

No. 5

Guest


สวัสดีค่ะ

อยากทราบว่า การเขียน โค้ด ดาต้ากริด จาก สโตร ใน SQL เขียน แบบไหนค่ะ

พอดีว่าไม่ได้ใช้ แบบ OLE อ่าค่ะ แคต่ใช้ ผ่านสโตร
Date : 2009-12-07 18:53:06 By : ATC
 

 

No. 6



โพสกระทู้ ( 538 )
บทความ ( 3 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ลองดูแบบนี้ครับสำหรับคนใช้การดึงข้อมูลแบบ StoreProcedure ครับ วิธีนี้ดีครับเข้า concept OOP แบ่งออกเป็นส่วนๆเอามาประกอบกันครับ

ดังนั้นผมก็จะนำเสนอการดึงข้อมูลออกมาในแบบ OOP เหมือนกันนะครับ
เริ่มเลยก็ต้องมี clase ของ table นั้นๆ ก่อนครับ (ประยุกต์ใช้ได้ครับ ไม่ต้องเป็น table ก็ได้ เป็น view หรือแค่ column ที่เราอยากจะดึงมาก็ได้ครับ เพียงแต่ต้องทำ class รองรับไว้ก่อน)

Code : class CategoryDetails (C#)
01.using System;
02.using System.Data;
03.using System.Configuration;
04.using System.Web;
05.using System.Web.Security;
06.using System.Web.UI;
07.using System.Web.UI.WebControls;
08.using System.Web.UI.WebControls.WebParts;
09.using System.Web.UI.HtmlControls;
10. 
11.   public class CategoryDetails
12.   {
13.      public CategoryDetails() { }
14. 
15.      public CategoryDetails(int id, DateTime addedDate, string addedBy, string title, string description)
16.      {
17.         this.ID = id;
18.         this.AddedDate = addedDate;
19.         this.AddedBy = addedBy;
20.         this.Title = title;
21.         this.Description = description;
22.      }
23. 
24.      private int _id = 0;
25.       public int ID
26.       {
27.           get { return _id;}
28.           set { _id = value;}
29.       }
30. 
31.      private DateTime _addedDate = DateTime.Now;
32.      public DateTime AddedDate
33.      {
34.         get { return _addedDate; }
35.         set { _addedDate = value; }
36.      }
37. 
38.      private string _addedBy = "";
39.      public string AddedBy
40.      {
41.         get { return _addedBy; }
42.         set { _addedBy = value; }
43.      }
44. 
45.      private string  _title = "";
46.      public string  Title
47.      {
48.         get { return _title; }
49.         set { _title = value; }
50.      }
51. 
52.      private string _description = "";
53.      public string Description
54.      {
55.         get { return _description; }
56.         set { _description = value; }
57.      }
58.   }


Class สำหรับการเรียกใช้งาน
Code : class Categorys_DataProvided (C#)
01.using System;
02.using System.Data;
03.using System.Configuration;
04.using System.Web;
05.using System.Web.Security;
06.using System.Web.UI;
07.using System.Web.UI.WebControls;
08.using System.Web.UI.WebControls.WebParts;
09.using System.Web.UI.HtmlControls;
10.using System.Collections.Generic;
11.public abstract class Categorys_DataProvided :
12.   {
13.      public abstract List<CategoryDetails> GetCategories();
14.      public abstract CategoryDetails GetCategoryByID(int categoryID);
15.      public abstract bool DeleteCategory(int categoryID);
16.      public abstract bool UpdateCategory(CategoryDetails category);
17.      public abstract int InsertCategory(CategoryDetails category);
18. 
19.      สำหรับรับค่าข้อมูลเพียง record เดียว
20.      protected virtual CategoryDetails GetCategoryFromReader(IDataReader reader)
21.      {
22.         return new CategoryDetails(
23.            (int)reader["CategoryID"],
24.            (DateTime)reader["AddedDate"],
25.            reader["AddedBy"].ToString(),
26.            reader["Title"].ToString(),
27.            reader["Description"].ToString());
28.      }
29. 
30.      /// <summary>
31.      /// Returns a collection of CategoryDetails objects with the data read from the input DataReader
32.      /// </summary>
33.      protected virtual List<CategoryDetails> GetCategoryCollectionFromReader(IDataReader reader) <- สำหรับรับค่าหลายๆ records
34.      {
35.         List<CategoryDetails> categories = new List<CategoryDetails>();
36.         while (reader.Read())
37.            categories.Add(GetCategoryFromReader(reader));
38.         return categories;
39.      }
40.   }


ส่วนติดต่อกับฐานข้อมูล ก็จะเอา function, procedure ที่ประกาศไว้ใน class ข้างบนมาเขียนขยายลงไปว่าเอาอะไรมาจากไหน
Code : class SQLDataCategorys (C#)
01.using System;
02.using System.Data;
03.using System.Configuration;
04.using System.Web;
05.using System.Web.Security;
06.using System.Web.UI;
07.using System.Web.UI.WebControls;
08.using System.Web.UI.WebControls.WebParts;
09.using System.Web.UI.HtmlControls;
10.using System.Data.SqlClient;
11.using System.Collections.Generic;
12.using System.Web.Caching;
13.   public class SQLDataCategorys : Categorys_DataProvided
14.   {
15.      public override List<CategoryDetails> GetCategories()
16.      {
17.         using (SqlConnection cn = new SqlConnection(this.ConnectionString))
18.         {
19.            SqlCommand cmd = new SqlCommand("my_GetCategories", cn);
20.            cmd.CommandType = CommandType.StoredProcedure;
21.            cn.Open();           
22.            return GetCategoryCollectionFromReader(ExecuteReader(cmd));
23.         }
24.      }
25. 
26.      public override CategoryDetails GetCategoryByID(int categoryID)
27.      {
28.         using (SqlConnection cn = new SqlConnection(this.ConnectionString))
29.         {
30.            SqlCommand cmd = new SqlCommand("my_GetCategoryByID", cn);
31.            cmd.CommandType = CommandType.StoredProcedure;
32.            cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = categoryID;
33.            cn.Open();
34.            IDataReader reader = ExecuteReader(cmd, CommandBehavior.SingleRow);
35.            if (reader.Read())
36.               return GetCategoryFromReader(reader);
37.            else
38.               return null;
39.         }
40.      }
41.   }

นำเสนอแค่ 2 นะครับเพราะเห็นว่าอยากได้แค่กรณีดึงข้อมูล ส่วนที่ไม่ได้เอาให้ดูจะเป็นพวก insert, update, delete ก็ดูจากที่นำเสนอไปก็น่าจะมีแนวทางการใช้งานแล้วมั๊งเนาะ

ส่วนนี้จะเป็น Store Procedure ที่ใช้กับ class ข้างบนครับ ลองเอาไปดูเป็นแนวทางครับ
** Store Procedure นี้ใช้กับ SQLServer2005 Express ครับ

Code
USE [MyDBSample] ---> StoreProc 1 : my_GetCategorys
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[my_GetCategories]
AS
SET NOCOUNT ON

SELECT CategoryID, AddedDate, AddedBy, Title, Description
FROM dbo.MyCategories
ORDER BY AddedBy DESC, Title ASC
<-------------------------------------------------------------------------------------------->
USE [MyDBSample] ---> StoreProc 2 : my_GetCategoryByID
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[my_GetCategoryByID]
(
@CategoryID int
)
AS
SET NOCOUNT ON

SELECT CategoryID, AddedDate, AddedBy, Title, Description
FROM MyCategories
WHERE CategoryID = @CategoryID


หวังว่าจะพอช่วยได้นะครับ
Date : 2009-12-08 00:25:13 By : salapao_codeman
 

 

No. 7



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Code (VB.NET)
1.Dim objConn As New MySqlConnection
2.Dim objCmd As New MySqlCommand
3.Dim strConnString, strSQL As String
4. 
5.strConnString = "Server=localhost;User Id=root; Password=root; Database=database; Pooling=false"
6.objConn.ConnectionString = strConnString
7.objConn.Open()


Go to : VB.NET/C# MySQL (ADO.NET) พื้นฐานการ เขียนโปรแกรม ADO.NET เชื่อมต่อกับฐานข้อมูล MySQL
Date : 2012-03-23 22:10:10 By : webmaster
 

 

No. 8



โพสกระทู้ ( 139 )
บทความ ( 0 )



สถานะออฟไลน์


โหลด MyODBC http://dev.mysql.com/downloads/connector/odbc/มาก่อนนะครับ หลังจากนั้นก็ทำตามขั้นตอนในเว็บนี้ได้เลย
Date : 2012-03-26 13:32:54 By : Jumyut
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : VB.Net + Mysql จะเขียนโค้ดติดต่อ VB.Net กับฐานข้อมูล Mysql
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่