private void srr_RssHeaderReceive(SimpleRssReader Sender, RssHeader Header) { System.Console.WriteLine("Header:" + Header.Link); System.Console.WriteLine("Header:" + Header.Title);
this.SaveToDataBase("SP_AddChannel",typeof(RssHeader),Header);
}
private void srr_RssItemReceive(SimpleRssReader Sender, RssItem Item) { System.Console.WriteLine("Item: " + Item.Title); System.Console.WriteLine("Item: " + Item.Link); System.Console.WriteLine("Item: " + Util.StripHTML(Item.Description));
this.SaveToDataBase("SP_AddChannelsDetails",typeof(RssItem),Item);
} private void SaveToDataBase(string sp, Type t,object instance) { //获取 sp 所有参数 SqlParameter[] spa = SqlHelperParameterCache.GetSpParameterSet(this.Connection, sp); System.Collections.Hashtable ht = new System.Collections.Hashtable();
for (int i = 0; i < spa.Length; i++) { //保存 参数名称与其位置(次序) 的关系 ht.Add(spa[i].ParameterName.ToLower().Replace("@", ""), i);
//相当于为存储过程的所有参数赋初值 spa[i].Value = null; }
//得到所有的属性 PropertyInfo[] pi = t.GetProperties(); foreach (PropertyInfo x in pi) { if (ht.ContainsKey( x.Name.ToLower())) { //根据参数(属性)名称得到参数的次序! int i = (int) ht[x.Name.ToLower()]; if (spa[i].Direction == System.Data.ParameterDirection.Input || spa[i].Direction == System.Data.ParameterDirection.InputOutput) { object o; if (x.PropertyType.Name == "String") { o = x.GetValue(instance,null); if (o != null) { string s = Util.StripHTML((string) o); o = s; } } else { o = x.GetValue(instance,null); }
spa[i].Value = o; } }
}
if (t == typeof(RssItem)) { spa[0].Value = ((RssItem) instance).Header.URL; }
SqlHelper.ExecuteNonQuery(this.Connection, CommandType.StoredProcedure, sp, spa); if (spa[spa.Length - 1].Value != System.DBNull.Value) { System.Console.WriteLine("Save to ID: {0} successful!", spa[spa.Length - 1].Value); } else { System.Console.WriteLine("save failed! may be duplicate!"); } } } }
//========================================================================================================== /* --sql Script if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SP_AddChannel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[SP_AddChannel] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SP_AddChannelsDetails]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[SP_AddChannelsDetails] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Channels]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[Channels] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ChannelsDetails]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) |