Using VS.NET to create. NET components reusable database
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:
/ / / <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:
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:
public class myDbLink
public string sDbPath = "";
public string sDbTable = "";
public string sPassword = "";
public DataView getData ()
oConn = new OleDbConnection ( "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + sDbPath + "; Password =" + sPassword +";");
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:
And then in the Page_Load event WebForm1.aspx.cs type the following code:
private void Page_Load (object sender, System.EventArgs e)
oDbTable = new myDbLink ();
oDbTable.sDbPath = "D: \ \ _My_Documents \ \ database.mdb";
oDbTable.sPassword = "";
oDbTable.sDbTable = "myTab"; / / myTab is a database table
DataGrid1.DataSource = oDbTable.getData ();
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
- Published Activex
- Prepared to use ActiveX Controls C # (c)
- Prepared to use ActiveX Controls C # (b)
- Prepared to use ActiveX Controls C # (a)
- C # in the use of DirectSound audio
- Visual C # to create and use ActiveX components
- NUnit in the plug-in VS.NET 2003.
- A paging control
- Real grid similar to Excel Control
- Visual Studio.NET in the use of custom plug-ins to maximize your productivity (a)
- Visual Studio.NET in the use of custom plug-ins to maximize your productivity (b)
- Visual Studio.NET in the use of custom plug-ins to maximize your productivity (c)
- Using VS.NET to create. NET components reusable database
- Can teach you a confirmation dialog pop-up custom Web server control ConfirmButton
- WebBrowser control capture DHTML events
- Introduced with a free data display / entry / update / delete function in asp.net control
- DataGrid control (a)
- Blog to your personalized calendar control to create
- . NET client applications:. NET applications updating components (2)
Can't Find What You're Looking For?
Rating: Not yet rated