ASP. NET databases have many advantages in use, which have won wide favor from programmers. Now, let's take a look at the ASP. NET database application.
For a skilled ASP developer, the application of ASP. NET database is not only simple, but also powerful. Many Web developers chose ASP because of this. So far, they have always believed that ASP. NET database functions are good and can fully meet the development requirements.
I. NameSpace
To use the functions of the ASP. NET database, you cannot leave NameSpace. What is namespace? A little simpler. namespace is like a control in Delphi. You must put them in your Form to use them. Similarly, if you want to use ASP. NET database, you must first reference the corresponding namespace. The namespace of the ASP. NET database includes the following:
- <% @ 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"
- 'Create DataSet
- SqlCmd = New SQLDataSetCommand (strsql, conn)
- 'Add table Products to DataSet
- SqlCmd. FillDataset (ds,"Products")
-
- ReturnDs. Tables ("Products"). DefaultView
- End Function
-
- 'Bind a data Function
- Sub BindGrid ()
- DataGrid1.DataSource = CreateDataSource ()
- DataGrid1.DataBind ()
- End Sub
-
- 'Page Logon
- Sub Page_Load (Source AS Object, e as EventArgs)
- CreatedataSource ()
- BindGrid ()
- End Sub
- </Script>
-
- <Html>
-
- <Head>
- <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 usage </title>
- </Head>
-
- <Body>
- <Form RunAt ="Server">
- <ASP: DataGrid id ="Maid"RunAt ="Server"/>
- </Form>
- </Body>
-
- </Html>
The preceding is the simplest ASP. NET database application. The unique function is to list all records in the Products table of the NorthWind database. We can compare it with ASP and find that this program is so simple. To better understand the basic framework of the database application, let's take a look at how this database application is implemented.
When logging on to the page, call the CreateDataSource and BindGrid functions. The CreateDataSource function connects to the database and establishes DataView. The BindGrid function binds the data to the DataGrid. The specific statements of these two functions are described in the Code.