using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Select();
}
private void Select()
{
string sqlcon = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlConnection conn = new SqlConnection(sqlcon);
conn.Open();
string sql = "select top 3 * from 抽奖结果 order by newid()";
string sql2="";
SqlDataAdapter ap = new SqlDataAdapter(sql, conn);
DataTable ds=new DataTable ();
ap.Fill(ds);
conn.Close();
//创建xml
XmlDocument xml = new XmlDocument();
XmlElement element = xml.CreateElement("xml");
element.SetAttribute("Version", "2.0");//添加属性
element.SetAttribute("encoding", "utf-8");
element.SetAttribute("xmlns:media", "http://search.yahoo.com/mrss/");
for (int i = 0; i < ds.Rows.Count; i++)
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Open();
}
else if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string sql3 = "select count(*) from dbo.得奖用户 where code=" + ds.Rows[i][0];//如果当前的奖项已经被抽中,则不会在被抽取
SqlCommand an = new SqlCommand(sql3,conn);
if (Convert.ToInt32(an.ExecuteScalar()) <= 0)
{
sql2 += "insert into 得奖用户 values ('" + ds.Rows[i][0].ToString().Trim() + "');";
XmlElement node = xml.CreateElement("tt" + ds.Rows[i][0].ToString());
node.SetAttribute("name", "txt" + ds.Rows[i][0].ToString());
node.InnerText = ds.Rows[i][0].ToString();
element.AppendChild(node);
}
}
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Open();
}
else if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand command = new SqlCommand(sql2, conn);
command.ExecuteNonQuery();//将当前的抽奖结果记录下来
xml.AppendChild(element);
xml.Save(Server.MapPath("~/code.xml"));
Response.Redirect("~/code.xml");
}
}