/// <Summary>
/// Use this method to output strings Based on the template
/// </Summary>
/// <Param name = "template"> </param>
/// <Param name = "dt"> </param>
/// <Returns> </returns>
Public static string Render (string template, DataTable dt)
{
StringBuilder sb = new StringBuilder (1000 );
List <string> arr = new List <string> ();
List <string> columns = new List <string> ();
Int previusendindex = 0;
// Find {xxx}
Int startIndex = template. IndexOf ('{');
Int endIndex = template. IndexOf ('}');
While (startIndex! =-1)
{
// Store the string between the last} and the currently searched {. If {is searched for the first time, previousEndIndex = 0 stores the string between the start string and the first {.
Arr. Add (template. Substring (previousEndIndex, startIndex-previusendindex ));
// Store the column name
Columns. Add (template. Substring (startIndex + 1, endIndex-startIndex-1 ));
StartIndex ++;
EndIndex ++;
Previusendindex = endIndex;
StartIndex = template. IndexOf ('{', startIndex );
EndIndex = template. IndexOf ('}', endIndex );
}
// If the template does not end with}, there is a string
If (previusendindex <template. Length)
{
Arr. Add (template. Substring (previusendindex ));
}
// Method Execution here, arr. Length = columns. Length or arr. Length = columns. Length + 1
Int I;
Foreach (DataRow row in dt. Rows)
{
I = 0;
For (; I <columns. Count; I ++)
{
Sb. Append (arr [I]);
Sb. Append (row [columns [I]. ToString ());
}
If (I <arr. Count)
{
Sb. Append (arr [I]);
}
}
Return sb. ToString ();
}
/// <Summary>
/// Use regular expression Matching Based on the template and then replace the output string
/// </Summary>
/// <Param name = "template"> </param>
/// <Param name = "dt"> </param>
/// <Returns> </returns>
Public static string RenderTemplate (string template, DataTable dt)
{
StringBuilder sb = new StringBuilder (1000 );
MatchCollection matches = reg. Matches (template );
String rowStr = template;
Foreach (DataRow row in dt. Rows)
{
RowStr = template;
Foreach (Match match in matches)
{
RowStr = rowStr. Replace (match. Value, row [match. Groups [1]. Value]. ToString ());
}
Sb. Append (rowStr );
Sb. Append ("\ n ");
}
Return sb. ToString ();
}
Create table links
(
Siteurls varchar (100 ),
Sitename varchar (100)
)
Declare @ I int
Set @ I = 0
While (@ I <100000)
Begin
Insert links values ('HTTP: // www.baidu.com ', 'Baidu ')
Set @ I = @ I + 1
End
First:
The background code is as follows:
Public override void ProcessRequest (HttpContext context)
{
Stopwatch watch = new Stopwatch ();
Watch. Start ();
Base. ProcessRequest (context );
Watch. Stop ();
HttpContext. Current. Response. Write (watch. Elapsed. ToString ());
}
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Rpt. DataSource = GetTable ();
Rpt. DataBind ();
}
}
Public DataTable GetTable ()
{
Using (SqlConnection conn = new SqlConnection ("Data Source = wh189 \ sqlexpress; Initial Catalog = A; User ID = sa; Password = sa "))
{
DataTable dt = new DataTable ();
SqlDataAdapter sda = new SqlDataAdapter ("select top 50000 * from dbo. links", conn );
Sda. Fill (dt );
Return dt;
}
}
The front-end code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "TestWeb. _ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div style = "display: none;">
<Asp: Repeater ID = "rpt" runat = "server">
<ItemTemplate>
<A href = '<% # (System. Data. DataRowView) Container. DataItem) ["siteurl"] %>'>
<% # (System. Data. DataRowView) Container. DataItem) ["sitename"] %> </a>
</ItemTemplate>
</Asp: Repeater>
</Div>
<Asp: Button ID = "Button1" runat = "server" Text = "Button"/>
</Form>
</Body>
</Html>
The time is about 1 second. If System. Data. DataRowView is not used and Eval is used, the time is about 1.2 seconds.
Second:
The front-end code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "TestWeb. _ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div style = "display: none;">
<% System. Data. DataTable dt = GetTable ();
Foreach (System. Data. DataRow dr in dt. Rows)
{%>
<A href = '<% = dr ["siteurl"] %>'>
<% = Dr ["sitename"] %> </a>
<%}
%>
</Div>
<Asp: Button ID = "Button1" runat = "server" Text = "Button"/>
</Form>
</Body>
</Html>
The background code is as follows:
Public override void ProcessRequest (HttpContext context)
{
Stopwatch watch = new Stopwatch ();
Watch. Start ();
Base. ProcessRequest (context );
Watch. Stop ();
HttpContext. Current. Response. Write (watch. Elapsed. ToString ());
}
Protected void Page_Load (object sender, EventArgs e)
{
}
Public DataTable GetTable ()
{
Using (SqlConnection conn = new SqlConnection ("Data Source = wh189 \ sqlexpress; Initial Catalog = A; User ID = sa; Password = sa "))
{
DataTable dt = new DataTable ();
SqlDataAdapter sda = new SqlDataAdapter ("select top 50000 * from dbo. links", conn );
Sda. Fill (dt );
Return dt;
}
}
The time is about 0.2 seconds.
Third:
The front-end code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "TestWeb. _ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div style = "display: none;">
<% = Tool. Render ("<a href = '{siteurl}'> {sitename} </a>", GetTable () %>
</Div>
<Asp: Button ID = "Button1" runat = "server" Text = "Button"/>
</Form>
</Body>
</Html>
The background code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data;
Using System. Collections;
Using System. Data. SqlClient;
Using System. Diagnostics;
Using System. Text;
Using System. Text. RegularExpressions;
Namespace TestWeb
{
Public partial class _ Default: System. Web. UI. Page
{
Public override void ProcessRequest (HttpContext context)
{
Stopwatch watch = new Stopwatch ();
Watch. Start ();
Base. ProcessRequest (context );
Watch. Stop ();
HttpContext. Current. Response. Write (watch. Elapsed. ToString ());
}
Protected void Page_Load (object sender, EventArgs e)
{
}
Public DataTable GetTable ()
{
Using (SqlConnection conn = new SqlConnection ("Data Source = wh189 \ sqlexpress; Initial Catalog = A; User ID = sa; Password = sa "))
{
DataTable dt = new DataTable ();
SqlDataAdapter sda = new SqlDataAdapter ("select top 50000 * from dbo. links", conn );
Sda. Fill (dt );
Return dt;
}
}
}
}
/// <Summary>
/// Summary of the Tool
/// </Summary>
Public class Tool
{
/// <Summary>
/// Use this method to output strings Based on the template
/// </Summary>
/// <Param name = "template"> </param>
/// <Param name = "dt"> </param>
/// <Returns> </returns>
Public static string Render (string template, DataTable dt)
{
StringBuilder sb = new StringBuilder (1000 );
List <string> arr = new List <string> ();
List <string> columns = new List <string> ();
Int previusendindex = 0;
// Find {xxx}
Int startIndex = template. IndexOf ('{');
Int endIndex = template. IndexOf ('}');
While (startIndex! =-1)
{
// Store the string between the last} and the currently searched {. If {is searched for the first time, previousEndIndex = 0 stores the string between the start string and the first {.
Arr. Add (template. Substring (previousEndIndex, startIndex-previusendindex ));
// Store the column name
Columns. Add (template. Substring (startIndex + 1, endIndex-startIndex-1 ));
StartIndex ++;
EndIndex ++;
Previusendindex = endIndex;
StartIndex = template. IndexOf ('{', startIndex );
EndIndex = template. IndexOf ('}', endIndex );
}
// If the template does not end with}, there is a string
If (previusendindex <template. Length)
{
Arr. Add (template. Substring (previusendindex ));
}
// Method Execution here, arr. Length = columns. Length or arr. Length = columns. Length + 1
Int I;
Foreach (DataRow row in dt. Rows)
{
I = 0;
For (; I <columns. Count; I ++)
{
Sb. Append (arr [I]);
Sb. Append (row [columns [I]. ToString ());
}
If (I <arr. Count)
{
Sb. Append (arr [I]);
}
}
Return sb. ToString ();
}
}
The time is about 0.2 seconds.
If you increase the query data volume to 0.1 million, it is found that the second method is several seconds faster than the third method. However, if the Render method in the third method is improved as follows, the speed of the two methods is almost the same:
Public static string Render (string template, DataTable dt)
{
// Use HttpResponse for direct output instead of StringBuilder
HttpResponse res = HttpContext. Current. Response;
List <string> arr = new List <string> ();
List <string> columns = new List <string> ();
Int previusendindex = 0;
// Find {xxx}
Int startIndex = template. IndexOf ('{');
Int endIndex = template. IndexOf ('}');
While (startIndex! =-1)
{
// Store the string between the last} and the currently searched {. If {is searched for the first time, previousEndIndex = 0 stores the string between the start string and the first {.
Arr. Add (template. Substring (previousEndIndex, startIndex-previusendindex ));
// Store the column name
Columns. Add (template. Substring (startIndex + 1, endIndex-startIndex-1 ));
StartIndex ++;
EndIndex ++;
Previusendindex = endIndex;
StartIndex = template. IndexOf ('{', startIndex );
EndIndex = template. IndexOf ('}', endIndex );
}
// If the template does not end with}, there is a string
If (previusendindex <template. Length)
{
Arr. Add (template. Substring (previusendindex ));
}
// Method Execution here, arr. Length = columns. Length or arr. Length = columns. Length + 1
Int I;
Foreach (DataRow row in dt. Rows)
{
I = 0;
For (; I <columns. Count; I ++)
{
Res. Write (arr [I]);
Res. Write (row [columns [I]. ToString ());
}
If (I <arr. Count)
{
Res. Write (arr [I]);
}
}
Return "";
}