The sixth day of the 10 day society asp.net

Source: Internet
Author: User
Tags homesite tostring
ASP.net learning Purpose: Learn to read a 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.

That's it for today, start tomorrow. Add, delete, and modify data.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.