ASP MySQL Connect to Database |
ASP MySQL Connect to Database ในการเขียน ASP ติดต่อฐานข้อมูล MySQL นั้นสามารถทำได้โดยการติดต่อผ่าน Driver ชื่อ MyODBC ซึ่งดาวน์โหลดได้จาก www.mysql.com ในที่นี้ผมใช้เวอร์ชั่น 5.1 เป็นเวอร์ชั่นล่าสุด
ดาวน์โหลดจาก URL นี้ได้โดยตรงครับ
Download MyODBC 5.1 Driver

Install MyODBC

Install MyODBC

Install MyODBC
Install MyODBC
MySQL ODBC 5.1 Driver Connection Syntax
<%
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=host;UID=user; " & _
"pwd=password;database=database;option=16384;"
%>
ODBC Connection Syntax
<%
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DSNname (ODBC)","user" , "password"
%>
การ Set DSN (ODBC) ฐานข้อมูล MySQL
https://www.thaicreate.com/tutorial/mysql-dsn-odbc.html
ตัวอย่างการเขียนติดต่อ ASP กับ MySQL
AspMySQLConnect.asp
<% Option Explicit %>
<html>
<head>
<title>ThaiCreate.Com ASP & MySQL Tutorial</title>
</head>
<body>
<%
On Error Resume Next '*** Not Show ASP Error ***'
Dim Conn
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost;UID=root; " & _
"pwd=root;database=mydatabase;option=16384;"
If Err.Number <> 0 Then
Response.write("Database cannot connect Error = " & Err.Description)
Else
Response.write("Database connected.")
End If
Conn.Close()
Set Conn = Nothing
%>
</body>
</html>
MySQL Database ที่ใช้ในบทเรียน
CREATE TABLE `audit` (
`AuditID` int(4) NOT NULL,
`CustomerID` varchar(4) NOT NULL,
`Date` date NOT NULL,
`Used` double NOT NULL,
PRIMARY KEY (`AuditID`)
) ENGINE=MyISAM;
INSERT INTO `audit` VALUES (1, 'C001', '2008-08-01', 100000);
INSERT INTO `audit` VALUES (2, 'C001', '2008-08-05', 200000);
INSERT INTO `audit` VALUES (3, 'C001', '2008-08-10', 300000);
INSERT INTO `audit` VALUES (4, 'C002', '2008-08-02', 400000);
INSERT INTO `audit` VALUES (5, 'C002', '2008-08-07', 100000);
INSERT INTO `audit` VALUES (6, 'C002', '2008-08-15', 300000);
INSERT INTO `audit` VALUES (7, 'C003', '2008-08-20', 400000);
INSERT INTO `audit` VALUES (8, 'C003', '2008-08-25', 200000);
INSERT INTO `audit` VALUES (9, 'C004', '2008-07-04', 100000);
INSERT INTO `audit` VALUES (10, 'C005', '2008-07-04', 200000);
CREATE TABLE `country` (
`CountryCode` varchar(2) NOT NULL,
`CountryName` varchar(30) NOT NULL,
PRIMARY KEY (`CountryCode`)
) ENGINE=MyISAM;
INSERT INTO `country` VALUES ('TH', 'Thailand ');
INSERT INTO `country` VALUES ('EN', 'English');
INSERT INTO `country` VALUES ('US', 'United states');
CREATE TABLE `customer` (
`CustomerID` varchar(4) NOT NULL,
`Name` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`CountryCode` varchar(2) NOT NULL,
`Budget` double NOT NULL,
`Used` double NOT NULL,
PRIMARY KEY (`CustomerID`)
) ENGINE=MyISAM;
INSERT INTO `customer` VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO `customer` VALUES ('C002', 'John Smith', '[email protected]', 'EN', 2000000, 800000);
INSERT INTO `customer` VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO `customer` VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);

|