Friday, March 19, 2010

Create webservice with access database and C#

Create webservice with access database and C#
<%@ WebHandler Language="C#" Class="rss" %>

using System;

using System.Web;

using System.Data.OleDb;

using System.Text;

using System.Xml;

public class rss : IHttpHandler {

public void ProcessRequest(HttpContext context)

{

string sTxt = BuildXmlString();

context.Response.ContentType = "text/xml";

context.Response.ContentEncoding = System.Text.Encoding.UTF8;

context.Response.Write(sTxt);

}

private string BuildXmlString()

{

string sTitle = "Super Value Deals and Deals of The Day";

string sSiteUrl = "http://www.cccc.com";

string sDescription = "Super Value Deals and Deals of The Day";

string sTTL = "60";

System.Text.StringBuilder oBuilder = new System.Text.StringBuilder();

oBuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

oBuilder.Append("<rss version=\"2.0\"><channel>");

oBuilder.Append("<title>");

oBuilder.Append(sTitle);

oBuilder.Append("</title>");

oBuilder.Append("<link>");

oBuilder.Append(sSiteUrl);

oBuilder.Append("</link>");

oBuilder.Append("<description>");

oBuilder.Append(sDescription);

oBuilder.Append("</description>");

oBuilder.Append("<ttl>");

oBuilder.Append(sTTL);

oBuilder.Append("</ttl>");

AppendItems(oBuilder);

oBuilder.Append("</channel></rss>");

return oBuilder.ToString();

}

public void AppendItems(System.Text.StringBuilder oBuilder)

{

string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=c:\\sites\\Axxx.mdb";

OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);

OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();

string ProdTy = "M2";

string Strsql = "SELECT top 101 * from DetailUrl where ProdType='M2'";

myOleDbCommand.CommandText = Strsql;

myOleDbConnection.Open();

OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();

myOleDbDataReader.Read();

while (myOleDbDataReader.Read())

{

string sTitle = "Super Value Deals and Deals of The Day";

string sLink = "http://www.visli.com/";

string sDescription = "Super Value Deals and Deals of The Day";

string sPubDate = "System.DateTime.Now";

oBuilder.Append("<item>");

oBuilder.Append("<title>");

oBuilder.Append(myOleDbDataReader["TextUrl"].ToString());

oBuilder.Append("</title>");

oBuilder.Append("<link>");

oBuilder.Append(myOleDbDataReader["NameUrl"].ToString());

oBuilder.Append("</link>");

oBuilder.Append("<description>");

oBuilder.Append("&lt;img src=" + myOleDbDataReader["ImageUrl"].ToString() + "&gt;" + "&lt;br&gt;" + myOleDbDataReader["Price"].ToString() + "&lt;br&gt;visit &lt;a href=http://www.visli.com&gt;http://www.visli.com&lt;/a&gt;");

oBuilder.Append("</description>");

oBuilder.Append("<pubDate>");

oBuilder.Append(myOleDbDataReader["UpdDate"].ToString());

oBuilder.Append("</pubDate>");

oBuilder.Append("</item>");

}

}

public bool IsReusable {

get {

return false;

}

}

}

No comments: