从数据库里提取生成XML

      .NET 2005-9-1 12:59
<%@ Page language="c#" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>

<%
Response.ContentType="text/xml";
Response.ContentEncoding=Encoding.GetEncoding(936);
%>
<script language="C#" runat="server" >
System.Data.IDataReader read=null;
String ex_str;
string sql_str;
int count=0;

void Page_Load(Object sender, EventArgs E) {

if(Request.QueryString["no"]!=null)
{
sql_str="SELECT quest.* FROM quest WHERE (((quest.[no])="+Request.QueryString["no"]+"))";
count=1;
}else{
sql_str="SELECT * FROM quest";
count=get_count();
}

read=get_read(sql_str);

}
int get_count()
{
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source='"+Server.MapPath("a21.mdb")+"'";
System.Data.IDbConnection dbConnection = new System.Data.OleDb.OleDbConnection(connectionString);
System.Data.IDataReader dataReader=null;

string queryString = "SELECT count(*) FROM quest";
System.Data.IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
try
{
dbConnection.Open();
dataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

}
catch(Exception ex)
{
ex_str=ex.Message;
return 0;
}
dataReader.Read();
int temp=dataReader.GetInt32(0);
if(temp<=0)
{
ex_str="数据库里没有习题!";
return 0;
}
return temp;

}
System.Data.IDataReader get_read(string sql_str)
{
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source='"+Server.MapPath("a21.mdb")+"'";
System.Data.IDbConnection dbConnection = new System.Data.OleDb.OleDbConnection(connectionString);
System.Data.IDataReader dataReader=null;

string queryString = sql_str;
System.Data.IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;

try
{
dbConnection.Open();
dataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

}
catch(Exception ex)
{
ex_str=ex.Message.Trim();
}
return dataReader;
}

</script>


<?xml version="1.0" encoding="GB2312"?>
<!--检测是否有错误-->
<%if(ex_str==null)
{
%>
<售票系统习题 状态="0" 习题数="<%=count%>">
<%
while(read.Read())
{
%>
<习题>
<编号><%=read.GetInt32(0)%></编号>
<问题><![CDATA[<%=(read.GetString(1)).Trim()%>]]></问题>
<回答1><![CDATA[<%=(read.GetString(2)).Trim()%>]]></回答1>
<回答2><![CDATA[<%=(read.GetString(3)).Trim()%>]]></回答2>
<回答3><![CDATA[<%=(read.GetString(4)).Trim()%>]]></回答3>
<回答4><![CDATA[<%=(read.GetString(5)).Trim()%>]]></回答4>
<选择><%=read.GetInt32(6)%></选择>
</习题>
<%
}
if(read!=null)read.Close();
%>
</售票系统习题>
<!--如果有错误,把状态设置为1-->
<%
}else
{
%>
<售票系统习题 状态="1" 习题数="<%=count%>">
<%=ex_str%>
</售票系统习题>
<%
}
%>
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}