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 Azure > Windows Azure (Web Sites) and ASP.Net > ตอนที่ 2 : ASP.Net ติดต่อกับฐานข้อมูล SQL Azure/SQL Database บน Azure



Clound SSD Virtual Server

ตอนที่ 2 : ASP.Net ติดต่อกับฐานข้อมูล SQL Azure/SQL Database บน Azure

ตอนที่ 2 : ASP.Net ติดต่อกับฐานข้อมูล SQL Azure/SQL Database บน Azure ในการเขียน ASP.Net และทำงานบน Web Site ของ Windows Azure เราสามารถที่จะทำการสร้าง SQL Azure หรือ SQL Database ขึ้นมาใช้งานได้ทันที ด้วยการเลือก Create SQL Database พร้อมกับ Web Site โดยเมื่อหลังจากที่สร้าง Web Site พร้อมกับ SQL Database แล้ว จะมีการสร้าง Service ของ SQL Database มาให้อัตโนมัติ และเราสามารถที่จะเขียน ASP.Net เพื่อติดต่อกับ Database ผ่าน Connection String ที่กำหนดขึ้น หรือจะเขียน Application อื่น ๆ ที่อยู่คนล่ะแห่งเพื่อเรียกใช้ SQL Database ก็ได้เช่นเดียวกัน โดยใน SQL Database และสามารถที่จะกำหนด Allow ของ IP ที่จะทำการเชื่อมต่อ ในการที่จะกำหนดความปลอดภัยของ Database ได้อีกด้วย

ASP.Net Web Site SQL Server Database

เริ่มต้นด้วยการสร้าง Web Site ด้วยการคลิกที่ NEW -> COMPUTE -> WEB SITE -> CUSTOM CREATE

ASP.Net Web Site SQL Server Database

กรอก URL ของ Web Site พร้อมกับคลิกเลือก Create a SQL Database

ASP.Net Web Site SQL Server Database

กำหนดชื่อ Database / User / Password และไปขึ้นตอนถัดไป

ASP.Net Web Site SQL Server Database

หลังจากที่ Create Web Site เรียบร้อย เราก็จะได้ Web Site ดังรูป

ASP.Net Web Site SQL Server Database

และเมื่อคลิกที่ Service ของ SQL DATABASE ก็จะเห็นรายการของ SQL Database แสดงขึ้นมา 1 รายการ








ASP.Net Web Site SQL Server Database

คลิกที่หน้า Dashboard ของ SQL Database แสดงรายละเอียดต่าง ๆ ของ SQL เช่น SERVER NAME ของ Database และจะเห็นเมนูว่า MANAGE URL (ใช้จัดการกับ SQL Server ผ่านเว็บ)

การจัดการกับ SQL Database ในการจัดการกับ SQL Database สามารถทำได้ 2 วิธีคือ ผ่าน URL และผ่าน Tools ของ SQL Server Management Studio

ASP.Net Web Site SQL Server Database

กรณีที่ MANAGE ผ่าน URL ให้กรอก Username และ Password ที่ได้จากขั้นตอนการ Create

ASP.Net Web Site SQL Server Database

จะเห็นว่าตอนนี้มี Error แจ้งว่า

Firewall check failed. Cannot open server 'kijec06v2w' requested by the login. Client with IP address '14.207.13.132' is not allowed to access the server. To enable access, use the SQL Azure Portal or run sp_set_firewall_rule on the master database to create a firewall rule for this IP address or address range. It may take up to five minutes for this change to take effect.


นั้นหมายถึงว่า IP ที่เราทำการเชื่อมต่อเพื่อไปจัดการกับ SQL Database นั้นไม่ได้มีการ Allow ไว้

ASP.Net Web Site SQL Server Database

เมื่อทำการ Connect ผ่าน SQL Server Management Studio จากเครื่อง Client ของเรา ก็จะได้ผลลัพธ์เช่นเดียวกัน

ASP.Net Web Site SQL Server Database

แสดง Error ว่ายังไม่มีการ Allow หมายเลข IP ที่จะขอเชื่อมต่อ

ASP.Net Web Site SQL Server Database

ให้กลับมาที่หน้า Dashboard ของ SQL Database คลิกที่ Manage allow ip addresses

ASP.Net Web Site SQL Server Database

จะเห็นว่ามีหมายเลข IP ของเราแสดงเป็นค่า Default (ในกรณี IP แบบ Dynamic จะต้องเพิ่มทุกครั้ง)

ASP.Net Web Site SQL Server Database

ให้เพิ่มเข้ามาในรายการ Allow IP

ASP.Net Web Site SQL Server Database

จากนั้นคลิกที่ SAVE

ASP.Net Web Site SQL Server Database

ทดสอบการ Manage ผ่าน URL อีกครั้ง

ASP.Net Web Site SQL Server Database

สามารถเข้าสู่หน้าจอการ Manage ของ SQL Database ผ่าน URL ได้แล้ว

ASP.Net Web Site SQL Server Database

ทดสอบการเชื่อมต่อ SQL Server Management Studio อีกครั้ง

ASP.Net Web Site SQL Server Database

เข้าสู่หน้าจอ SQL Server Management Studio สำหรับการจัด Database ของ SQL Database บน Windows Azure ซึ่งเราสามารถเพิ่มแก้ไขตารางต่าง ๆ จากเครื่อง Client ของเราได้ทันที

ASP.Net Web Site SQL Server Database

ทดสอบการสร้าง ตารางและข้อมูลง่าย ๆ ด้วยการสร้างผ่านการ Run ผ่าน Query

CREATE TABLE [dbo].[customer](
	[CustomerID] [varchar](4) NOT NULL,
	[Name] [varchar](50) NULL,
	[Email] [varchar](50) NULL,
	[CountryCode] [varchar](2) NULL,
	[Budget] [float] NULL,
	[Used] [float] NULL,
 CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED 
(
	[CustomerID] ASC
)
);

INSERT INTO [customer] VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO [customer] VALUES ('C002', 'John  Smith', '[email protected]', 'UK', 2000000, 800000);
INSERT INTO [customer] VALUES ('C003', 'Jame Bond', '[email protected]', 'US', 3000000, 600000);
INSERT INTO [customer] VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);


ASP.Net Web Site SQL Server Database

หลังจากสร้างเสร็จแล้วให้ Refresh ซะ 1 ครั้ง จากนั้นก็จะได้ Table ชื่อว่า customer ดังรูป

ASP.Net Web Site SQL Server Database

ข้อมูลใน Table ที่ชื่อว่า customer ซึ่งเราจะใช้ ASP.Net ในการที่จะดึงข้อมูลเหล่านี้มาแสดง

ASP.Net Web Site SQL Server Database

เราสามารถที่จะดู Connection String ได้จากเมนู Dashboard และ Show connection sring

ASP.Net Web Site SQL Server Database

เป็น Connection String ในรูปแบบต่าง ๆ อย่าลืมว่าในกรณีที่ Client เรียกใช้จากแหล่งอื่น ๆ อย่าลืมกำหนด Allow IP ด้วย

ASP.Net Web Site SQL Server Database

กลับมาหน้า Dashboard ของ Web Site ก็จะมีข้อมูลอื่น ๆ เช่นกัน เช่นข้อมูลเกี่ยวกับการ Upload ผ่าน FTP และ Git








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


ทดสอบการสร้าง ASP.NET แบบง่าย ๆ เพื่อติดต่อกับ SQL Database

Connection String
Server=kijec06v2w.database.windows.net;Uid=thaicreatedb;PASSWORD=qwer123*;database=thaicreate-sql-server;Max Pool Size=400;Connect Timeout=600;


default.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">

	Dim objConn As SqlConnection
	Dim objCmd As SqlCommand

    Sub Page_Load(sender As Object, e As EventArgs)
		Dim strConnString As String
		strConnString = "Server=kijec06v2w.database.windows.net;Uid=thaicreatedb;PASSWORD=qwer123*;database=thaicreate-sql-server;Max Pool Size=400;Connect Timeout=600;"
		objConn = New SqlConnection(strConnString)
		objConn.Open()

		BindData()
    End Sub

	Sub BindData()
		Dim strSQL As String
		strSQL = "SELECT * FROM customer"

		Dim dtReader As SqlDataReader
		objCmd = New SqlCommand(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</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 Web Site SQL Server Database

จากนั้นให้ทำการ Upload ด้วย FTP ไปไว้บน Web Site

ASP.Net Web Site SQL Server Database

ทดสอบเรียกผ่าน Web Site ของ Windows Azure ก็จะได้ผลลัพธ์ดังรูป

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


   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-03-25 10:04:01 / 2017-03-24 10:22:47
  Download : No files
 Sponsored Links / Related

 
ตอนที่ 1 : สร้าง Project ASP.Net รันบน Web Sites ง่าย ๆ บน Windows Azure
Rating :

 
ตอนที่ 3 : ASP.Net เพื่อติดต่อกับฐานข้อมูล MySQL Database บน Azure
Rating :

 
ตอนที่ 4 : จัดการ ASP.Net Web Site บน Windows Azure ด้วย Web Matrix
Rating :

 
ตอนที่ 5 : ASP.Net บน Visual Studio และการ Deploy ผ่าน Visual Studio
Rating :

 
ตอนที่ 6 : ติดตั้ง Orchard CMS (ASP.Net MVC) จาก Gallery ของ Web Site
Rating :

 
ตอนที่ 7 : แหล่งความรู้การใช้งาน .Net Framework บน Windows Azure
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 00
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 อัตราราคา คลิกที่นี่