|
In the previous section, we briefly talked about the application of the database, and we didn't specify how the execution statement was used for display. Oh, is to stay in this section of the talk. Here we have to look at the Datareader,dataset and DataGrid controls, the DataGrid is the display control, we're looking at its related applications, we don't shiver here, DataReader and datasets can be used to store data content, But DataReader can only store the results of the query, the dataset is relatively more complex, his function is very powerful, we step by step, this section is not finished, we only talk about its storage database functions, hehe, in order to and DataReader explain. Speaking of the database, the first useful data to do, I have a random to do a database, named company, and added a few data into
DataReader
DataReader See the name, is to read the data, we can through the command of the Execute method, will get the data into the Datareader,datareader there are many methods and properties, commonly used is read, I don't want to talk about it here either. Let's take a look at its specific application first.
<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.SQL"%>
<script language= "C #" runat= "Server" >
public void Page_Load (Object Src,eventargs E)
{ file://Fixed statement
String myselectquery = "SELECT * from Company";
String myconnstring= "SERVER=LOCALHOST;UID=SA;PWD=123456;DATABASE=ASPCN";
file://Union knot
SqlConnection myconnection = new SqlConnection (myConnString);
SQLCommand mycommand = new SQLCommand (myselectquery,myconnection);
Myconnection.open (); file:// The DataReader of the fixed meaning
SqlDataReader MyDataReader; file:// and DataReader with the result
Mycommand.execute (out MyDataReader);
file://will DataReader tied to the DataGrid
Show. Datasource=mydatareader;
Show. DataBind ();
file://shut
Mydatareader.close ();
Myconnection.close ();
}
</script>
<title></title>
<body>
<asp:datagrid id= "Show" Runat=server headerstyle-backcolor= "#aaaadd"/>
</body>
In which we see the definition of DataReader
SqlDataReader MyDataReader;
and his assignment.
Mycommand.execute (out MyDataReader);
After these two steps are completed, we store the results of the execution into the DataReader.
Finally, we bind it to the DataGrid control, and if we don't understand what the bundle is, look at the article in this case.
| Id |
Name |
Age |
Sex |
Wage |
| 1 |
Flying |
20 |
Man |
1400 |
| 2 |
Tom |
23 |
Man |
5000 |
| 3 |
John doe |
47 |
Man |
7786 |
| 4 |
Harry |
20 |
Man |
6788 |
| 5 |
Miaocuihua |
30 |
Woman |
45676 |
| 6 |
Fangshiyu |
20 |
Man |
4656 |
Also tell you that if we need to get DataReader to store the value of a particular field, we can use a similar method mydatareader["FieldName" to get the following is a simple example
.......
while (Mydatareader.read ())
{
Response.Write ("<tr>");
Response.Write ("<td>" +mydatareader["Name"). tostring+ "</td>");
Response.Write ("</tr>");
}
.......
DataSet
Wow, this is a difficult thing to understand for beginners. What's the best way to say it? We can think of the dataset as a non-linked recordset (this should be familiar to everyone). The dataset is stored with data, and this is like a database with tables, columns (Columns), associations (relationships), constraints (constrains), and data. This is not the same as the database, but he is not a database (how the more confused), we can first in the dataset to manipulate the data (add, delete, update), and finally submitted to the database to operate. And the data in the dataset not only can be from the database, but also can be XML and other data, and even user input can be directly stored in the dataset
We only talk about the dataset for the role of data, hehe, but also just the display of data. Other applications, in the next section, let's take a look at some examples first.
<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Data.SQL"%>
<script language= "C #" runat= "Server" >
public void Page_Load (Object Src,eventargs E)
{ file://Fixed statement
String myselectquery = "SELECT * from Company";
String myconnstring= "SERVER=LOCALHOST;UID=SA;PWD=123456;DATABASE=ASPCN";
file://Union knot
SqlConnection myconnection = new SqlConnection (myConnString);
Sqldatasetcommand Mydatasetcommand = new Sqldatasetcommand (myselectquery,myconnection);
DataSet myDataSet = new DataSet ();
file://The results into a dataset
Mydatasetcommand.filldataset (myDataSet, "Company");
Show. Datasource=mydataset.tables["Company"]. DefaultView;
Show. DataBind ();
}
</script>
<title></title>
<body>
<asp:datagrid id= "Show" Runat=server headerstyle-backcolor= "#aaaadd"/>
</body>
The result shown above is the same as the previous one, so let's explain the code
Here we use the DataSetCommand, we just need to pass its FillDataSet method, the query table company's results into the dataset, the specific operation method is:
Mydatasetcommand.filldataset (myDataSet, "Company");
The future is tied up, there is nothing to say. Just to be aware, the dataset is its DataView and the DataGrid bind, and that's why I've said it in the previous article. Do not understand everyone can turn to look at the front, a lot of look at the source program.
In addition, we can add multiple tables to a dataset query results, these results can be different connection, this implementation is very simple, you can do it yourself.
|