The server carries data introduction
Data visit is the core content that the program uses in real world. Asp.net offerred to accuse richly, he and CLR (universal language runs a library) the offerred APIs that uses administrative data visit (applied process interface) close together union. This chapter discharges a few DateGrid that use Asp.net repeatedly to accuse an example that will bind calm SQL to inquire result and XML data file beforehand. This chapter assumes learner is familiar with database foundation and SQL inquiry language.
Wincheer notes: Discharge beforehand (this word does not know Walk Through) this how accurate interpreter, his means is in computer programming, undertake having constituent discussion inside a group, a process that so that be opposite,the logic of computer program has dogging checking.
The server carries data the visit is more distinctive, its reason depends on Web page do not have voice. This is brought about in try to execute routine, when if insert,be being recorded newlier perhaps, appeared the challenge that a few have difficulty. You will be in no less than this chapter sees, dataGrid accuses can help government these challenges, allow you to condense more applied process logic, and reduce the detail of incident processing and state government.
Connections (join) , Commands (command) , with Datasets (data set)
Universal language runs hour (CLR) the APIs that offerred one a complete set of to manage data to visit, with will enhance data application process to develop an environment. Obtain and these applied process interfaces are filled with consistent pattern fill data, and no matter what real data source is (SQL Server, OLEDB, XML, etc) 3 the most commonly used objects are Connections, commands, with Datasets.
Connection states the physics that stores to data joins, join for example SQL Server or XML file.
Command expresses to obtain (Select) perhaps operate (Insert, update, the command that Delete) data stores.
Dataset expresses to use the real data that program in order to works. Noticing Datasets always is the data source join with them and data model depart, and can be revised by independence. Nevertheless, undertake modification to Dataset, can coordinate very easily with primitive data model consistent.
The more detail problem that about hour government running in universal language data visits, consult ADO.NET is summarized please.
The visit is based on the data of SQL
Applied process often needs to carry out one or more Select to SQL database, insert, update, or Delete statement. The form below revealed the code of a few give typical examples that implement these functions.
| It is quotative content below: Function Citing —————————————————————— Inquire simply | SELECT * From Employees WHERE FirstName = ‘Bradley’; —————————————————————— Associated inquiry | SELECT * From Employees E, managers M WHERE E.FirstName = M.FirstName; —————————————————————— Insert | INSERT Into Employees VALUES (’123-45-6789′ , ‘Bradley’ , ‘Millington’ , ‘Program Manager’); —————————————————————— Newer | UPDATE Employees SET Title = ‘Development Lead’ WHERE FirstName = ‘Bradley’; —————————————————————— Delete | DELETE From Employees WHERE Productivity<10; —————————————————————— |
The page that wants to let you can visit SQL database, system.Data and System.Data.SqlClient name space must introduce in the page:
<%@ Import Namespace= "System.Data" %>
<%@ Import Namespace= "System.Data.SqlClient" %>
To carry out Select inquiry from SQL database, you need to build a join to arrive through join string the SqlConnection object of the database, construct next a SqlDataAdapter target that contains inquiry statement. To use inquiry return result fill to arrive DataSet object, need calls the Fill method of SqlDataAdapter.
| It is quotative content below: 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, “Authors” ); |
No less than mentions before this chapter in that way, the advantage of use Dataset depended on offerring a detached database view. You can operate Dataset in using a program, agree your modification and actual database next. To the applied program that runs for long, because avoided to read access often to occupy a source, it is best processing method consequently. To Web application process, often use brief operation (it is simple indication data only normally) the request that will handle a client carry. Below this kind of circumstance, we can use SqlDataReader to replace DataSet object.
When SqlDataReader object offerred a kind to obtain data from Sql database, only forward, read-only finger. Because SqlDataReader object uses form type data to flow (TDS) read access to occupy from database join directly, so, below the condition that allows to use, dataSet of his executive efficiency prep above.
Use SqlDataReader object when, need uses SqlCommand to replace SqlDataAdapter. SqlCommand uses ExecuteReader method to get SqlDataReader boy or girl friend. When the attention is using SqlCommand, must be opened explicitly and shut SqlConnection. After calling ExecuteReader method, sqlDataReader object can occupy a source to bind as number accuse to ASP.NET server surely. Next bar will demonstrate this kind of circumstance.
| It is quotative content below: SqlConnection MyConnection = New SqlConnection(”server=(local)\NetSDK;database=pubs;Trusted_Connection=yes” ); SqlCommand MyCommand = New SqlCommand(”select * From Authors” , myConnection);MyConnection.Open(); SqlDataReader Dr = MyCommand.ExecuteReader(); . . . MyConnection.Close(); |
Should execute a SQL order that does not need to return data, be like Inserts, updates, with Deletes, also use SqlCommand. This command is carried out through calling ExecuteNonQuery method, return the linage that handles actually. When the attention uses SqlCommand, must open join explicitly; And SqlDataAdapter is automatic open join.
| It is quotative content below: SqlConnection MyConnection = New SqlConnection(”server=(local)\NetSDK;database=pubs;Trusted_Connection=yes” ); SqlCommand MyCommand = New SqlCommand( “UPDATE Authors SET Phone=’(800) 555-5555′ WHERE Au_id = ‘123-45-6789′” , MyConnection);MyCommand.Connection.Open();MyCommand.ExecuteNonQuery();MyCommand.Connection.Close(); |
Important clew:
Bind SQL data decide DataGrid
The example below was revealed a simple inquiry the statement is bound accuse to DataGrid surely. DataGrid consign a list that includes Sql data.
Similar data binds the DropDownList that decides Na Yizhang to mention, dataGrid accuses a supportive Ienumerable or the DataSource attribute of Icollection type, as DataSet same. You can be passed will (include in DataSet) the DefaultView attribute of the watch assigns to want to use (in DataSet) the name of the watch, will use DataSet to accuse, defaultView attribute represents the state that expresses currently in DataSet, any changes that include place of applied process code to make (the change) that be deleted all right for example or is worth. After installing DataSource attribute, can call DataBind() to come fill accuses.
| It is quotative content below: MyDataGrid.DataSource=ds.Tables[”Authors” ].DefaultView; MyDataGrid.DataBind(); |
Another identical grammar is to appoint DataSource and DataMember at the same time. In this exemple, ASP.NET was gotten automatically for you obtained DefaultView.
| It is quotative content below: MyDataGrid.DataSource=ds; MyDataGrid.DataMember= “Authors” ; MyDataGrid.DataBind(); |
You also can be bound directly decide SqlDataReader. If you show data merely, so character of only forward ” suits the ” of SqlDataReader very much this kind of circumstance, at the same time you can get higher executive performance.
Attention: S of neon of Yi chrysanthemum busy whizs branny Zheng cheeks Mei of scabbard A Chinese-style unlined garment mu this kind of data visits border 荄 AtaSet mode will demonstrate; Actually, these example can use SQLDataReader to rescript as much.
Carry out with parameter denotive Select commands
You also can use SqlDataAdapter object to carry out the Select sentence that carries parameter. The example below demonstrates how to use Select HtmlControl to accuse a delivered cost to change the outcome of inquiry.
SqlDataAdapter includes Parameters gather, can use variable identifier (a “@” ) is added to replace a cost before the name. The name that you can give this gather to increase a new SqlParameter to appoint parameter, type, with size, set the value of his Value attribute next.
| It is quotative content below: MyCommand.SelectCommand.Parameters.Add(new SqlParameter(”@State” , sqlDbType.NVarChar, 2));MyCommand.SelectCommand.Parameters[”@State” ].Value = MySelect.Value; |
Important clew: The setting of EnableViewState attribute default that notes DataGrid is False. If be filled when every time page requests,fill data, not was necessary to let DataGrid save the condition message that watch single shot sends. As a result of the data of all state that DataGrid saves it to include, because this shuts EnableViewState,can raise a page execute function.
The data of the fill list casing of the example static state above. If the value in the database had produced change, this kind of method can not work very well. Because list casing also supports IEnumerable DataSource attribute, so you can use Select inquiry to come the value of dynamic fill list casing, can make sure database and user interface always agree so. The example below demonstrated this process.
Data is inserted in SQL database
To group is inserted to record in the database, you can increase an input to express sheet on the page, the incident that refers in watch sheet next sentence one is carried out to insert a command in the handle. Resemble above two case are same, you are the jussive of parameter gather fill that uses command target value. Inserting data before, the cost that notices the examination ensures from the watch sheet gets cannot is empty, the accident that can prevent database field to restrain a condition so is wrong. In be being expressed to prevent data advocate index and the record that are about to insert repeat, can use Try/catch statement piece will carry out insert a command.
Besides clear examination data-in, you also can be used in front paragraphic mentioned test and verify accuses will check data input. The example below was revealed to you how to use accomplish this. Attention criterion expression test and verify accuses a convenience when checking the field such as author Id, zip code and telephone number.
