"ASP. NET Quick Start" Study Notes

Source: Internet
Author: User
There are basically three types of commonly used objects: connection, command, and dataset.

    • A connection represents a physical connection to certain data storage areas (such as SQL Server or XML files.
    • Command indicates the command to search (select) from the data storage area or perform operations (insert, update, delete) on the data storage area.
    • Dataset indicates ApplicationProgramThe actual data used. Note: datasets are always disconnected from their source connection and data models and can be independent of each other.
      Modify. However, changes to a dataset can be easily coordinated with the initial data model.

The advantage of using a dataset is that it provides you with a disconnected database view. You can operate the dataset in the application, and then associate
Tune the changes and the actual database. This is usually the best method for long-running applications. For Web applications
Each request performs a short operation (usually only displays data ). Generally, you do not need to maintainDatasetObject. For this type
Situation, you can useSqldatareader.

SqldatareaderOnly forward read-only pointers are provided for data retrieved from the SQL database.To use sqldatareader, declare sqlcommand instead of sqldataadapter.SqlcommandPublic returnSqldatareaderOfExecutereaderMethod. Note that,When usingSqlcommandMust be explicitly opened and closedSqlconnection.

Important: Always remember to close the connection to the data model before the page is executed. If the connection is not closed, the connection limit may be inadvertently exceeded while the page instance is being reclaimed.

Server = (local); in this way, you can avoid Website access failures due to changes in the server IP address.

Common Database bindingCode 

Sqlconnection myconnection = new sqlconnection ("Server = (local) \ netsdk; database = pubs; trusted_connection = yes ");
Sqldataadapter mycommand = new sqldataadapter ("select * from authors", myconnection );

Dataset DS = new dataset ();
Mycommand. Fill (DS, "author ");

Mydatagrid. datasource = Ds. Tables ["author"]. defaultview;
Mydatagrid. databind ();

When you execute a command that does not require data to be returned (such as insert, update, or delete ),Sqlcommand. This command is calledExecutenonqueryMethod, and this method returns the number of affected rows

DefaultviewAttribute RepresentationDatasetThe current status of the table, including any changes made by the application code (for example, row deletion or value change ). SetDatasourceCallDatabind ()Fill control.

 
Mydatagrid. datasource = Ds. tables ["Authors"]. defaultview; mydatagrid. databind (); and mydatagrid. datasource = Ds; mydatagrid. datamember = "Authors"; mydatagrid. databind (); equivalent
 
Important:Note:DataGridOfEnableviewstateThe property has been setFalse. If
Fill in the data in the request to makeDataGridThe status information that will be sent by the form through the round-trip is not
Benefits. BecauseDataGridStore all of its data during maintenance. It is important to disable it when appropriate, so that you can
Improve page performance.
 
Execute parameterized SelectionCode
 string selectcmd = "select * from authors where State = @ State"; sqlconnection myconnection = new sqlconnection ("Server = (local) 
\ netsdk; database = pubs; trusted_connection = yes "); sqldataadapter mycommand = new sqldataadapter (selectcmd,
myconnection); mycommand. selectcommand. parameters. add (New sqlparameter
("@ State", sqldbtype. nvarchar, 2); mycommand. selectcommand. parameters ["@ State"]. value =
myselect. value; dataset DS = new dataset (); mycommand. fill (DS, "author"); mydatagrid. datasource = Ds. tables ["author"]. defaultview; mydatagrid. databind ();

A common requirement for any grid is to sort the data contained in it. AlthoughDataGridThe control does not explicitly sort its data, but it does provide a way to call the event handler when you click a column header that can be used to sort data. WhenDataGridOfAllowsortingSet propertyTrueIt willSortCommand to stimulate the hyperlink to the column header of the grid. SetDataGridOfOnsortcommandSet the attribute to the handler to be called when you click the column link. Column nameDatagridsortcommandeventargsParameterSortexpressionAttribute transfer. This parameter can be used to setDataviewOfSortAttribute. The following example shows the process.

 

  
        
        
allowsorting = "true" runat = "server"/>
  
        
        
allowsorting = "true" runat = "server"/>
<SCRIPT> protected function mydatagrid_sort (SRC: object, E: datagridsort
Commandeventargs): void {... vaR Source: dataview = Ds. tables ["Authors"]. defaultview; source. sort = E. sortexpression; mydatagrid. databind () ;}</SCRIPT> <form runat = "server"> <asp: DataGrid id = "mydatagrid" onsortcommand = "mydatagrid_sort"
Allowsorting = "true" runat = "server"/> </form>

 

As mentioned at the beginning of this section,DatasetABSTRACT Data is designed in a way independent from the actual data source. You can understand this by changing the focus of the example from SQL to XML.DatasetSupports addingFilestreamObject used as a parameterReadxmlMethod. In this case
The file to be read under must contain both the schema and data to be read.

You can also read data and architecture separately, and useDatasetOfReadxmldataAndReadxmlschemaMethod

  1. The managed data of the Common Language Runtime Library accesses the abstract data of the API and represents it in a consistent manner. It is irrelevant to the actual data source (such as SQL Server, oledb, and XML.
  2. To enable page access to the classes required for SQL data access, you mustSystem. DataAndSystem. Data. sqlclientThe namespace is imported to the page.
  3. Filling a dataset from an SQL query involves creatingSqlconnection, SetSqldataadapterThe connection between an object and a dataset containing query statements and the filling of a dataset from a command.
  4. DataGridControls supportedIenumerable(OrIcollection) TypeDatasourceAttribute. By assigningDatasetOfDefaultviewProperty (DataviewType), you can set this to the SQL query result.
  5. SqldataadapterMaintain a parameters parameter that can be used to replace the variable identifier (represented by "@" before the name) with a value.
    Set.
  6. UseSqlcommandInsteadSqldataadapter. This command is calledExecutenonqueryMethod, and this method returns the number of affected rows.
  7. UseSqlcommandMust be opened explicitlySqlconnection(SqldataadapterAutomatically opens a connection for you ). Always remember to closeSqlconnection. If the connection is not closed, the connection limit may be inadvertently exceeded while the page instance is being reclaimed by garbage collection.
  8. To allow row editing,DataGridInteger typeEdititemindexAttribute, which indicates which row of the grid should
    Is editable. After this attribute is set,DataGridThe rows are displayed as text input Boxes Based on the index, rather than simple labels.
  9. DataGridPublish a field name that can be set as the primary keyDatakeyfieldAttribute. ConnectUpdatecommandEvent
    In the processing program, you canDataGridThe key name of the datakeys set.
  10. UseDataGridInBoundcolumnControls allow you to fully control the column sequence and theirReadonlyAttribute.
  11. UseDataGridInTemplatecolumnControls allow you to have full control over column content.
  12. ButtoncolumnControls can be used to present only button controls in each row of the column, and the column can be associated with events.
  13. HyperlinkcolumnCan be addedDataGridColumns set, which can be located on another page when you click the link.
  14. WhenDataGridOfAllowsortingSet propertyTrueIt willSortCommand
    Hyperlink. SetDataGridOfOnsortcommandSet the attribute to the handler to be called when you click the column link.
  15. DatasetSupports addingFilestreamAs a parameterReadxml,ReadxmldataAndReadxmlschemaMethod, and this parameter can be used to fillDataset.
  16. Using Stored Procedures can reduce the overhead of performing heavy database operations in applications.

DatalistAndRepeaterAlso supportedDatasourceAttribute (Datasource) Can be set to anyIcollection,IenumerableOrIlistsourceType

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.