平常我們使用的TeachSamples資料庫中有一個bbc_country表,現在要求使用Ajax技術做一個應用:製作一個html頁面,能夠無重新整理的反應bbc_country表中的資料變化,包括反映每條記錄的添加、刪除、修改,還要能實現根據不同的條件,無重新整理的在html頁面中顯示不同的查詢結果。
具體的例子,可以參見 http://www.koonsoft.net/samples/ajax/bbc_country.html,在點擊按鈕後請注意看該頁面的源檔案內容是否發生了變化。
請注意上面的樣本並不完善:
1、這個例子本身的資料來源於 http://www.koonsoft.net/samples/ajax/bbc_country.xml,但這個資料來源 是不會變化的,並沒有達到反映資料變化的要求
2、這個例子沒有實現按Region不同來重新載入資料。
如何從資料庫中讀出資料並產生XML,我提供了一個C#控制台應用程式,供大家參考:
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";
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();
}
}
}