asp.net| Data | database
Some netizens say, will not C #, in asp.net inside or choose vb.net, this I do not object, but I think since is learning new things, don't care how much is new. In fact, C # in general operation, only to distinguish between case and a semicolon this two points and vb.net different from the other is similar AH. In the ASP we read the data directly with the recordset, and then in the HTML code interspersed <%=%> can be displayed, but ASP.net is fastidious is the code separation, if still this display data can not code separation. So we have two methods: if you are reading a recorded data or not much data, we use DataReader to collect the data, then assign the value to the Label control's Text property, and if we read a lot of data, we use the DataGrid.
Today we're going to say 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 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"]);
}
You can see that we first connect to the database and then open, for the Select command, we declare a OleDbCommand to execute, and then declare a OleDbDataReader to read the data, using ExecuteReader (), Objdatareader.read () began to read, in the output of the time we should note that the Text property can accept only strings, so we want to read out of the data into a string to do.
Convert Variable type function:
Convert to String: convert.tostring ()
Convert to Number: Convert.toint64 (), Convert.ToInt32 (), Convert.ToInt16 () is based on number of digits from long 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, we compare the C # and VB syntax, will find in C # seems more simple, the tutorial I focus on C #, for VB code I do not explain.
Let's say that if the code is reading the SQL database, we can change it
1, the code to start the
<% @Import namespace= "System.Data"%>
<% @Import namespace= "System.Data.OleDb"%>
Amended to
<% @Import namespace= "System.Data"%>
<% @Import namespace= "System.Data.SqlClient"%>
2, the code inside all the declared object oledbxx into SQLXX is so simple
You can even solve it with a replacement, so I'm going to stop listing SQL Server code in the future.