[ASP Basics] Connects to Access Database via ASP

xiaoxiao2021-03-05  25

(http://pcit.diy.myrice.com/jiaochueng/asp/4-2.htm)

Preface:

How to establish a connection through the ASP and database is a very realistic issue, I try to write very detailed. I believe this article believes that you have been able to write similar relatively simple and practical programs.

(1) Selection of databases:

There are many databases you can choose, SQL Server, Access (*. MDB), Excel (*. Xls), FoxPro (*. Dbf) or even ordinary text files (* .txt) can achieve the function of storing data. So what database is the most appropriate? Where SQL Server is server-level, enough to cope with tens of thousands of visits per day. Access97 is recommended for non-server-level database recommendations. Because: (1) As long as PWS or IIS4 is installed, the ODBC driver of Access is installed, that is, your system will definitely support access to the Access database. (2) The SQL instruction supported by Access is very complete. (3) The Access ODBC driver is very efficient. Although it is not a server-level database, it is still more than enough database support as a small and medium-sized enterprise. (4) Access 97 is in office97, it is very convenient to get and use.

This article is based on the Access database to introduce Web-based data inventory method.

(2) Example: We build a "customer" database, you need to save the following customer information: customer name, contact name, city, phone number.

(1) First establish an Access database Customer.mdb (useful in the future). Established the following field: customer name, contact name, city, phone number, customer number, time. Save the table as "customer" (in the future, ===> Look at the map). Note: 2 (customer number, time) when establishing a field, because they are used in the future. It is recommended that it is best to add these two items in the future, and it will not be useless. In addition, Access will ask if the Access will ask if you build a keyword, we don't have to build it. Also: The "default value" of the time field is "now ()", that is, the time to go to the system. At the same time, the index entry is set to "have (allowed repetition)", the meaning: the web is highly possible to access data at the same time, so it is allowed to repeat, the index is set to "have" can speed up the sorting speed. ===> Look at the picture

(2) Establish a submission form Add.htm:

The source code is as follows:

Add Database record </ title> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312"> </ head> <body bgcolor = "# ffffffff "> <p align =" center "> <font size =" 5 "> Add Database Record </ font> </ p> <form method =" post "Action =" adddatabase.asp "> <div align =" center > <table width = "46%" border = "0"> <tr> <td width = "28%"> <div align = "right"> company name: </ div> </ td> <TD Width = "72%"> <input type = "text" name = "companyName"> </ td> </ tr> <tr> <td width = "28%> <div align =" right "> Contact Name : </ Div> </ td> <td width = "72%"> <input type = "text" name = "cname"> </ td> </ tr> <tr> <td width = "28%" > <div align = "right"> City: </ div> </ td> <td width = "72%"> <input type = "text" name = "city"> </ td> </ TR> <Tr> <TD Width = "28%> <div align =" right "> Phone number: </ div> </ td> <TD width =" 72% "> <input type =" text "name =" Tel "> </ td> </ tr> <tr> <td width =" 28%> <div align = "right"> </ div> </ td> <td width = "72%> <input TYPE = "submit" name = "submit" value = "submit"> <input type = "reset" name = "value ="</p> <p>RESET "> </ td> </ tr> </ table> </ div> </ form> </ body> </ html> This is nothing difference with ordinary HTML, and the key part has been marked with yellow. Submit method Using POST, the procedure submitted after submitting is AddDatabase.asp (discuss it next step). There is also a name to each item (Name, useful).</p> <p>(3) Key parts: Establish contacts with the database and add information collected in the form to the ACCESS database.</p> <p>The ASP code is placed between <% and%>.</p> <p><% CompanyName = Request ("CompanyName") CNAME = Request ("cname") City = request ("city") Tel = Request ("tel")</p> <p>The above, first obtain the data filled in the form, using the ASP Request object very easy to achieve data collection. The double quotation marks are the names of each project established in the previous step. So in the future, it will be a good habit for each project, although it is sometimes not used. The data obtained in this way is saved in the variable of the equal sign side.</p> <p>If CompanyName = "" or cName = "" or city = "" or tel = "" Then'Response.write "<html> <body> <center> <font size = 5> Please fill in the data complete. </ Font > </ center> </ body> </ html> "response.redirect" error.htm "Response.end endiff</p> <p>Here is to detect if each item is input, if it is not executed down, and the error message is displayed. Note: You can use two methods. I am listed. Method 1: Write an error page using the response.write method, similar to the Document.Write written by JavaScripts, just ASP uses Response.write. Method 2: Use the Response.Redirect method to guide the error page error.htm. I add a single quotes before the code is the code. (ASP is indicated by a single quotation number ').</p> <p>Set dbconnection = server.createObject ("adodb.connection") dbpath = server.mappath ("Customer.mdb") DBConnection.Open "driver = {Microsoft Access Driver (* .mdb)}; dbq =" & dbpath</p> <p>(1) Establish a Connection object: DBConnection, establish a connection (connection) is the beginning of the data inventory. We use the ASP's built-in object method Server.createObject to establish a connection to the database. AdoDB is an ASP's data inventory to get a component, don't worry that there is an ASP has it, it is built-in. (2) Open the database Customer.mdb using the DBConnection.open method. Driver Parameter Syntax: Driver = {Microsoft Access Driver (* .mdb)} must be written. Tell the system uses Access ODBC driver. The DBQ parameter is used to specify the open database file, which must be a complete path. For example, "c: /inetpub/wwwroot/customer/customer.mdb", we use server.mappath to get the full path to the database. SET RS = Server.createObject ("AdoDb.Recordset")</p> <p>Built RECORDSET object RS. Connection is just a connection between the database, and the RECORDSET object should be established again.</p> <p>RS.Open "Customer", DbConnection, AdoPENDYNAMIC, 3</p> <p>Open the table "Customer" in the database (the customer is a table name). Syntax: RS.Open data table name or SQL instruction, established Connection object (here is dbconnection), lock type</p> <p>RS.ADDNEWRS ("Company Name") = CompanyNamers ("Contact Name) = CNAMERS (" City ") = cityRS (" Phone Number) = TELRS.UPDATE</p> <p>Add data to the database request with AddNew. Finally, use Update to write. Note: The variable on the right side of the equal sign holds form data taken with Request. In the double quotation mark is the field of the database, it should be consistent with the database. Otherwise it will be wrong. In this way, the data within the variable is written to the field corresponding to the database.</p> <p>Response.write "<html> <body> <center> <font size = 5> Congratulations, your data has been added to the database! <P>" response.write "<a href='ViewDatabase.asp'> View data </a> "Response.write" </ font> </ center> </ body> </ html> "</p> <p>After the write is successful, the congratulations will be displayed and the connection to view the database record.</p> <p>%> <html></p> <p><head> <title> </ title> </ head></p> <p><body> </ body> </ html></p> <p>(3) Display / view database record:</p> <p><% set dbconnection = server.createObject ("adoDb.connection") dbpath = server.mappath ("customer.mdb") dbconnection.open "Driver = {Microsoft Access Driver (* .mdb)}; dbq =" & dbpath</p> <p>Establish a connection with the database, with the same. SQL = "SELECT *" SET CUSTOMERLIST = DBCONNECTION.EXECUTE (SQL)%></p> <p>Establish a SQL query statement, indicating that all records from the "Customer" table, the ORDER BY Time DESC represents "Sort in time descending sequence," the last added information is then displayed. The following sentence performs SQL queries and returns the result to the Recordset object CustomerList.</p> <p><html> <body> <font size = "5"> <center> database record </ center> </ font> <center> <table border = 1> <TR></p> <p><TD> Name </ TD> <TD> Company Name </ TD> <TD> Contact Name </ TD> <TD> City </ TD> <TD> Phone Number </ TD> </ TR> < % Do While Not Customerlist.eof%></p> <p>The detection record has arrived at the last one. EOF represents end of file.</p> <p><Tr> <TD> <% = CustomerList ("Customer Number)%> </ TD> <TD> <% = Customerlist (" Company Name ")%> </ TD> <TD> <% = Customerlist (" Contact Name ")%> </ TD> <TD> <% = Customerlist (" The City ")%> </ TD> <TD> <% = CustomerList (" Phone Number)%> </ TD> < / TR></p> <p><% = CustomerList ("Customer Number)%> It can be said to be a short-written in the response.write method. Write the data within the Customer Number field to the TD tag.</p> <p><% CustomerList.Movenext loop%></p> <p>If there is not yet to go to the last one, the pointer is moved to the next. Use the Do While ... Loop loop to get all the records one by one.</p> <p></ Table> </ center></p> <p><center> <input type = "button" οnclick = "javascript: location.href = 'add.htm'" value = "Add Data"> </ center> </ body> </ html></p> <p>Defect: only the most basic functionality is achieved. Don't say other functions first, just look at the data adding and viewing, you should also add "Paging Function". Otherwise, the data is large to a certain extent, all data is basically impossible. I will launch an improved version in the next phase.</p> <p>-------</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-34393.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="34393" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.037</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'cW0YPVN6MGO9IIcbB_2FMumJ468pX8d5evN3qoFwy_2F2Hd5VCOaSbFaRqC30Zx_2B_2FggIFwjB_2BqNJhFJC9_2Beb'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>