Database connection (Error-free)

Source: Internet
Author: User
Tags count empty end insert sql return string
Data | database/Database Connection | database connection ASP connecting SQL Server2000 database routines
--------------------------------------------------------------------------------

<%
Dim mode
Mode=request.form ("Text4")
If Mode=1 Then
Name=request.form ("Text1")
Age=request.form ("Text2")
Addr=request.form ("Text3")
If not isnumeric (age) Then Response.Write "Age must is Number!<a href=javascript:history.go ( -1) >try > ": Response.End
AGE=CLNG (age)
End If
'********************************************************************
' Connections related to SQL Server2000
' You can build your database connection by referencing it
'********************************************************************
' Please note:
' Please configure strserver,struid,strsapwd,strdbname four parameters according to the situation
Dim Strserver,struid,strsapwd,strdbname
Strserver= "(local)" Database server name
Struid= "TestUser" ' Your login account
strsapwd= "12345" ' Your login password
Strdbname= "db_test_com" ' Your database name

Dim Conn ' Database connection
Dim strdsn ' Database connection string
Dim Rs ' command string
strdsn= "Driver={sql server};server=" &StrServer& "uid=" &StrUid& ";p wd=" &StrSaPwd& "; Database= "&strdbname
' Establish a connection with the database master
Set Conn = Server.CreateObject ("ADODB. Connection ")
Set Rs=server.createobject ("ADODB. RecordSet ")
Conn.Open strDSN
'********************************************************************
'********************************************************************


Dim strSQL
'********************************************************************
' Read database related operations
Sub Readdb ()
Strsql= "SELECT * FROM Test"
Rs.Open strsql,conn,1,1
If Rs. EOF then Response.Write "No record at all": Exit Sub
Response.Write "<table border=1>"
Response.Write "<tr>"
For i=0 to Rs. Fields.count-1
Response.Write "<td><font color=blue>" &rs. Fields (i). name& "</font></td>"
Next
Response.Write "</tr>"
While not Rs. Eof
Response.Write "<tr>"
For i=0 to Rs. Fields.count-1
Response.Write "<td>" &rs. Fields (i). value& "</td>"
Next
Response.Write "</tr>"
Rs. MoveNext
Wend
Response.Write "</table>"
Rs. Close
End Sub
'********************************************************************


'********************************************************************
' Write database related operations
Sub InsertData ()
Strsql= "INSERT into Test (NAME,AGE,ADDR) VALUES (' &name&" ', ' &age& ', ' "&addr& ')"
Rs. Open strsql,conn,1,3
End Sub
'********************************************************************

If Mode=1 Then
Call InsertData ()
Response.Write "Insert ok!"
ElseIf mode=2 Then
Call READDB ()
End If
' Release the database connection object
Set rs=nothing
Set conn=nothing
%>
<HTML>
<HEAD>
<TITLE></TITLE>
<script language=javascript>
function Clickit (flag) {
var Form1=document.form2
Form1.text4.value=flag;

if (flag==1) {
if (form1.text1.value== "") {
alert ("name cant empty!");
return false;
}
if (form1.text2.value== "") {
alert ("Age cant empty!");
return false;
}
if (form1.text3.value== "") {
alert ("addr cant empty!");
return false;
}

}

[1] [2] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.