Author: Flying
We must learn about DataReader, Dataset, and DataGrid controls. DataGrid is a display control. Everyone has to look at it related applications, we are here, DataReader and DataSet can be used to store data content, but DataReader can only store queries. As a result, Dataset is relatively complicated. His function is very powerful. Let's take a step in step, we only tell the function of the storage database, huh, in order to explain with DataReader. Tell the database, first useful data Only, I am here casually, named Company, add a few data into it.
DataReader
DataReader looks at the name, it is to read the data, we can use the Command's Execute method to save the data into DataReader, DataReader has many methods and properties, often read, here I don't want to talk more. We are still Take a look at its specific application.
<% @ Page language = "c #"%>
<% @ Import namespace = "system.data"%>
<% @ Import namespace = "system.data.sql"%>
Public void Page_Load (Object SRC, Eventargs E)
{
// Define statement
String myselectQuery = "select * from company";
String myconnstring = "server = localhost; uid = sa; pwd = 123456; database = aspcn";
//coupling
SqlConnection MyConnection = New SqlConnection;
Sqlcommand mycommand = new sqlcommand (MySelectQuery, MyConnection);
MyConnection.open ();
/ / Define DataReader
SqlDataReader MyDataReader;
// During the result of DataReader
MyCommand.execute (Out MyDataRead);
// Bundle DataReader with DataGrid
Show.datasource = MyDataReader;
Show.databind ();
//shut down
MyDataReader.Close ();
MyConnection.Close ();
}
script>
hEAD>
body>
html>
In these we see DataReader's definition
SqlDataReader MyDataReader;
And his assignment
MyCommand.execute (Out MyDataRead);
After completing these two steps, we store the resulting results into the DataReader. Finally, we bundle it (bind) to the DataGrid control, if you don't understand what is bundled, see the article of this example.
ID Name Age SEX WAGE
1 flying knife 20 male 1400
2 three 23 men 5000
3 Li Si 47 Men 7786
4 king five 20 male 6788
5 Miao Cuihua 30 Female 45676
6 Fang Shiyu 20 Men 4656
Also tell you, if we need to get the value of the DataReader storage specific field, you can use myDataReader ["FieldName"] to get the following is a simple example.
.......
While (MyDataReader.Read ())
{
Response.write ("
Response.write ("
Response.write (" TR>");
}
.......
DataSet
Wow, this is a very difficult situation for beginners. How do you say it? We can see Dataset as a unpignated recordset (this should be familiar). DataSet is stored in data, and this is like a database, there is a table (tables), columns, relationship, Constrains and data. Some of this and the database are nothing, but he is not a database (how to say more, we can act in DataSet (add, delete, update), and finally submit it to the database. Moreover, the data in the DataSet is not light from the database, and it can be XML and other data, and even the user's input can be stored directly into DataSet.
We only talk about DataSet for data role, huh, but also just data display. Other applications, then say, let's take a look at a picture first.
<% @ Page language = "c #"%>
<% @ Import namespace = "system.data"%>
<% @ Import namespace = "system.data.sql"%>
Public void Page_Load (Object SRC, Eventargs E)
{
// Define statement
String myselectQuery = "select * from company";
String myconnstring = "server = localhost; uid = sa; pwd = 123456; database = aspcn";
//coupling
SqlConnection MyConnection = New SqlConnection;
SqlDataSetCommand myDataSetCommand = New SqlDataSetCommand (MySelectQuery, MyConnection);
DataSet mydataset = new dataset (); // Save the result into the DataSet
MyDataSetCommand.FillDataSet (MyDataSet, "Company");
Show.DataSource = mydataset.tables ["company"]. defaultview;
Show.databind ();
}
script>
hEAD>
body>
html>
The results of the above display are the same as before, let's explain the code.
Here we used DataSetCommand, we need to save the results of the query table Company to DataSet through its FillDataSet method, and the specific method of operation is:
MyDataSetCommand.FillDataSet (MyDataSet, "Company");
There is a bundle, there is nothing to say. Just pay attention to it, Dataset is its DataView with DataGrid, this reason I have already said in the previous article. You don't understand, you can turn to your front and look at the source program.
In addition, we can add multiple tables of query to a DataSet. These results can be different. This implementation is very simple. You can do it since yourself.