Asp. NET database application Guide

Source: Internet
Author: User
Tags bind execution modify prev sort microsoft frontpage
For a skilled ASP developer, ASP database application is not only simple, but also powerful. This is why many Web developers choose the ASP, so far, they have been thinking that the ASP database functionality is good enough to meet the development requirements. However, for a user who has just learned about ASP or is not very familiar with HTML, ASP database application is too much trouble: write the program when the ASP code interspersed between the HTML code, modification and detection is extremely inconvenient; write a program that doesn't feel like writing a traditional program. Data paging inconvenient, and do not understand why it is so cumbersome, almost every page of the direct number of data. These troubles, every ASP developer has ever experienced, then, now, ASP. NET learners, they will never face these, they can quickly develop database applications, and the program performance is much higher than before. Now, let's look at the ASP.net database application.

One: Name space namespace
To use ASP.NET database functionality, it is not possible to leave the use of namespace namespace. What is the name space, say theory can say half-day, we do not need to understand, simple, the name control is like the control in Delphi, you must put them into your form to use them, similarly, if you want to use ASP.net database function, you must first refer to the corresponding namespace. Asp. NET has these in the name space of the database:



The above mentioned ado+, it is the next generation of ADO, just like ASP.net is the next generation of ASP, compared to ado,ado+ has the following characteristics:
i) support XML;
(ii) better performance;
C. Convenient programming interface;

The specific use of the name space is as follows:
<%@ Import namespace= "name space"%>

Example:
<%@ Import namespace= "System.Data"%>

It should be noted that the above code must be at the top of the page.

Two: Basic concepts
Before using ASP.net, you must also understand some basic concepts:
ADOConnection: Equivalent to the database connection in ASP;
Adocommand: Equivalent to the database command in ASP;
DataView: The equivalent of an ASP ADO recordset Recordset;
DataSet: A collection of multiple data tables;

The above concept is really more abstract, if you do not understand now, it does not matter, now follow the use, will be gradually understood later.

Three: Database basic use Model
Look at a lot of concepts, now first look at an example (for convenience, directly using SQL Server with the Northwind database), this article for example, are based on this model, please be sure to master:

<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SQL"%>

<script language= "VB" runat= "Server" >
Function CreateDataSource ()
Dim Conn as SqlConnection
Dim ConnStr as String
Dim strSQL as String
Dim SQLCMD as Sqldatasetcommand
Dim DS as New DataSet
' Connect to the database
Connstr=
"Server=nhga-d36kq26twb;database=northwind; pwd=; Uid=sa "
conn= New SqlConnection (CONNSTR)
' SQL statement
Strsql= "Select * FROM Products"
' Set up a dataset
Sqlcmd=new Sqldatasetcommand (Strsql,conn)
' Add Table Products to DataSet
Sqlcmd.filldataset (ds, "products")

Return DS. Tables ("Products"). DefaultView
End Function

' Bind data function
Sub Bindgrid ()
Datagrid1.datasource=createdatasource ()
Datagrid1.databind ()
End Sub

' Page Login
Sub Page_Load (Source as object,e as EventArgs)
CreateDataSource ()
Bindgrid ()
End Sub
</Script>


<meta http-equiv= "Content-language" content= "ZH-CN" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<meta name= "generator" content= "Microsoft FrontPage 4.0" >
<meta name= "ProgId" content= "FrontPage.Editor.Document" >
<title>asp. NET database using </title>

<body>
<form runat= "Server" >
<asp:datagrid id= "DataGrid1" runat= "Server"/>
</Form>
</body>


Execute the above procedure, the effect is as follows:



(Program execution effect)


This is one of the simplest asp.net database applications, and the only function is to list all the records for the Northwind database Products table. Can compare with ASP, find this program is so simple. To better understand the basic framework of database applications, let's take a look at how this database application is implemented in particular.

When the page is logged in, the CreateDataSource and Bindgrid functions are invoked, the CreateDataSource function connects to the database, and the Dataview,bindgrid function is established to bind the data to the DataGrid. Specific statements about these two functions are described in the code.

Four: Basic database application
With the above template, ASP. NET database application has been the basic framework, now, we need to make some changes in this framework, it is more in line with our actual use requirements.

One) interface settings
If the data we present to our visitors all use the interface of the above template, it is also not. But
Who doesn't want their pages to look beautiful? Or the above code, just modify the DataGrid section, the database section unchanged. In the program:
<asp:datagrid id= "DataGrid1" runat= "Server"/>
Modified to:
<asp:datagrid id= "DataGrid1" runat= "Server"
Bordercolor= "BLACK"
Borderwidth= "1"
Cellpadding= "3"

Backimageurl= ""
Backcolor= "#FFCCCC"
Forecolor= "BLACK"

Headerstyle-backcolor= "#CCCCFF"
Headerstyle-forecolor= "Blue."

Alternatingitemstyle-backcolor= "#F3f3f3"
>
</ASP:DataGrid>
You will get the following effects:



(Program execution effect)

In this interface, we set the font, each row background, DataGrid background, and so on, we are now one by one analysis:
Bordercolor= "BLACK"
Borderwidth= "1"
Cellpadding= "3"
The above statement sets the DataGrid border to black, the border thickness to 1, and the cell span to 3;
Backimageurl= "Apictue.gif"
Backcolor= "#FFCCCC"
Forecolor= "BLACK"
The above statement sets the background image of the DataGrid to be apicture.gif; the background color is #ffffcc; the font color is black;
Headerstyle-backcolor= "#CCCCFF"
Headerstyle-forecolor= "Blue."
The above statement sets the header property of the DataGrid, the background color is #ccccff, and the font color is: blue;
Alternatingitemstyle-backcolor= "#F3f3f3"
The above statement sets the DataGrid each row alternating background color is f3f3f3; This statement in the ASP implementation is very cumbersome, here, a sentence to solve.

II) data Paging
ASP inside the data paging is really cumbersome, have to put a lot of energy to write code. Some programmers just because of this, if the data is not too much simply without paging, reluctantly cope. Now let's not worry about paging the data, add the code to the DataGrid setting:
Allowpaging= "True"
Pagesize= "5"
Pagerstyle-horizontalalign= "Right"
pagerstyle-nextpagetext= "Next page >>"
pagerstyle-prevpagetext= "<< prev"
The page effect is as follows:



(Program execution effect)

This is a standard data paging, is not more simple than the ASP? Let's take a closer look at how the code is:
Allowpaging= "True"
The above code allows the data to be paginated, false by default. So, if you want data paging, be sure to add this sentence;
Pagesize= "5"
PageSize, as in ASP, indicates how many records each page has;
Pagerstyle-horizontalalign= "Right"
The above statement indicates that the page break is to the right of the page's position.
pagerstyle-nextpagetext= "Next page >>"
pagerstyle-prevpagetext= "<< prev"
The above statement means to use the "Next" "page" as a page sign, if you do not want this, you want to use the number directly, remove these two sentences on it.
Attention:
and paging related to the PageCount property, you can get the total number of paging;

III) data sorting
Data sorting is not within the SQL statement can be set, why do you want to specialize in a sort of data? The data ordering of SQL statements can only be set on the server side, what if the user wants to sort according to their preferences? There is no solution in the ASP, now, we see how asp.net solve this problem.
Add a statement to the DataGrid settings:
Allowsorting= "true"
Onsortcommand= "Sort_grid"
The first sentence means that the DataGrid uses the order of requirements; The second inning indicates the event after the click Sort; let's take a look at this event:
Sub Sort_grid (Sender as object,e as DataGridSortCommandEventArgs)
Sortfield=e.sortfield
Datagrid1.datasource=createdatasource ()
Datagrid1.databind ()
End Sub
After clicking the sort, first, set the Sort field to click on the field and how to display the data again. The effect is as follows:



(Program execution effect)

As shown above, each field name is a LinkButton (connection), click on the field name, you can sort by this field. It should be noted that the sort does not sort the current page, but rather the entire DataGrid.

IV) Data Editing
Database operation of data browsing, most of the data is actually processing (add, modify, delete). Data processing, ASP. NET and ASP are not much different, we look at an example of adding data to understand the ASP.net data editing implementation.
Sub Insertrec ()
Dim Conn as SqlConnection
Dim ConnStr as String
Dim Sqlinsertcmd as SQLCommand
Dim Sqlinsert as String

Connstr= "Server=nhga-d36kq26twb;database=mybase; pwd=; Uid=sa "
conn= New SqlConnection (CONNSTR)

Sqlinsert= "Insert into MyTable (title,name,content) Values (@title, @name, @content)"
Sqlinsertcmd=new SQLCommand (Sqlinsert,conn)

SQLINSERTCMD.PARAMETERS.ADD (New SqlParameter ("@title", sqldatatype.varchar,20))
SQLINSERTCMD.PARAMETERS.ADD (New SqlParameter ("@name", sqldatatype.varchar,20))
SQLINSERTCMD.PARAMETERS.ADD (New SqlParameter ("@content", sqldatatype.varchar,20))

Sqlinsertcmd.parameters.item ("@title"). Value= "Test3"
Sqlinsertcmd.parameters.item ("@name"). Value= "Test3"
Sqlinsertcmd.parameters.item ("@content"). Value= "Test3"

Conn.Open ()
Sqlinsertcmd.execute ()

End Sub

In the above code, you first create a database connection and then create an INSERT statement that uses a parameter that starts with a @, which may differ slightly from the ASP. Before you can use parameters, you must create a parameter and describe the data type of the parameter. How to open the connection, you can execute the INSERT statement.

V: summary
Above we design to the ASP.net database operation commonly used part, basically may realize our general programming request. However, if you want to fully understand the ASP.net database features, the above is obviously not enough, which requires us to learn more about the other asp.net knowledge, mastery.


Related Article

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.