Tutorials to .com

Tutorials to .com » Dotnet » Control-and-components » Using VS.NET to create. NET components reusable database

Using VS.NET to create. NET components reusable database

Print View , by: iSee ,Total views: 10 ,Word Count: 1055 ,Date: Thu, 21 May 2009 Time: 9:04 AM

Microsoft. NET framework for the introduction, in order to build a distributed application system, we provide a powerful tool. Compared to the traditional Windows applications, COM components,. NET component development should be simple, fast and more. And the deployment of components are not COM components as the need to mark where in the registry,. NET components as long as the copy to the appropriate folder in it.

The following we will use. NET integrated development tools process Visual Studio. NET development of a practical Access2000 database access components, and its repeated use of the ASP.NET application in another system.

(A) the creation of components

Start VS.NET, the new Visual c # project, choose a template "library." As shown in Figure 1.




This machine is in our "My Documents" \ Visual Studio Projects generated a new folder ClassLibrary1. And automatically generated Class1.cs documents:

using System;

namespace ClassLibrary1

(

/ / / <summary>

/ / / Class1 summary.

/ / / </ Summary>

public class Class1

(

public Class1 ()

(

/ /

/ / TODO: Add in the constructor logic here

/ /

)

)

)



Because we are about to generate the components and data access, it is necessary to add a database reference statement:

using System.Data;

using System.Data.OleDb;

In addition, we can change our own namespace naming myDB, also changed the class name myDbLink. Then we also define three properties, to make our code more generic:

public string sDbPath = ""; / / database path (including the database name)

public string sDbTable = ""; / / table name

public string sPassword = ""; / / database password

myDbLink category getData () method will return the view query form. After we complete custom code is 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;

)

)

)

getData () method in the meaning of the code as follows: First of all, the statement quoted the three types of variables oConn (Object Database Connectivity), oAdp (data adapter object), oDtSt (dataset object). And then instantiate database connection object and open database connection oConn; through SQL statement to generate the data adapter object oAdp; then oAdp access to the Fill method to fill data to a data set of table objects oDtSt Table (automatically generated) in the. Finally, to return to the default view table table DefaultView.

We look forward to in order to generate the names of the components for myClass.dll files, you must click the menu "item" \ "ClassLibrary1 attribute" property page in the pop-up dialog box, the "assembly name" value was revised to "myClass" to determine the property page dialog window to close. And then "Run" menu under the "generation ClassLibrary1" command. At this time in the "My Documents" \ Visual Studio Projects \ bin \ Debug under myClass.dll to generate a document that we are about to use the database access components.



(B) the deployment of components

Generated in order to test our components, you can create a separate "ASP.NET Web Application" project, assuming that the project known as WebApplication2, this will generate a WebApplication2 in IIS virtual path, at the same time under the default Web site to generate a folder WebApplication2 . We are about to test the components will be copied to the bin under the WebApplication2 folder. Such components on the deployment.



(C) ASP.NET test code

The generated project WebApplication2 drag and drop a DataGrid on the WebForm components, in the works to add a reference to myClass.dll components, and then in the paper quoted the head of our namespace:

using myDB;

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

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 database table

DataGrid1.DataSource = oDbTable.getData ();

DataGrid1.DataBind ();

)

Due to out from the database as the table view of data sources and DataGrid1 binding DataGrid1, so after running WebApplication2 project, DataGrid1 shown on the data in Table myTab.


.Net Control and components Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.