I don't want to make it too complicated. I just want to demonstrate how to implement DataReader and DataSet. It plays a very interesting role and does not care whether the code structure is beautiful or the performance is good. I just want to achieve the purpose of this Article.
In order to facilitate your hands-on practice, the database uses ACCESS and a download link is provided at the end of the article. If SQL Server and ORACLE are to be used in the actual project, change the names of several OleDb functions.
I use three languages to implement the same functions. First, the "standard language" of. NET-C # is provided as the basic reference.
The following is the C # version:
<% @ Page Language = "C #" Inherits = "main_cs, main_cs" %>
Main_cs.dll file source code:
using System;using System.Data;using System.Data.OleDb;using System.Text; public class main_cs:System.Web.UI.Page { OleDbDataReader dr; OleDbCommand cmd; DataSet ds; OleDbDataAdapter adp; OleDbConnection conn; StringBuilder connStr; public void Page_Load() { connStr = new StringBuilder("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="); connStr.Append(Server.MapPath("image.mdb")); conn = new OleDbConnection(connStr.ToString()); cmd = new OleDbCommand("SELECT title FROM image_data",conn); conn.Open(); dr = cmd.ExecuteReader(); while(dr.Read()) { Response.Write(dr["title"]); } dr.Close(); conn.Close(); ds = new DataSet(); adp = new OleDbDataAdapter("SELECT title FROM image_data",conn); adp.Fill(ds); Response.Write(ds.Tables[0].Rows[0]["title"]); } } |
The following is the C ++ version:
<% @ Page AutoEventWireup = "true" Inherits = "main, net_dll" %>
Create C ++ in VisualStudio2003. net class library, named net_dll, referenced in the project System. data, System. web, System. XML to generate the net_dll.dll file. The following is the source code of the main file (all other header files and resource files are ignored when they do not exist ):
#include "stdafx.h"#include "net_dll.h"#using
#using
#using
#using
using namespace System;;using namespace System::Xml;using namespace System::Web;using namespace System::Text;using namespace System::Data;using namespace System::Data::OleDb;public __gc class main : public System::Web::UI::Page{private: OleDbDataReader __gc* dr; OleDbCommand __gc* cmd; DataSet __gc* ds; OleDbDataAdapter __gc* adp; OleDbConnection __gc* conn; StringBuilder __gc* connStr;public: void Page_Load() { connStr = new StringBuilder("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="); connStr->Append(HttpContext::Current->Server->MapPath("image.mdb")); conn = new OleDbConnection(connStr->ToString()); cmd = new OleDbCommand("SELECT title FROM image_data",conn); conn->Open(); dr = cmd->ExecuteReader(); while(dr->Read()) { Response->Write(dr->get_Item("title")); } dr->Close(); conn->Close(); ds = new DataSet(); adp = new OleDbDataAdapter("SELECT title FROM image_data",conn); adp->Fill(ds); Response->Write(ds->Tables->get_Item(0)->Rows->get_Item(0)->get_Item("title")); }};
|
The following is the J # version:
<% @ Page language = "VJ #" Inherits = "main_jsl, main_jsl" %>
Similar to C ++, use J # To create a class library named main_jsl. Reference System. Data, System. Web, and System. XML in the project. The source code is as follows:
import System.Data.*;import System.Data.OleDb.*;import System.Web.*;import System.Text.*;public class main_jsl extends System.Web.UI.Page{ OleDbDataReader dr; OleDbCommand cmd; DataSet ds; OleDbDataAdapter adp; OleDbConnection conn; StringBuilder connStr; public void Page_Load() { connStr = new StringBuilder("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="); connStr.Append(get_Server().MapPath("image.mdb")); conn = new OleDbConnection(connStr.ToString()); cmd = new OleDbCommand("SELECT title FROM image_data",conn); conn.Open(); dr = cmd.ExecuteReader(); while(dr.Read()) { get_Response().Write(dr.get_Item("title")); } dr.Close(); conn.Close(); ds = new DataSet(); adp = new OleDbDataAdapter("SELECT title FROM image_data",conn); adp.Fill(ds); get_Response().Write(ds.get_Tables().get_Item(0).get_Rows().get_Item(0).get_Item("title")); }} |
After that, place the DLL file in the Bin folder of the root directory of the site.
The functions of the above three files are the same.
Through the observation of the above three codes, we can find that the C # code is quite concise and can write the most concise Jscript. net, next article), J #, and C ++ are ugly.
In the DataTable and DataReader operations, we can see that J # And C ++ use the get_Item () function in a large number. I estimate that both J # And C ++ undergo Object transformation, it may have a slight impact on performance.
Using these two languages to create ASP. NET may be useful to friends who are used to C ++ and JAVA.
I believe that each language has its own strengths. Everyone learns from each other and constructs a good overall structure. I have seen many C # beginners who seem to be interested in VB. NET is biased, and "never use VB" is the glory. There are some friends who are interested in C ++. net and J # do not know much about each other, so they misunderstand that these two languages are useless. In fact, these understandings are one-sided.
For example:
/* Jscript | Jscript.Net */var conn = Server.CreateObject("ADODB.Connection");conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("image.mdb"));var rs = conn.Execute("SELECT * FROM image_data");while(!rs.EOF){Response.Write(rs("title").value);rs.MoveNext();}rs.Close();conn.Close(); |
You don't need to think that this is only ASP, which is also the ASP. NET of the geographic path. Although in our. in the eyes of NET developers, the reputation of RecordSet may not be as good as that of DataReader, but this is not covered in this article. Here we only say: if C # is used to implement the same functions above, the result can only be slow execution, long code, and complex structure.
In. in the world of NET, no language is the best or the worst. Each of the five languages has its own strengths and weaknesses. All of them generate the IL intermediate code, that is, they can be integrated by nature.. NET.
- Several methods for transferring page values using ASP. NET
- Implement RSA encryption in ASP. Net
- Session Value Sharing between JSP and ASP. Net