Thứ Tư, 26 tháng 8, 2015

Nhập dữ liệu vào Database không có lỗi Data Type

Câu hỏi :
Nhập dữ liệu vào Database không có lỗi Data Type

Trả lời :
Bạn sẽ lưu được dữ liệu vào database mà không còn lo lắng về kiểu dữ liệu nhập vào có tương thích với kiểu dữ liệu trong database. Bạn sẽ dễ dàng hơn trong việc cập nhật dữ liệu với hàm được cung cấp
 
<SCRIPT LANGUAGE=''VBSCRIPT'' RUNAT=''Server''>
Function CForDb(Proposed, ReplaceBy)

'==========================================================================
'=== chuyển đổi các thông tin chuyển tới từ các form để lưu trong db '==========================================================================

CForDb = ReplaceBy
On Error Resume Next

If IsNull(Proposed) Or Len(Proposed) = 0 Then
Exit Function
End if

Select Case TypeName(ReplaceBy)
Case ''Boolean''
    CForDb = Cbool(Proposed)
Case ''Date''
    If IsDate(Proposed) then CForDb = CDate(Proposed)
Case ''Integer'', ''Long''
    CForDb = CLng(Proposed)
Case Else
    CForDb = CStr(Proposed)
End Select
End function
</SCRIPT>


<!--#include file=''CForDb.inc''-->
<!--#include file=''adovbs.inc''-->
<html>
<head>
</head>
<body>
<%
On Error Resume Next
Set oRs = Server.CreateObject(''ADODB.Recordset'')
' Bạn tạo tương ứng với database của bạn
oRs.Open ''Table1'', ''DSN=mydb'', adOpenKeyset, adLockPessimistic, adCmdTable
If Err.Description <> '''' Then
    Response.Write ''<B>Database Error: '' + Err.Description + ''</B>''
else
    oRs.AddNew
    oRS.Fields(''Quote'') = CForDb(Request.Form(''Quote'').Item, Null)
    oRS.Fields(''FirstVisit'') = CForDb(Request.Form(''FirstVisit'').Item, Date())
    oRS.Fields(''Happy'') = CForDb(Request.Form(''Happy'').Item, false)
    oRS.Fields(''Age'') = CForDb(Request.Form(''Age'').Item, 0)
    oRS.Update
If Err.Description <> '''' Then
Response.Write ''<B>Database Error: '' + Err.Description + ''</B>''
else
Response.Write ''<p ALIGN=CENTER><br><br><br><b><big>Thank you for filling out the form</big></b><br></p>'' & vbCrLf
End If
End If
oRS.Close
Set oRs = Nothing
%>
</body>
</html>
 

Bạn có thể tải source tại đây
Theo pcworld.com.vn

Không có nhận xét nào:

Đăng nhận xét