平常我们使用的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#控制台应用程序,供大家参考:
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();
}
}
}