Asp.net reads the database

Source: Internet
Author: User
Tags homesite

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 thenCodeInterspersed with <% = %> can be displayed, but ASP. NET focuses on code separation. If it still displays data like this, 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.

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 ××, the object stated in the Code, to SQL ××
you can even replace all the objects to solve the problem.

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.