Objective: to learn to read a database
Some netizens say that C # is not supported. NET or select VB. NET, but I don't think that since it is learning new things, don't worry about how much is new. In fact, C # is generally case sensitive and has a semicolon (;), which is similar to VB. NET. In ASP, we can directly use RECORDSET to read data, and then insert <% = %> in HTML code to display the data, but ASP. NET focuses on code separation. If the data is displayed in this way, code separation is not allowed. Therefore, we have two Methods: To read a record or a small amount of data, we can use DATAREADER to collect data and assign the value to the Text attribute of the LABEL control; to read a large amount of data, we use the DATAGRID.
Today let's talk about DATAREADER:
String strConnection = "Provider = Microsoft. Jet. OleDb.4.0; Data Source = ";
StrConnection + = Server. MapPath (strDb );
OleDbConnection objConnection = new OleDbConnection (strConnection );
OleDbCommand objCommand = new OleDbCommand ("here is the SQL statement", objConnection );
ObjConnection. Open ();
OleDbDataReader objDataReader = objCommand. ExecuteReader ();
If (objDataReader. Read ())
{
Oicq. Text = Convert. ToString (objDataReader ["useroicq"]);
Homesite. Text = Convert. ToString (objDataReader ["usersite"]);
Face. SelectedItem. Text = Convert. ToString (objDataReader ["userface"]);
}
We can see that we connect to the database and then open it. For the select command, we declare an OleDbCommand to execute it, And then declare an OleDbDataReader to read data, executeReader () and objDataReader are used. read () starts to Read data. when outputting data, note that the Text attribute only accepts strings. Therefore, we need to convert all the Read data into strings.
Conversion variable type function:
Convert to string: Convert. ToString ()
Convert to a number: Convert. ToInt64 (), Convert. ToInt32 (), Convert. ToInt16 () is based on the number of digits from grow to short
Convert to date: Convert. ToDateTime ()
------------------
Dim objConnection as OleDbConnection
Dim objCommand as OleDbCommand
Dim objDataReader as OleDbDataReader
ObjConnection = new OleDbConnection (Provider = Microsoft. Jet. OleDb.4.0; Data Source = "+ Server. MapPath (strDb ))
ObjCommand = new OleDbCommand ("here is the SQL statement", objConnection)
ObjConnection. Open ()
ObjDataReader = objCommand. ExecuteReader ()
If objDataReader. Read ()
Oicq. Text = Convert. ToString (objDataReader ["useroicq"])
Homesite. Text = Convert. ToString (objDataReader ["usersite"])
Face. SelectedItem. Text = Convert. ToString (objDataReader ["userface"])
End if
In fact, when you compare the C # and VB syntaxes, you will find that it is easier to use C #. I will focus on C # In the tutorial. I will not explain the VB code.
The following describes how to change the code to read the SQL database.
1. Start the code
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. OleDb" %>
Change
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. SqlClient" %>
2. It is so easy to change OleDb ×× in the code to SQL ××.
You can even replace all of them to solve the problem, so I will not list the SQL Server code in the future.
Let's talk about how to add, delete, and modify data tomorrow.