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 > ASP > ASP SQL Server > ASP SQL Server Used Function Database Query



Clound SSD Virtual Server

ASP SQL Server Used Function Database Query

ASP SQL Server Used Function Database Query ตัวอย่างนี้ผมได้ยกตัวอย่างการเขียน ASP ติดต่อกับ SQL Server โดยใช้ Function เข้ามาช่วยจัดการในด้านการเพิ่ม/ลบ/แก้ไข เพื่อความสะดวกและง่ายต่อการแก้ไขหรือพัฒนาโปรแกรมครับ


ตัวอย่าง

AspSQLServerFunctionDatabase.asp

<%	
	'*** By ThaiCreate.Com ***'
	'*** By @W_IN ***'

	Const strHost = "localhost"
	Const strUserID = "sa"
	Const strPassword = ""
	Const strDatabase = "mydatabase"
	
	'*** Connection String ***'
	Function ConnectionString()
		ConnectionString = "Driver={SQL Server};Server="&strHost&"; " & _
		"Database="&strDatabase&";UID="&strUserID&";PWD="&strPassword&";"
	End Function
	
	'*** Excute & objRec (Dynamic RecordSet) ***'
	Function fncExecuteSQLDynamic(strSQL,objRec)	
		Dim Conn
		Set Conn = Server.Createobject("ADODB.Connection")
		Conn.Open ConnectionString,strUserID,strPassword	
		Set objRec = Server.CreateObject("ADODB.Recordset")		
		objRec.Open strSQL,Conn,1,3
		Open.Close()
		Set Open = Nothing
	End Function
	
	'*** Excute & objRec (Static RecordSet) ***'
	Function fncExecuteSQLStatic(strSQL,objRec)	
		Dim Conn
		Set Conn = Server.Createobject("ADODB.Connection")
		Conn.Open ConnectionString,strUserID,strPassword	
		Set objRec = Conn.Execute(strSQL)
		Open.Close()
		Set Open = Nothing
	End Function
	
	'*** Excute & Return (True/False) ***'
	Function fncExecuteSQL(strSQL)  
		On Error Resume Next
		Dim Conn,objExec
		Set Conn = Server.Createobject("ADODB.Connection")
		Conn.Open ConnectionString,strUserID,strPassword	
		Set objExec = Conn.Execute(strSQL)
		If Err.Number = 0 Then
			fncExecuteSQL = True
		Else
			fncExecuteSQL = False
		End If
		Set objExec = Nothing
		Open.Close()
		Set Open = Nothing		
	End Function

	'*** Function Insert Record ***'
	Function fncInsertRecord(strTable,strField,strValue)
		On Error Resume Next
		Dim strSQL		
		strSQL = "INSERT INTO "&strTable&" ("&strField&") VALUES ("&strValue&")"
		fncInsertRecord = fncExecuteSQL(strSQL)
	End Function

	'*** Function List Record ***'
	Function fncListRecord(objRec,strTable,strCondition)
		On Error Resume Next
		Dim strSQL
		strSQL = "SELECT * FROM  "&strTable&" WHERE "&strCondition
		Call fncExecuteSQLDynamic(strSQL,objRec)
	End Function

	'*** Function Select Record ***'
	Function fncSelectRecord(objRec,strTable,strCondition)
		On Error Resume Next
		Dim strSQL
		strSQL = "SELECT * FROM  "&strTable&" WHERE "&strCondition
		Call fncExecuteSQLStatic(strSQL,objRec)
	End Function

	'*** Function Update Record ***'
	Function fncUpdateRecord(strTable,strCommand,strCondition)
		On Error Resume Next
		Dim strSQL
		strSQL = "UPDATE "&strTable&" SET "&strCommand&" WHERE "&strCondition
		fncUpdateRecord = fncExecuteSQL(strSQL)
	End Function

	'*** Function Delete Record ***'
	Function fncDeleteRecord(strTable,strCondition)
		On Error Resume Next
		Dim strSQL
		strSQL = "DELETE FROM "&strTable&" WHERE "&strCondition
		fncDeleteRecord = fncExecuteSQL(strSQL)
	End Function
%>




AspSQLServerUsedFunctionDatabase.asp

<% Option Explicit %>
<html>
<head>
<title>ThaiCreate.Com ASP & SQL Server Tutorial</title>
</head>
<body>
<!--#include file="ASPSQLServerFunctionDatabase.asp"-->
<%
Dim strTable,strField,strValue


'**** Call to function insert record ****'
Dim objInsert
strTable = "customer"
strField = "CustomerID,Name,Email,CountryCode,Budget,Used"
strValue = " 'C005','Weerachai Nukitram','[email protected]','TH','2000000','0' "
objInsert = fncInsertRecord(strTable,strField,strValue)
If objInsert = True Then
	Response.write("Record inserted.<br>")
Else
	Response.write("Record already exist.<br>")
End IF
Response.write("<br>===========================<br>")

'**** Call to function list record ****'
Dim objList,strCondition
strTable = "customer"
strCondition = " 1=1 "
Call fncListRecord(objList,strTable,strCondition)
If objList.EOF Then
	Response.write("Record not found<br>")
Else
	Response.write("Customer List.<br>")
	objList.MoveFirst
	While Not objList.EOF
		Response.write(""&objList.Fields("CustomerID").Value&"")
		Response.write(", "&objList.Fields("Name").Value&"")
		Response.write(", "&objList.Fields("Email").Value&"")
		Response.write(", "&objList.Fields("CountryCode").Value&"")
		Response.write(", "&objList.Fields("Budget").Value&"")
		Response.write(", "&objList.Fields("Used").Value&"<br>")
	objList.MoveNext
	Wend
End If
objList.Close()
Set objList = Nothing
Response.write("<br>===========================<br>")

'**** Call to function select record ****'
Dim objSelect
strTable = "customer"
strCondition = " CustomerID = 'C005' "
Call fncSelectRecord(objSelect,strTable,strCondition)
If objSelect.EOF Then
	Response.write("Record not found<br>")
Else
	Response.write("Customer Detail.<br>")
	Response.write("CustomerID = "&objSelect.Fields("CustomerID").Value&"<br>")
	Response.write("Name = "&objSelect.Fields("Name").Value&"<br>")
	Response.write("Email = "&objSelect.Fields("Email").Value&"<br>")
	Response.write("CountryCode = "&objSelect.Fields("CountryCode").Value&"<br>")
	Response.write("Budget = "&objSelect.Fields("Budget").Value&"<br>")
	Response.write("Used = "&objSelect.Fields("Used").Value&"<br>")
End If
objSelect.Close()
Set objSelect = Nothing
Response.write("<br>===========================<br>")


'**** Call to function update record ****'
Dim strCommand,objUpdate
strTable = "customer"
strCommand = " BUDGET = '4000000' "
strCondition = " CustomerID = 'C005' "
objUpdate = fncUpdateRecord(strTable,strCommand,strCondition)
If objUpdate = True Then
	Response.write("Record updated.<br>")
Else
	Response.write("Error update record.<br>")
End IF
Response.write("<br>===========================<br>")


'**** Call to function delete record ****'
Dim objDelete
strTable = "customer"
strCondition = " CustomerID = 'C005' "
objDelete = fncDeleteRecord(strTable,strCondition)
If objDelete = True Then
	Response.write("Record deleted.<br>")
Else
	Response.write("Record not delete.<br>")
End IF

%>
</body>
</html>


Screenshot

ASP & SQL Server






   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-08-10 16:28:50 / 2008-09-14 14:54:33
  Download : Download  ASP SQL Server Used Function Database Query
 Sponsored Links / Related

 
ASP SQL Server Connect to Database
Rating :

 
ASP SQL Server List Table Properties
Rating :

 
ASP SQL Server List Record
Rating :

 
ASP SQL Server List Record (ODBC)
Rating :

 
ASP SQL Server Random Record
Rating :

 
ASP SQL Server List Record Paging/Pagination
Rating :

 
ASP SQL Server Search Record
Rating :

 
ASP SQL Server Multiple Column
Rating :

 
ASP SQL Server Multiple Column and Paging/Pagination
Rating :

 
ASP SQL Server Search Record Paging/Pagination
Rating :

 
ASP SQL Server Add/Insert Record
Rating :

 
ASP SQL Server Check Already Exists Add/Insert Record
Rating :

 
ASP SQL Server Transaction (BeginTrans,CommitTrans,RollbackTrans)
Rating :

 
ASP SQL Server Edit/Update Record
Rating :

 
ASP SQL Server Delete Record
Rating :

 
ASP SQL Server Multiple Checkbox Delete Record
Rating :

 
ASP SQL Server Used Used Include Function
Rating :

 
ASP SQL Server Database Class
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 02
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 อัตราราคา คลิกที่นี่