Linq to SQL (1)


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in D:\InetPub\vhosts\kwu-1639.package\kennywu.info\wwwroot\wp-content\plugins\wp-syntax\wp-syntax.php on line 380

You can crate a Plain Old CLR Object to map the underlining database table w/o the help of the LINQ-to-SQL designer. To represent a customer class, you need the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Text;
 
namespace LinqToSql
{
    [Table(Name="Customers")]
    public class Customer
    {
        [Column]
        public string customerId {get; set;}
        [Column]
        public string companyName {get; set;}
        [Column]
        public string city {get; set;}
        [Column]
        public string country {get; set;}
    }
 
    class LinqToSql
    {
        static void Main(string[] args)
        {
            string connString = @"Server=.SQLEXPRESS; 
            Database=Northwind; Trusted_Connection=Yes";
            DataContext db = new DataContext(connString);
 
            // crate typed table
            Table<Customer> customers = db.GetTable<Customer>();            
            //query database
            var custs = from c in customers
                        where c.country =="USA"
                        select c;
            //display customers
            foreach (var cust in custs)
            {
                Console.WriteLine(
                    "{0} {1} {2} {3}", 
                    cust.customerId, cust.companyName, cust.city, cust.country
                    );
            }
            db = null;
        }
    }
}

Implement RSS news feed in .Net


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in D:\InetPub\vhosts\kwu-1639.package\kennywu.info\wwwroot\wp-content\plugins\wp-syntax\wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in D:\InetPub\vhosts\kwu-1639.package\kennywu.info\wwwroot\wp-content\plugins\wp-syntax\wp-syntax.php on line 380

There are many ready to use RSS wrappers on the net, the one from Web Service Share is easy to use and comes on top from Google search.
Download two DLL,

  • Wss.Common.dll
  • Wss.News.WebService.PcWorldNews.GetDataFeed.dll

from Class Library add them to the Bin folder. Add the following snippet to the aspx markup.

<asp:Literal ID="Literal1" runat="server" EnableViewState="False"></asp:Literal>

The code behind file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using Wss.Common;
using WebServices = Wss.News.WebServices;
using Rss = Wss.News.Rss20;
 
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int _rssNum = 10;
        Rss.Result rssResult = WebServices.PcWorldNews.GetDataFeed();        
        foreach (var _rss in GetRss.SelectRss(rssResult,   _rssNum))
        {
            Literal1.Text += String.Format("<a href="{1}">{0}</a><br /><hr />", 
                _rss.Title, _rss.Link);
        }        
    }
}
 
public struct aRss {
    public string Title { get; set; }
    public string Link { get; set; }    
    //public string Description { get; set; }    
}
static public class GetRss {    
 
    static public List<aRss> SelectRss(Rss.Result rssResult, int _rssNum)
    {        
        List<aRss> allRss = new List<aRss>();
        for (int i = 0; i < Math.Min(_rssNum, rssResult.Items.Count); i++)
        {
            allRss.Add(new aRss
            {
                Title = rssResult.Items[i].GetTitle(""),
                Link = rssResult.Items[i].GetLink(""),
                //Description = _item.GetDescription("")                
            }
            );
        }        
        return allRss;
    }
}

The class will return only the first 10 entries. The number is specified by the variable “_rssNum”.

How to Retrieve HTTP content in .NET


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in D:\InetPub\vhosts\kwu-1639.package\kennywu.info\wwwroot\wp-content\plugins\wp-syntax\wp-syntax.php on line 380

HttpWebRequest and HttpWebResponse greatly simplified http content retrial process. Code snippet below demonstrated how easy to use.

using System;
using System.Text;
using System.Net;
using System.IO;
 
static void Main(string[] args)
{
    string webUrl = "http://www.google.com";
    WebRequest req = WebRequest.Create(webUrl);
    req.Timeout = 10000;
    WebResponse resp = req.GetResponse();
    StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.ASCII);
    Console.WriteLine(reader.ReadToEnd());
    resp.Close(); // Close the response to free resources.
    reader.Close();
}

Simple and elegant. To look for more…Retrieving HTTP content in .NET