ASP. NET Web Matrix is the latest ASP. NET programming tool released by Microsoft. It has comprehensive functions and is easy to use and flexible. Now let's take a look at how to use this tool to design a webpage that integrates data.
ASP. NET programming tool ASP. NET Web Matrix I. Create a database
ASP. NET Web Matrix now supports Microsoft SQL Server and a built-in simple desktop database MSDE. This simple database is similar to the individual version of SQL Server and can be downloaded for free. To illustrate the use of this database, we first download this free database tool from the Microsoft website.
ASP. NET Web Matrix "data" panel is no database figure 1), in ASP. NET Web Matrix, when a user clicks the "Data" tab, ASP. NET Web Matrix will give a prompt asking for Database Installation
At this time, we can install the SQL desktop database or download microsoft's MSDE database for free. After the installation is complete, you can create a data table in the local database or remote SQL database server, and then perform specific operations on the data through ASP. NET.
After MSDE is installed, click the "Data" Panel of ASP. NET Web Matrix to create a database.
Because the security of database creation and operation is the same as that of SQL Server, we will not detail the specific operations here. Here, we create a database named Orders
After the above settings, ASP. NET Web Matrix can perform database operations smoothly. We will perform operations on the above databases and data tables in the following programs. If you are using SQL Server, you can directly connect to a remote database by installing the desktop SQL Server locally. Generally, you can directly use Microsoft MSDE for personal applications or learning to use it, after all, the price and Management of SQL Serer are not easily achieved by individual users.
ASP. NET programming tool ASP. NET Web Matrix 2. use ASP. NET to operate databases
After the database is created, we can perform database operations. Below we will introduce database operations in several types, and strive to fully introduce the essence of ASP. NET Web Matrix database operations.
1. Simple data publishing
We know that the simplest and most basic database operation is the release of data table information. Here, we will first publish the Orders table information of the Orders database to the web page through ASP. NET.
In ASP. NET Web Matrix, the MXDataGrid server control is a table control that displays data from data sources such as databases. We can bind the control to the data source to display data when designing or running the program. Here, the MXDataGrid server control is used to display data.
1) in ASP. NET Web Matrix, select "File"> "New" to display the "New File" dialog box;
2) In the displayed dialog box, select the General Panel;
3) Select "ASP. NET Page ";
4) in the "Location" input box, enter the new file path;
5) enter the new file name "myFirstDataGrid. aspx" in the "FileName" input box ";
6) in the Language drop-down box, select Visual Basic as the ASP. NET development Language;
7) Click "OK;
8) in this way, the file "myFirstDataGrid. aspx" will appear in the "Design" view of ASP. NET Web Matrix.
9) Select the "Data" option page in the work window;
10) Click the database icon at the top of the options page to display the database selection interface. Here we select the local "Orders" database.
11) return to the work window, select the "Orders" table, and left-click the table to drag it to the ASP. NET page;
12) in this way, in the design view, we will see that the SQLDataSourceControl and MXDataGrid controls appear on the ASP. NET page.
Here, we should pay attention to the following points: first, ASP. NET Web Matrix will automatically set the attribute of sqlcececontrol for the database specified by the tool; second, ASP. NET Web Matrix will automatically bind MXDataGrid to SQLDataSourceControl;
13) Save and run this file. The myFirstDataGrid we just designed. aspx is automatically displayed in the browser. This page contains all the data in the Orders table. If there are more than 10 rows of data in the database, mxDataGrid displays 10 rows on each page and automatically pagination the data. A Page Link is displayed in the page;
Through the above operations, the content of the data table Ordrs is automatically published on the webpage. the above steps seem a lot. In fact, the specific design will be very simple. In this simple program, we create an automatic paging data release page without writing a line of code.
2. format the MXDataGrid
The default style of MxDataGrid is used to publish the data of the Data Table Orders. Although this style is good, it is obviously impossible for us to use this default setting for a long time to display the data, in this section, we will learn how to format MXDataGrid.
1) Open the "myFirstDataGrid. aspx" We just designed ";
2) Select the MxDataGrid control;
3) In the Properties window, select the AllowPaging attribute;
4) set the "AllowPaging" attribute of MxDataGrid to "False". This attribute sets whether MxDataGrid has the data paging function. Here we set it to "False"
5) then, select the "Fields" attribute in the "Properties" window;
6) The "Collection Editor" dialog box appears here. We will set some attributes related to the display of MXDataGrid data fields;
7) Change "OrderID" of the "HeaderText" attribute to "number ";
8) Click "OK" to save and run the page;
9) through these settings, we can see that the original OrderID column header in MXDataGrid has been changed to "Number"
3. Use Data Code Builder
Obviously, the database functions described above can only be a small part of our actual development work. To design well-developed database applications, we need more powerful functions. Code Builder can meet this requirement. It is designed based on the Code Generator to help users design some common functional modules. ASP. NET Web Matrix has some Code Builder, including some Code Builder specially designed for database design. In the following article, we will introduce how to use Select Code Builder to design a simple database search function. Now let's implement this function step by step.
1) create a new ASP. NET page. Set the language to VB and the file name to myFirstDataPage. aspx;
2) In the design view, select a TextBox, A DataGrid, and a Button Control from the "Web Control" option page to the design page. Note that, here we select the "DataGrid" control, not the "MXDatGrid" control;
3) The above is the Design of the "Design" Design view. Now, click the "Code" tab and select the Code view;
4) on the left of the Code view, we can see the "Toolbox" column, where there is the Code Builder tab. On the Code Builder tab, we Select "Select" and drag it to the design area; we can see that there is actually more than "Select", there are other "Insert", "Delete", and so on, that is, basically common database operations, you can use the methods described here.
5) at this time, the "Connect Database" dialog box appears. Here we select the "Orders" Database in the "LocalHost" server;
6) at this time, the Code Builder Wizard "Code Builder Wizard" named "Construct Select Query" "create database selection" appears ")
7) Select the data table to be queried from the "Tables" on the left of the interface. Here, select the "Orders" data table. In the "Columns" on the right, select the data column to be queried, check the data column to be queried. to query All data columns, select "*" or click "Slect All". Here, select "*". At this time, we can see that some "Select" statements "SELECT [Orders] appear in the" Preview "section of the above interface. * FROM [Orders] ", we can see that it is a standard Select query statement;
8) Click the "Where" button on the above interface to display the "Where Clause Builder" interface for setting the "Where" statement. This interface consists of two parts: left and right, and left with the Where statement equal to sign, on the right side is the right side of Where statement equal to number. In the "Columns" column on the left, we select "OrderID". In the "Filter" field on the right side, we enter "@ OrderID ", this setting means to query all data records whose "OrderID" is equal to the variable "OrderID". Click "OK" and return to the preceding interface. We can see that the Select statement has changed: SELECT [Orders]. * FROM [Orders] WHERE ([Orders]. [OrderID] = @ OrderID );
9) on the "Construct Select Query" page, click "Next" to go to the Next Code Builder Wizard, which is the "Preview" of the Query statement ";
10) on the "Query Preview" page, click "Text Query" to check the previously designed Query statement. A dialog box for setting the value of @ OrderID appears, here we set it to "1"; here it means to select the data row with OrderId 1;
11) Click the "OK" button on the above page to view the query result.
12) We can see that the test results of the query statement are correct. Click "Next". The third step of Code Builder Wizard appears, we need to set the query function name and the query method. Here, we can select "DataSet" and "DataReader" to implement data query;
13) on the above page, we select "DataSet" to query data and click "Finish" to end the Wizard. At this time, we can see that a function has been added to the "Code" view, this is the "MyQueryMethod" function we just created through the wizard;
- Function MyQueryMethod(ByVal orderID As Integer) As System.Data.DataSet
- Dim connectionString As String = "server='localhost'; trusted_connection=true; Database='Orders'"
- Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)
-
- Dim queryString As String = "SELECT [Orders].* FROM [Orders] WHERE ([Orders].[OrderID] = @OrderID)"
- Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
-
- sqlCommand.Parameters.Add("@OrderID", System.Data.SqlDbType.Int).Value = orderID
-
- Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
- Dim dataSet As System.Data.DataSet = New System.Data.DataSet
- dataAdapter.Fill(dataSet)
-
- Return dataSet
- End Function
14) Click the "Design" tab to return to the Design view. Double-click the "Button" We added to go to the "Code" Code Design window;
15) Add the following button Event code in the code design view:
- Sub Button1_Click(sender As Object, e As EventArgs)
-
- DataGrid1.DataSource =MyQueryMethod(CInt(TextBox1.Text))
- DataGrid1.DataBind()
-
- End Sub
The introduction of ASP. NET Web Matrix, an ASP. NET programming tool, is here. We hope that you can understand the basic information of ASP. NET Web Matrix.
- Step 11 of ASP. NET Programming Model Page Lifecycle
- Interface Design Standards for ASP. NET Programming specifications
- Brief Analysis on naming rules of ASP. NET Programming specifications
- Analysis of ASP. NET programming standards and Their encoding specifications
- Test Specification Analysis of ASP. NET Programming specifications