When the page is loaded, it is required to display all channel names in the database in the list of channels, which is the display function of the RSS channel. This section mainly implements this feature.
Designing ways to read the contents of a database
Read the contents of the database, first of all to design the method of reading data. The method in this example is best added to the Rssmanager management class. The steps to implement the method are as follows:
(1) Open the "Rssmanager" management class file under the App_Code directory.
(2) Add the method "Getrssname" to the class, primarily by reading the channel name from the database and saving the returned results in an array. Note When you use the array "ArrayList", you must reference the namespace "using System.Collections;". The actual implementation code is shown in Listing 12-8.
Code listing 12-8 ways to get data from a database
public string[] Getrssname ()
{
//Initializing SQL command
SqlCommand cmd = new SqlCommand ();
Executes the SQL command within a scope, releasing all objects
using (SqlConnection conn = new SqlConnection (connectionString))
After the scope has ended {
//To determine the database connection state
if (conn). State!= ConnectionState.Open)
Conn. Open ();
Cmd. Connection = conn;
//define command properties
Cmd.commandtype = CommandType.Text;
Cmd.commandtext = "Select Rss_name,rss_url from Rssstore";
//execute command and return data
SqlDataReader rdr = cmd. ExecuteReader (commandbehavior.closeconnection);
//define an array to return
ArrayList myarray = new ArrayList ();
The loop determines whether there is data
while rdr. Read ())
{
//If there is data, it is stored in an array, where the data is separated by a comma between
MyArray. ADD (RDR. GetString (0) + "," + rdr. GetString (1));
}
Return myarray
}
}
(3) According to "Ctrl+s" to save the design of the code.