To create a. NET reusable database component with Vs.net

Source: Internet
Author: User
Create a | data | database Microsoft's. NET architecture, which provides us with a powerful tool for building distributed Application systems. The. NET component development is much simpler and faster than the traditional Windows application's COM components. Also, the deployment of components does not need to be marked as a COM component in the registry, as long as the. NET components are copied to the appropriate folder.

Here we will actually develop a Access2000 database access component with the integrated development tool of the. NET program, and reuse it in another ASP.net application system.

(i) Component creation

Starts Vs.net, creates a new Visual C # project, and the template chooses class library. As shown in figure one.




This creates a new folder ClassLibrary1 under our machine's My Documents \visual Studio Projects. and automatically generate Class1.cs files:

Using System;

Namespace ClassLibrary1

{

<summary>

Summary description of the CLASS1.

</summary>

public class Class1

{

Public Class1 ()

{

//

TODO: Add constructor logic here

//

}

}

}



Because the components we are about to generate are 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 with our own named MyDB, and the class name will be replaced by Mydblink. Then we also defined three attributes to make our code more versatile:

public string sDBPath = ""; Database path (including database name)

public string sdbtable = ""; Table name

public string spassword = ""; Database Password

The GetData () method in the Mydblink class returns a view of the table for the query. So we customize 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 * from" + sdbtable, oconn);

ODTST = new DataSet ();

Oadp.fill (ODTST, "table");

return odtst.tables["Table". DefaultView;

}

}

}

The meaning of the code in the GetData () method is to declare three variable oconn (database connection object), OADP (data adapter object), ODTST (Data set object) first. It then instantiates the database Connection object and opens the database connection oconn, then generates the data adapter object OADP through the SQL statement, and populates the table table (automatic generation) of the DataSet object odtst through the Oadp fill method. Finally, the default view DefaultView for table tables is returned.

In order to generate the component files that we expect to be named MyClass.dll, you must click the "project" \ "ClassLibrary1 Properties" menu, and in the Pop-up Property Page dialog box, modify the value of "assembly name" to "MyClass", and then close the Property Page dialog window. Then execute the Generate ClassLibrary1 command under the Run menu. A MyClass.dll file is generated under My Documents \visual Studio Projects\bin\debug, which is the database access component that we are going to use.



(ii) Deployment of components

To test the components we build, we can build another "ASP.net Web application" project, assuming the project name is WebApplication2, which will generate a WebApplication2 virtual path in IIS, Also generates a WebApplication2 folder under the Default Web site. The components we are about to test will be copied to the Bin folder under WebApplication2. This is where the components are deployed.



Three ASP.net's test code

Drag a DataGrid component on the WebForm of the generated WebApplication2 project, add a reference to the MyClass.dll component in the project, and then reference our namespaces in the file header:

Using MyDB;

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

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 a table in the database

DataGrid1.DataSource = Odbtable.getdata ();

Datagrid1.databind ();

}

Since the table view fetched from the database is bound to the DATAGRID1 as a DATAGRID1 data source, the data in the table Mytab is displayed in DataGrid1 after the WebApplication2 project is run.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.