Use Asp.net to generate an RSS file so that the Rss reader can subscribe to it.
The environment is as follows:
A: database environment:
SQLServer2005 (database instance: Machine name + SQLServer2005)
B: Development Environment
Vs2005.net
Database
Create table Tb_Test (ID int identity (100), Title varchar (20), Description varchar (), AddDate datetime default (getdate ()))
Go
Insert into tb_test (Title, Description)
Select 'China news ', 'xinhua News Agency'
Union all
Select 'Central news ', 'CCTV'
Union all
Select 'International News', 'International broadcast'
Union all
Select 'Global news 'and 'Global broadcast'
Union all
Select 'phoenix news ', 'phoenix TV'
Go
Web. config
<? Xml version = "1.0"?>
<! --
Note: In addition to manually editing this file, you can also use
Web management tools to configure application settings. You can use
"Website"-> "Asp. Net configuration" option.
The complete list of settings and comments is displayed in
In machine. config. comments, this file is usually located in
In WindowsMicrosoft. NetFrameworkv2.xConfig
-->
<Configuration>
<Deleetask>
<Add key = "Db_cfgConn" value = "server =. SQLSERVER2005; database = Test_DB; uid = sa; pwd = ylwtsmt"/>
</AppSettings>
<ConnectionStrings/>
<System. web>
<! --
Set compilation debug = "true" to insert the debugging symbol
Compiled pages. However, this
Performance is affected, so this value is only available during development.
Set to true.
-->
<Compilation debug = "true"/>
<! --
In the <authentication> section, you can configure
Security Authentication mode,
To identify the user.
-->
<Authentication mode = "Windows"/>
<! --
If an unprocessed error occurs during request execution,
You can configure the corresponding processing steps in the <mermerrors> section. Specifically,
This section allows developers to configure
Html error page to be displayed
To replace the error stack trace.
<CustomErrors mode = "RemoteOnly" defaultRedirect = "GenericErrorPage.htm">
<Error statusCode = "403" redirect = "NoAccess.htm"/>
<Error statusCode = "404" redirect = "FileNotFound.htm"/>
</CustomErrors>
-->
</System. web>
</Configuration>
VS.net code:
GetRSS code:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. IO;
Public class GetRss
{
/// <Summary>
/// Write the RSS File Based on the file path
/// </Summary>
/// <Remarks>
/// Example:
/// WriteRss ("D: Vs2005GenerateRssRSS_Folder est_tb.xml", "test_tb ")
/// Note: Adjust the generated RSS feed Based on the table structure. This instance is only for testing.
/// </Remarks>
/// <Param name = "pathfilename"> file path </param>
/// <Param name = "tablename"> table name </param>
/// <Returns> true or false </returns>
Public static bool WriteRss (string pathfilename, string tablename)
{
Try
{
FileInfo finfo = new FileInfo (pathfilename );
Using (FileStream fs = finfo. OpenWrite ())
{
StreamWriter sw = new StreamWriter (fs, System. Text. Encoding. GetEncoding ("UTF-8 "));
Sw. WriteLine (GetRss. GetRSSString (tablename ));
Sw. Flush ();
Sw. Close ();
}
Return true;
}
Catch (System. Exception ex)
{
System. Web. HttpContext. Current. Response. Write (ex. Message );
Return false;
Throw;
}
}
/// <Summary>
/// Organize RSS strings that meet the latest standards
/// Parameter: Table name.
/// </Summary>
/// <Remarks>
/// Example:
/// GetRSS ()
/// Note: Adjust the generated RSS feed Based on the table structure. This instance is only for testing.
/// </Remarks>
/// <Param name = "tablename"> table name </param>
/// <Returns> return a DataSet data source </returns>
Public static string GetRSSString (string Tablename)
{
Try
{
DataSet ds = GetRSSData (Tablename );
String strRSS = "";
StrRSS = strRSS + "<? Xml version = "1.0"?> "+ System. Environment. NewLine;
StrRSS = strRSS + "<rss xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "" + System. Environment. NewLine;
StrRSS = strRSS + "xmlns: xsd =" http://www.w3.org/2001/XMLSchema "+ System. Environment. NewLine;
StrRSS = strRSS + "xmlns: slash =" http://purl.org/rss/1.0/modules/slash/ "version =" 2.0 ">" + System. Environment. NewLine;
StrRSS = strRSS + "<channel>" + System. Environment. NewLine;
StrRSS = strRSS + "<title> subscription title </title>" + System. Environment. NewLine;
StrRSS = strRSS + "<link> http: // www. ** com.cn/</link>" + System. Environment. NewLine;
StrRSS = strRSS + "<description> description </description>" + System. Environment. NewLine;
StrRSS = strRSS + "<language> zh-CN </language>" + System. Environment. NewLine;
StrRSS = strRSS + "<generator> www. ** com.cn </generator>" + System. Environment. NewLine;
StrRSS = strRSS + "<copyright> Beijing ** company </copyright>" + System. Environment. NewLine;
For (int I = 0; I <ds. Tables [0]. Rows. Count; I ++)
{
StrRSS = strRSS + "<item>" + System. Environment. NewLine;
StrRSS = strRSS + "<title>" + ds. Tables [0]. Rows [I] ["Title"] + "</title>" + System. Environment. NewLine;
StrRSS = strRSS + "<link> http: // www. ** .com.cn/ArticleShow@ "+ ds. tables [0]. rows [I] ["ID"] + ". html </link> "+ System. environment. newLine;
StrRSS = strRSS + "<author/>" + System. Environment. NewLine;
StrRSS = strRSS + "<guid> http: // www. ** .com.cn/ArticleShow@ "+ ds. tables [0]. rows [I] ["ID"] + ". html </guid> "+ System. environment. newLine;
StrRSS = strRSS + "<pubDate>" + Convert. toDateTime (ds. tables [0]. rows [I] ["AddDate"]. toString ()). toString ("yyyy-MM-dd HH: mm") + "</pubDate>" + System. environment. newLine;
StrRSS = strRSS + "<comments> http: // www. ** .com.cn/ArticleShow@ "+ ds. tables [0]. rows [I] ["ID"] + ". html </comments> "+ System. environment. newLine;
StrRSS = strRSS + "<slash: comments> 0 </slash: comments>" + System. Environment. NewLine;
StrRSS = strRSS + "<source url =" http: // www. ** .com.cn/ArticleShow@ "+ ds. tables [0]. rows [I] ["ID"] + ". html ">" + ds. tables [0]. rows [I] ["Title"] + "</source>" + System. environment. newLine;
StrRSS = strRSS + "<description>" + ds. Tables [0]. Rows [I] ["Description"] + "</description>" + System. Environment. NewLine;
StrRSS = strRSS + "</item>" + System. Environment. NewLine;
}
StrRSS = strRSS + "</channel>" + System. Environment. NewLine;
StrRSS = strRSS + "</rss>" + System. Environment. NewLine;
Return strRSS;
}
Catch (Exception ex)
{
System. Web. HttpContext. Current. Response. Write (ex. Message );
Throw;
}
}
/// <Summary>
/// Obtain the RSS Data Source
/// Parameter: Table name.
/// </Summary>
/// <Remarks>
/// Example:
/// DataSet ds = GetRSSData (TableName)
/// Note: Adjust the generated RSS feed Based on the table structure. This instance is only for testing.
/// </Remarks>
/// <Param name = "Tablename"> table name </param>
/// <Returns> return a DataSet data source </returns>
Public static DataSet GetRSSData (string Tablename)
{
String DBConnStr = System. Configuration. ConfigurationManager. etettings ["Db_cfgConn"];
System. Data. SqlClient. SqlDataAdapter DataAdapter = new System. Data. SqlClient. SqlDataAdapter ();
System. Data. SqlClient. SqlConnection Connection = new System. Data. SqlClient. SqlConnection (DBConnStr );
If (Connection. State! = ConnectionState. Open)
{
Connection. Open ();
}
Try
{
System. data. sqlClient. sqlCommand Command = new System. data. sqlClient. sqlCommand ("SELECT @ COLUMNS = @ COLUMNS + ',' + name from sys. columns where OBJECT_ID = OBJECT_ID (@ TABLENAME) order by column_id SET @ COLUMNS = 'select' + STUFF (@ COLUMNS, 1, 1 ,'') + 'from' + @ TABLENAME + ''' EXEC (@ COLUMNS) ", Connection );
Command. CommandType = CommandType. Text;
Command. Parameters. Add ("@ TABLENAME", SqlDbType. VarChar, 20 );
Command. Parameters ["@ TABLENAME"]. Value = Tablename;
Command. Parameters. Add ("@ COLUMNS", SqlDbType. VarChar, 2000 );
Command. Parameters ["@ COLUMNS"]. Value = "";
Command. ExecuteNonQuery ();
DataAdapter. SelectCommand = Command;
DataSet = new DataSet ();
If (DataSet! = Null)
{
DataAdapter. Fill (DataSet, "table ");
}
Return DataSet;
}
Catch (System. Exception ex)
{
System. Web. HttpContext. Current. Response. Write (ex. Message );
Connection. Close ();
Throw;
}
}
}
Front-end code (Note: There is no other html code on the html page)
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String Tablename = "Tb_Test"; // Request. QueryString ["TableName"];
String fname = System. Web. HttpContext. Current. Server. MapPath ("RSS_Folder") + "\" + Tablename + ". XML ";
GetRss. WriteRss (fname, Tablename );
}
}
This code can generate an Xml file named after a table in the (RSS_Folder) Directory of the current program.
You can subscribe to the RSS service in other RSS readers. (Foxmail 6.0 supports RSS subscription)