 |
|
|
 |
 |
|
ต้อง download connector สำหรับ connect ไปยัง Mysql ก่อนครับ
|
 |
 |
 |
 |
Date :
2009-11-20 15:58:55 |
By :
sarawut_k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเคยเขียนกับAccessก็จะเขียนคล้ายๆกันครับเพียงแต่จะต่างกันตรงที่คำสั่งConnectครับ
|
 |
 |
 |
 |
Date :
2009-11-20 16:55:02 |
By :
babyprogrammer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="MySql.Data.MySqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As MySqlConnection
Dim objCmd As MySqlCommand
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
objConn = New MySqlConnection(strConnString)
objConn.Open()
BindData()
End Sub
Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM customer"
Dim dtReader As MySqlDataReader
objCmd = New MySqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to Repeater ***'
myRepeater.DataSource = dtReader
myRepeater.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - MySQL</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>CustomerID</th>
<th>Name</th>
<th>Email</th>
<th>CountryCode</th>
<th>Budget</th>
<th>Used</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><asp:Label id="lblCustomerID" runat="server" Text='<%#Container.DataItem("CustomerID") %>'></asp:Label></td>
<td><asp:Label id="lblName" runat="server" Text='<%#Container.DataItem("Name") %>'></asp:Label></td>
<td><asp:Label id="lblEmail" runat="server" Text='<%#Container.DataItem("Email") %>'></asp:Label></td>
<td align="center"><asp:Label id="lblCountryCode" runat="server" Text='<%#Container.DataItem("CountryCode") %>'></asp:Label></td>
<td align="right"><asp:Label id="lblBudget" runat="server" Text='<%#Container.DataItem("Budget") %>'></asp:Label></td>
<td align="right"><asp:Label id="lblUsed" runat="server" Text='<%#Container.DataItem("Used") %>'></asp:Label></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td align="center"><asp:Label id="lblCustomerID" runat="server" Text='<%#Container.DataItem("CustomerID") %>'></asp:Label></td>
<td><asp:Label id="lblName" runat="server" Text='<%#Container.DataItem("Name") %>'></asp:Label></td>
<td><asp:Label id="lblEmail" runat="server" Text='<%#Container.DataItem("Email") %>'></asp:Label></td>
<td align="center"><asp:Label id="lblCountryCode" runat="server" Text='<%#Container.DataItem("CountryCode") %>'></asp:Label></td>
<td align="right"><asp:Label id="lblBudget" runat="server" Text='<%#Container.DataItem("Budget") %>'></asp:Label></td>
<td align="right"><asp:Label id="lblUsed" runat="server" Text='<%#Container.DataItem("Used") %>'></asp:Label></td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
ASP.NET MySQL (MySql.Data.MySqlClient)
|
 |
 |
 |
 |
Date :
2009-11-20 16:57:14 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2009-12-03 11:32:50 |
By :
adunafah |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สวัสดีค่ะ
อยากทราบว่า การเขียน โค้ด ดาต้ากริด จาก สโตร ใน SQL เขียน แบบไหนค่ะ
พอดีว่าไม่ได้ใช้ แบบ OLE อ่าค่ะ แคต่ใช้ ผ่านสโตร
|
 |
 |
 |
 |
Date :
2009-12-07 18:53:06 |
By :
ATC |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูแบบนี้ครับสำหรับคนใช้การดึงข้อมูลแบบ StoreProcedure ครับ วิธีนี้ดีครับเข้า concept OOP แบ่งออกเป็นส่วนๆเอามาประกอบกันครับ
ดังนั้นผมก็จะนำเสนอการดึงข้อมูลออกมาในแบบ OOP เหมือนกันนะครับ
เริ่มเลยก็ต้องมี clase ของ table นั้นๆ ก่อนครับ (ประยุกต์ใช้ได้ครับ ไม่ต้องเป็น table ก็ได้ เป็น view หรือแค่ column ที่เราอยากจะดึงมาก็ได้ครับ เพียงแต่ต้องทำ class รองรับไว้ก่อน)
Code : class CategoryDetails (C#)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class CategoryDetails
{
public CategoryDetails() { }
public CategoryDetails(int id, DateTime addedDate, string addedBy, string title, string description)
{
this.ID = id;
this.AddedDate = addedDate;
this.AddedBy = addedBy;
this.Title = title;
this.Description = description;
}
private int _id = 0;
public int ID
{
get { return _id;}
set { _id = value;}
}
private DateTime _addedDate = DateTime.Now;
public DateTime AddedDate
{
get { return _addedDate; }
set { _addedDate = value; }
}
private string _addedBy = "";
public string AddedBy
{
get { return _addedBy; }
set { _addedBy = value; }
}
private string _title = "";
public string Title
{
get { return _title; }
set { _title = value; }
}
private string _description = "";
public string Description
{
get { return _description; }
set { _description = value; }
}
}
Class สำหรับการเรียกใช้งาน
Code : class Categorys_DataProvided (C#)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public abstract class Categorys_DataProvided :
{
public abstract List<CategoryDetails> GetCategories();
public abstract CategoryDetails GetCategoryByID(int categoryID);
public abstract bool DeleteCategory(int categoryID);
public abstract bool UpdateCategory(CategoryDetails category);
public abstract int InsertCategory(CategoryDetails category);
สำหรับรับค่าข้อมูลเพียง record เดียว
protected virtual CategoryDetails GetCategoryFromReader(IDataReader reader)
{
return new CategoryDetails(
(int)reader["CategoryID"],
(DateTime)reader["AddedDate"],
reader["AddedBy"].ToString(),
reader["Title"].ToString(),
reader["Description"].ToString());
}
/// <summary>
/// Returns a collection of CategoryDetails objects with the data read from the input DataReader
/// </summary>
protected virtual List<CategoryDetails> GetCategoryCollectionFromReader(IDataReader reader) <- สำหรับรับค่าหลายๆ records
{
List<CategoryDetails> categories = new List<CategoryDetails>();
while (reader.Read())
categories.Add(GetCategoryFromReader(reader));
return categories;
}
}
ส่วนติดต่อกับฐานข้อมูล ก็จะเอา function, procedure ที่ประกาศไว้ใน class ข้างบนมาเขียนขยายลงไปว่าเอาอะไรมาจากไหน
Code : class SQLDataCategorys (C#)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Caching;
public class SQLDataCategorys : Categorys_DataProvided
{
public override List<CategoryDetails> GetCategories()
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("my_GetCategories", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
return GetCategoryCollectionFromReader(ExecuteReader(cmd));
}
}
public override CategoryDetails GetCategoryByID(int categoryID)
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("my_GetCategoryByID", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = categoryID;
cn.Open();
IDataReader reader = ExecuteReader(cmd, CommandBehavior.SingleRow);
if (reader.Read())
return GetCategoryFromReader(reader);
else
return null;
}
}
}
นำเสนอแค่ 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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|