We usually use the teachsamples database to have a bbc_country table. Now we need to use Ajax technology for an application: To create an HTML page, we can refresh the data changes in the bbc_country table, including the addition, deletion, and modification of each record, and the display of different query results on the HTML page without refreshing based on different conditions.
For more information, see examples.
Note that the preceding example is not complete:
1. The data in this example comes from http://www.koonsoft.net/samples/ajax/bbc_country.xml, but this data source will not change and does not meet the requirements for reflecting data changes.
2. In this example, data is not reloaded according to region.
How to read data from the database and generate xml, I provide a C # console applicationProgramFor your reference:
Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Data;
Using System. Data. sqlclient;
Using System. IO;
Using System. text;
Using System. xml;
Namespace Getxml
{
Public Class Program
{
Static Void Main ( String [] ARGs)
{
Sqlconnection sqlconn = New Sqlconnection ();
Sqlconn. connectionstring = " Password = ******; persist Security info = true; user id = sa; initial catalog = teachsamples; Data Source = 127.0.0.1 " ;
sqlcommand sqlcmd = New sqlcommand ();
sqlcmd. commandtext = " select * From bbc_country order by name " ;< br> sqlcmd. connection = sqlconn;
Xmlwriter writer= Null;
Try
{
// Create an xmlwritersettings object with the correct options.
Xmlwritersettings settings = New Xmlwritersettings ();
Settings. indent = True ;
Settings. indentchars = ( " \ T " );
Settings. omitxmldeclaration = False ;
// Create the xmlwriter object and write some content.
Writer = Xmlwriter. Create ( " Bbc_country.xml " , Settings );
Writer. writestartdocument ();
Writer. writestartelement ( " Countries " );
Try
{
Sqlcmd. Connection. open ();
Sqldatareader sqlrd = Sqlcmd. executereader ();
While (Sqlrd. Read ())
{
Writer. writestartelement ( " Country " );
Writer. writestartelement ( " Name " );
Writer. writestring (sqlrd [ " Name " ]. Tostring ());
Writer. writeendelement ();
Writer. writestartelement ( " Region " );
Writer. writestring (sqlrd [ " Region " ]. Tostring ());
Writer. writeendelement ();
Writer. writestartelement ( " Area " );
Writer. writestring (sqlrd [ " Area " ]. Tostring ());
Writer. writeendelement ();
Writer. writestartelement ( " Population " );
Writer. writestring (sqlrd [ " Population " ]. Tostring ());
Writer. writeendelement ();
Writer. writestartelement ( " GDP " );
Writer. writestring (sqlrd [ " GDP " ]. Tostring ());
Writer. writeendelement ();
Writer. writeendelement ();
}
}
Finally
{
Sqlcmd. Connection. Close ();
}
Writer. writeendelement ();
Writer. writeenddocument ();
Writer. Flush ();
}
Finally
{
If (Writer ! = Null )
Writer. Close ();
}
}
}