Create a .NET to create a .NET to reuse database components

zhaozj2021-02-17  62

The introduction of Microsoft's .NET architecture, providing us to build a distributed application system with a powerful tool. Compared to traditional Windows applications, .NET's component development is simple and fast. Moreover, the deployment of components is not like a COM component, but it is possible to make a mark in the registry, and the .NET component can be copied to the corresponding folder.

Below we use the .NET program integrated development tool Visual Studio .NET actually develop an Access2000 data inventory to get the component and reuse it in another ASP.NET application system.

(1) Creation of components

Start VS.NET, create a new Visual C # item, template Select "Class Library". As shown in the figure.

This will generate a new folder Classlibrary1 in our machine's "My Documents" / Visual Studio Projects. And automatically generate class1.cs files:

Using system;

Namespace Classlibrary11

{

///

/// Class1 summary description.

///

Public Class Class1

{

Public class1 ()

{

//

// TODO: Add constructor logic here

//

}

}

}

Because we are about to generate components related to data access, you must add a database reference statement:

Using system.data;

Using system.data.oledb;

In addition, we can replace the namespace to our own name mydb, and the class name is also replaced with mydblink. Then we also define three properties so that our code is very versatile:

Public string sdbpath = ""; // database path (including database name)

Public string sdbtable = ""; // table name

Public string spassword = ""; // Database instruction

The getData () method in the MyDBLINK class will return the view of the query table. This way we customized the complete code as follows:

Using system;

Using system.data;

Using system.data.oledb;

Namespace mydb

{

Public Class MyDBLINK

{

Public String SDBPath = ""

Public String SDBTABLE = ""

Public String Spassword = ""

Public DataView getData ()

{

OLEDBCONNECTION OCONN;

OLEDBDataAdapter OADP;

DataSet Odtst;

Oconn = new oledbconnection ("provider = microsoft.jet.Oledb.4.0; data source =" sdbpath "; password =" spassword ";")

Oconn.open ();

OADP = New OLEDBDataAdapter ("SELECT *" SDBTABLE, OCONN);

odtst = new dataset ();

OADP.FILL (ODTST, "Table");

Return Odtst.Tables ["Table"]. DefaultView;

}

}

}

The code in the getData () method is: first declare the three reference types of variables Oconn (Database Connection Object), OADP (Data Adapter Object), ODTST (Dataset Object). Then instantiate the database connection object and open the database connection oponn; generate the data adapter object OADP via the SQL statement; then fill the data set object ODTST Table table (Auto Generation) by OADP's Fill method. Finally, returns the default view of the Table table DefaultView. In order to generate the component file we expected to be myclass.dll, you must click on the menu "item" / "classlibrary1 property", in the pop-up property page dialog box, modify the value of the "Merchase Name" to "MyClass", determine After closing the property page dialog window. Then perform the "Generate ClassLibrary1" command under the "Run" menu. At this time, it is a Myclass.dll file under "My Document" / Visual Studio Projects / Bin / Debug, which is the upcoming data inventory.

(2) Deployment of components

In order to test the components we generated, an "ASP.NET Web Application" project can then be established, assume that the project is called WebApplication2, which will generate a WebApplication2 virtual path in IIS while generating a WebApplication2 folder under the default website. We are about to test the components to copy under the bin folder under WebApplication2. This component has been deployed.

(3) ASP.NET test code

Drag and drop a DataGrid component on WebForm to generate WebApplication2 projects, add reference to myclass.dll components in the project, and then quote our namespace in the file header:

USING MYDB;

Then type the following code in the weight_load event of WebForm1.aspx.cs:

Private Void Page_Load (Object Sender, System.EventArgs E)

{MYDBLINK ODBTABLE;

ODBTABLE = New mydblink ();

Odbtable.sdbpath = "d: //_my_documents//database.mdb";

Odbtable.spassword = ""

Odbtable.sdbtable = "myTab"; // myTab is the table in the database

DataGrid1.datasource = ODBTABLE.GETDATA ();

DataGrid1.databind ();

}

Since the table view taken from the database is bound to DataGrid1, after running the WebApplication2 project, the data in the table MyTab is displayed in DataGrid1.

Zhang Qingzhangking at 263.net http://www.why100000.com http://soft.why100000.com 2004.4.25

转载请注明原文地址:https://www.9cbs.com/read-28915.html

New Post(0)