ASP Database Insert
<%
'Declare Variables
Dim dcnDB 'As ADODB.Connection
Dim rsDB 'As ADODB.Recordset
Dim sqlInsert 'As String
Dim filePath 'As String
'Find path of database file
filePath = Server.MapPath("advisor.mdb")
'Open Database Connection
Set dcnDB = Server.CreateObject("ADODB.Connection")
'dcnDB.Open "DBName" - This is for an ODBC Datasource (if set up) or
' use the below code for opening a Access 2000 DB
dcnDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&filePath
dcnDB.Open
'Check for action
If Request.Form("action") = "Insert" Then
'Create SQL INSERT statement from form fields
sqlInsert = "INSERT INTO Students (StudentID,FirstName,LastName) VALUES (" _
& Request.Form("StudentID") & ",'" & Request.Form("FirstName") & "','" _
& Request.Form("LastName") & "')"
'Execute the query
dcnDB.Execute(sqlInsert)
'Notify action taken
Response.Write "Record Inserted<br><br>"
End If
%>
<!-- this is the HTML part (creating a table to output records) -->
<table width="400" cellpadding=0 cellspacing=0 border=0>
<tr>
<td valign="top" colspan=3>
<!-- navigation row -->
<a href="insert_students.asp">Add Student</a> |
<a href="query_students.asp">View Records</a>
<br><br>
Please fill in all fields before submitting:
<br><br>
</td>
</tr>
<form action="insert_students.asp" method="post">
<tr>
<td valign="top">Student ID</td>
<td valign="top"><input type="text" name="StudentID"></td>
</tr>
<tr>
<td valign="top">First Name</td>
<td valign="top"><input type="text" name="FirstName"></td>
</tr>
<tr>
<td valign="top">Last Name</td>
<td valign="top"><input type="text" name="LastName"></td>
</tr>
<tr>
<td valign="top" colspan=2 align="center">
<br>
<input type="reset" value="Clear Form"> <input type="submit" value="Insert Record">
<input type="hidden" name="action" value="Insert">
</td>
</tr>
</form>
</table>
<%
'Close Connection and clean up
dcnDB.Close
%>
Next