Three ways to access the database

xiaoxiao2021-03-06  20

With the rapid development of Internet / Intranet technology, Web has become a new business. Various manufacturers have also established direct contacts with end users through Internet / Intranet technology, such as selling products to users on the Web, so that users Query your favorite items online and provide online ordering services. So the web-based database technology came into being. However, in the traditional HTML page, the access database is generally implemented by the public gateway interface (CGI), which is not only developing difficulties, but also reduces the running efficiency of the server when there is a lot of concurrent requests, while using ASP (Active) Server Pages) Technical implementation of database access can better solve this problem.

Microsoft's ASP technology is a script writing model running on the server, which enables developers to write scripts using almost all scripting languages ​​(VBScript, JScript or Perl, etc.), which can perform application logic and can call ActiveX components. Perform special tasks, such as database queries, file input and other. It integrates the simplicity of IDC and the flexibility of ISAPI. Since Microsoft IIS 3.0 (Internet Information Server 3.0) has made Microsoft and has been committed to developing ASP technology. So far, ASP technology has been further developed, providing new features such as ASP scripting debugging tools, transaction, new ActiveX components, RDS in IIS 4.0.

In the ASP script, you can access the database in three ways: Traditional IDC (Internet DataBase Connector) mode, and RDS (Remote Data Service). In terms of concept, these three access methods are done by Internet Information Server. The web browser uses the HTTP protocol to submit requests to the Internet Information Server (IIS). The Internet Information Server performs the operation of the access database and answered as a document in an HTML format.

One. Internet Database Interface (IDC)

IDC is a traditional database query tool that defines and executes the SQL command for the database query and returns a page of the specified data format to the browser. The maximum feature of using IDC access to the database is simple, almost no need to program access to the database.

IDC uses two files to control how to access databases and how to create a returned web page. These files are IDC (.idc) files and HTML extensions (.htx) files, respectively.

The .idc file must include an ODBC data source (DataSource), the file name (Template) of the HTML extended file, and the SQL statement to be executed. There are many optional words

Segment, you can use it as needed.

The .htx file is a HTML document with the additional marker enclosed in <%%> or .

Use these tags to add dynamic data to the document. There are six keywords in .htx files

(Begindetail, EndDetail, IF, ELSE, ENDIF, and "% z") use to control how data in the database is merged with .htx files in HTML format. Database column name description What data is returned in the HTML document.

Below is a simple example of IDC application, this example is a inventory inventory of a virtual fruit store.

.idc file: DataSource: Kucun

template: idcreslt.htx

SQLStatement: select * from inventory table

.htx file: Fruit store inventory results </ title> </ head></p> <p><Body></p> <p><Table Border = 1> <Caption> Fruit Store Inventory Results </ CAPTION></p> <p><% begindetail%></p> <p><% IF CurrentRecord EQ 0%></p> <p><! - CurrentRecord represents the currently returned record -></p> <p><TR> <TD> Sequence Number </ TD> <TD> Product Name </ TD> <TD> Stock Quantity </ TD> <TD></p> <p></ Td> <TD> Unit </ TD> <TD> Enterprise Date </ TD> </ TR></p> <p><% endIF%></p> <p><! - Output Query Returned -></p> <p><TR> <TD> <% ID%> </ TD> <TD> <% Product Name%> </ TD> <TD> <% inventory</p> <p>%> </ Td> <td> <% 进价%> </ td> <td> <% unit%> </ td> <TD> <% purchase date%> </ td> </ tr></p> <p><% enddetail%></p> <p></ Table> </ body> </ html></p> <p>Note: <% BEGINDETAIL%> and <% endDetail%> Determines the boundary, determine which of the database will be used as return data and displayed in the document. The column returned by the query is enclosed by <%%>, as in the example of <% product name%> and <% inventory%>, etc.</p> <p>To perform an IDC query, the usual approach is to embed a .idc file connection in the HTML file. example</p> <p>For example, the following HTML statement issues a request to the web server and requires the IDCTEST.IDC file.</p> <p><a href="http://localhost/idcte/idcte/idctest/idc"> Stock query </A> When the web server receives this request, call httpodbc.dll (IDC), with a certain data source Connect, pass the SQL command to the database. When the SQL statement is executed, the IDC integrates the returned data into the .htx file. IDC returns this document to the web server, the web server returns to the browser.</p> <p>two. ActiveX Data Object (ADO)</p> <p>Unlike the IDC, use the ADO access database more similar to writing database applications, ADO puts most of the database operations in seven objects, programming these objects to perform the appropriate database operations in the ASP page. ADO is one of the core of ASP technology, which is concentrated in ASP technology rich and flexible database access. ADO has established a script writing model based on a web-way access to the database, which not only supports the core functions of any large database, but also supports the specific properties proprietary in many databases. ADO uses native data sources to access databases via ODBC. These databases can be a relational database, a textual database, a hierarchical database, or any database that supports ODBC. The main advantages of ADO are ease of use, high speed, occupancy memory and disk space, so it is very suitable for database access technologies as server-side. Compared to the CGI program accessed, it is multithreaded. When there is a lot of concurrent requests, the server's operational efficiency can also be maintained, and through the connection pool (Connection pool) technology and the full control of the database connection resource. Provide efficient connection and access with the remote database, but it also supports transaction (Transaction) to develop high-efficiency, highly reliable database applications.</p> <p>It is because using ADO needs to write scripts, so ADO can achieve more complex, more flexible database access logic. Currently, ADO includes seven objects such as Command, Connection, RecordSet and a dynamic Properties collection, and most of the database access tasks can be done through their combinations.</p> <p>The ASP script generally uses the ADO access database should use the Connection object to establish and manage the connection to the remote database; provide a flexible query using the Command object; use the RecordSet object to access the database query returned. These three are the most basic and core objects in ADO. The following example explains how to use these three objects to access the database.</p> <p><% DIM STRDSN, STRSQL</p> <p>'Define the used variable</p> <p>DIM CN, RS, CM, Objname, Objkucun, ObjPrice, Objunit, Objdate</p> <p>STRDSN = "filedsn = kucun.dsn"</p> <p>'Establish DSN string</p> <p>SET CN = Server.createObject ("AdoDb.Connection")</p> <p>'Creating an instance of the Connection object</p> <p>Cn.open strDSN</p> <p>'Establish a connection to the data source specified by STRDSN</p> <p>Set cm = server.createObject ("adoDb.command")</p> <p>'Creating an instance of a Command object</p> <p>Set cm.activeConnection = CN</p> <p>'Specify CM to connect to the database established by CN</p> <p>cm.commandtext = "INSERT INTO inventory" (product name, inventory, price, unit, enter</p> <p>Values ​​(?,?,?,?,?)</p> <p>'Predefined SQL query commands</p> <p>cm.prepared = true</p> <p>'Notification of the data source pre-processed the query command</p> <p>CM.Parameters.Append Cm.createParameter (Product Name ", 200,, 255)</p> <p>'Define query parameters</p> <p>CM.Parameters.Append Cm.createParameter ("Inventory", 200,, 255)</p> <p>CM.Parameters.Append Cm.createParameter ("Inc.", 200, 255)</p> <p>CM.Parameters.Append Cm.createParameter ("Unit", 200,, 255) cm.parameters.Append Cm.createParameter ("Promoting Date", 200, 255)</p> <p>CM (Product Name) = "Grape"</p> <p>'Assigning parameters</p> <p>cm ("stock") = 400</p> <p>CM ("Imprid") = 0.8</p> <p>CM ("unit") = "kg"</p> <p>CM ("purchase date") = # 95-6-12 #</p> <p>cm.execute</p> <p>'Perform a predefined SQL query command according to the given parameter value</p> <p>CM ("Product Name") = "Persimmon"</p> <p>cm ("stock") = 300</p> <p>CM ("Imprid") = 0.4</p> <p>CM ("unit") = "kg"</p> <p>cm ("purchase date") = # 95-6-11 #</p> <p>cm.execute</p> <p>SET RS = Server.createObject ("AdoDb.Recordset")</p> <p>'Creating an instance of the Recordset object</p> <p>strsql = "SELECT * FROM inventory"</p> <p>'Establish a query command string</p> <p>RS.Open Strsql, CN</p> <p>'Use the CN database connection to execute strsql definition query commands</p> <p>Set objName = rs ("Product Name")</p> <p>'Save the returned column in the variable</p> <p>Set objkucun = rs ("inventory")</p> <p>Set objprice = rs ("Inc.")</p> <p>Set objunit = rs ("unit")</p> <p>Set objdate = rs ("purchase date")%></p> <p><Center></p> <p>'Use the cycle statement to output the query results in tabular form</p> <p><Table Border = 1> <Caption> Fruit Store Inventory Results </ CAPTION></p> <p><% DO Until Rs.eof%></p> <p><TR> <TD> <% response.write objName%> </ td> <td> <% response.write</p> <p>Objkucun%> </ td> <td> <% response.write objprice%> </ td> <td> <%</p> <p>Response.write objunit%> </ td> <td> <% response.write objdate</p> <p>%> </ Td> </ tr></p> <p><% rs.movenext%></p> <p><% Loop%></p> <p></ Table> </ center></p> <p>In the above example, first establish a connection with the data source to be operated using the Connection object; then use the Command object to establish a predefined query command with five parameters (add a purchase record to the inventory table), and twice with different Parameter values ​​(grapes and persimmons) Execute the query command; finally use the Recordset object to query the inventory information of all items in the inventory table, and output in the form of a table. If you are more familiar with VB's database programming, it is found that the remote data object (RDO) in the VB is similar.</p> <p>Like IDC, the usual practice running the database access script is to embed a .asp file connection in the HTML file. For example, the following HTML statement issues a request to the web server, requiring execution</p> <p>ADOTEST.ASP file.</p> <p><a href="http://localhost/idctest/ado/adotest.asp"> inventory query</p> <p></a></p> <p>three. Remote Data Service (RDS)</p> <p>RDS is a new concept of IIS 4.0, which is developed by the original Advanced Data Connector (ADC) in the ASP. In IIS 4.0, RDS is integrated with ADO, using the same programming model, providing access to the remote database.</p> <p>Although ADO is capable of providing very powerful database access, it does not support data remote operations (Data Remoting). In other words, ADO can only perform queries and return the result of the database query, which is static, the database on the server does not "live connection". If the client needs to modify the data in the database, you must construct the SQL statement of the data to perform the corresponding query action.</p> <p>RDS is further more than ADO, it supports data remote operation. It not only performs queries and returns the result of database query, but also this result is "dynamic", the database on the server maintains "live connection relationship" as seen by the client. That is, the server-side data is moved to the client. After the client modifies the data, call a database update command to write the client's modification back to the database, just like using the local database.</p> <p>Since RDS is integrated with ADO, the bottom layer of RDS is done, so RDS can be understood as an ADO's RDS, that is, the remote data service of the ActiveX data object. Therefore, RDS also has an ease of use of ADO, which is only that RDS needs to be used with the data binding control, such as Sheridan ActiveX Database Grid control. As ADO is similar to RDO in VB, RDS is similar to the remote data control (RDC) in VB. Here is an example of providing client data remote operation using RDS:</p> <p><Html> <head> <title> Fruit store inventory results </ Title> </ hEAD></p> <p><Center> Fruit Shop Stock Plant </ CENTER></p> <p><! - Insert Sheridan Database Grid Control to the HTML page -></p> <p><Center> <Object</p> <p>ClassID = "CLSID: AC05DC80-7DF1-11D0-839E-00A024A94B3A"</p> <p>Codebase = "http://localhost/msadc/ssdatb32.cab" ID = "grid"</p> <p>DataSRC = "# ADC"</p> <p>Width = 600 height = 150></p> <p><Param name = "allowaddnew" Value = "true"></p> <p><Param name = "allowdelete" value = "true"></p> <p><Param name = "allowupdate" value = "true"> </ object></p> <p><Table></p> <p><! - Provide three text boxes, enter database connection information and query commands -> <TR> <TD> Server: <TD> <input name = "server" size = 70></p> <p><TR> <TD> Database Connection: <TD> <Input Name = "Connect" Size = 70></p> <p><TR> <TD> Query Command: <TD> <input name = "query" size = 70> </ table></p> <p><Hr></p> <p><! - Provide a client operation command button -></p> <p><Input Type = Button Name = "Run" value = "Run"></p> <p><Input Type = Button Name = "MoveFirst" Value = "First"></p> <p><Input Type = Button Name = "MovePrev" Value = "Previous"></p> <p><Input Type = Button Name = "MoveNext" Value = "Next"></p> <p><Input Type = Button Name = "MoveLast" Value = "Last"></p> <p><Input Type = Button Name = "Submit" Value = "Submit Update"></p> <p><Input Type = Button Name = "Cancel" Value = "Cancel Update"> </ center></p> <p><! - Insert the RDS object in the page, name "ADC", size is 1 pixel (invisible) -></p> <p><Object classid = "CLSID: BD96C556-65A3-11D0-983A-00C04FC29E33"</p> <p>ID = ADC Height = 1 width = 1> </ Object></p> <p><Script language = "vbscript"></p> <p><! - The following is the event handle of each command button and the form -></p> <p>Sub window_onload</p> <p><! - Form loading event, set the initial value in three text boxes -></p> <p>Server.Value = "http:// localhost"</p> <p>Connect.Value = "DSN = Kucun"</p> <p>Query.Value = "SELECT * from inventory table"</p> <p>End Sub</p> <p>Sub run_onclick <! - The process of "Run" command, executes the SQL command in the text box -></p> <p>Adc.server = Server.Value</p> <p>ADC.CONNECT = Connect.Value</p> <p>Adc.sql = query.value</p> <p>ADC.REFRESH</p> <p>End Sub</p> <p>Sub moveverfirst_onclick</p> <p><! - The process of "first" command -></p> <p>Adc.recordset.movefirst</p> <p>End Sub</p> <p>Sub moveXT_ONCLICK <! - "Next" command processing -> On Error ResMe Next</p> <p>Adc.recordset.movenext</p> <p>IF Err.Number <> 0 THEN</p> <p>Adc.recordset.movelast</p> <p>END IF</p> <p>End Sub</p> <p><! - "The last" command and the "last" command process, omitted-></p> <p>Sub Submit_onclick</p> <p><! - Processing of the "Submit Update" command -></p> <p>Adc.submitchanges</p> <p>ADC.REFRESH</p> <p>End Sub</p> <p>Sub Cancel_onclick</p> <p><! - "Cancel Update" command processing -></p> <p>Adc.cancelupdate</p> <p>ADC.REFRESH</p> <p>End Sub</p> <p></ Script> </ body> </ html></p> <p>Therefore, RDS is displayed and operated by binding data on ADO, providing a more powerful data expressions and remote data manipulation functions to clients. It can be said that RDS is currently the best web-based remote database access.</p> <p>The above is the three ways of the ASP access to the database, and they have their own features. The IDC is very simple, using the .idc file and .htx files separately complete the database access and output tasks, but it is not flexible. ADO is a way recommended in the ASP. It is powerful and convenient to use. It is one of the core technologies of ASP, but it is more complicated when providing the user's remote operation database. It is difficult to implement a certain difficulty. RDS is based on ADO and provides powerful tools for remotely operated databases. So when you need to provide high performance, high-reliability remote data operation functions, RDS is more ideal.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-39444.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="39444" 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.035</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 = '8YoIL8GygQn6EhNoKjVZAITl3_2FGWBm1dP72EGE1JzYJ6y3_2FVSM8qzSNDgHDlGkivIQAkOBegrNQn3gYg2R_2FzwQ_3D_3D'; 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>