- 热门文章:
- · 从一个舆论调查的制作谈面向对象的编程思路(三)
- · 从一个舆论调查的制作谈面向对象的编程思路(五)
- · 在asp.net中使用组件,也包括import和asemble的区别
- · 从一个舆论调查的制作谈面向对象的编程思路(四)
- · 分别用DataGrid、Repeater、DataList绑定XML数据的例子
- · 用DataList 控制元件开发的一个简单的留言本程序:
- · .Net边学边讲(二)
- · .Net边学边讲(一)
- · 里面是对一个body的属性进行server的一些设定,不过可以衍生到其他的一些htmlcontrol新手看看,或者有点...
- · 一个实现自定义event的文章。。。我还没有完全摸透。。不知道有没人有兴趣。。新手就不用看了,先学会走...
- · Server.Transfer,Response.Redirect 和 Page.Navigate 的区别
- · ASP.NET发送ICQ消息DIY
上一篇:从一个舆论调查的制作谈面向对象的编程思路(一) >>
从一个舆论调查的制作谈面向对象的编程思路(二)
/*新闻调查表*/
if exists(select * from sysobjects where id = object_id(@#Survey@#))
drop table Survey
go
create table Survey
(
ID int identity Primary key not null ,
SurveyID varchar(20) default "" not null ,
Kind tinyint default 0 not null ,
Title nvarchar(100) default "" not null ,
Description nvarchar(255) default "" not null ,
Amount int default 0 not null ,
BeginTime datetime default getdate() not null ,
EndTime datetime default getdate() not null ,
Active bit default 0 not null
)
go
好了,数据库建好了,我可以从上面的survey基类中继承出具体的子类,比如说我现在要做一个同足球相
关的调查,那就做这么一个类:
namespace Football
{
using System;
using MyClass.Util ;
using System.Data.SQL ;
using System.Collections ;
/// <summary>
/// 足球舆论调查
/// </summary>
/// <remarks>
/// 从MyClass.Util.Survey类继承而来
/// </remarks>
public class FootballSurvey : MyClass.Util.Survey
{
public FootballSurvey()
{
}
/// <summary>
/// 重载父类虚函数
/// </summary>
/// <param name="a_intID">该调查的数据库id </param>
public override void LoadFromDatabase(string a_strID)
{
MyClass.Util.MyConnection myConn = new MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.CommandText = "up_GetSurvey" ;
myCommand.CommandType = System.Data.CommandType.StoredProcedure ;
try
{
myConn.Open() ;
myCommand.ActiveConnection = myConn ;
myCommand.Parameters.Add(new SQLParameter("@a_strsurveyid" ,
SQLDataType.VarChar , 20)) ;
myCommand.Parameters["@a_strsurveyid"].Value = a_strID ;
SQLDataReader myReader ;
myCommand.Execute(out myReader) ;
//先取调查
if (myReader.Read())
{
this.m_strTitle = myReader["title"].ToString() ;
this.m_intHits = (int)myReader["amount"] ;
this.m_strID = a_strID ;
this.m_datBeginTime =
(DateTime)myReader["begintime"] ;
this.m_datEndTime = (DateTime)myReader["endtime"] ;
}
else
{
throw(new Exception("数据库中无此调查的纪录")) ;
}
//清空调查项
m_arrItems.Clear() ;
//取调查项
if (myReader.HasMoreRows)
{
while(myReader.Read())
{
SurveyItem item = new SurveyItem() ;
item.Text = myReader["title"].ToString() ;
item.ID = (int)myReader["id"] ;
item.Count = (int)myReader["amount"] ;
item.Description =
myReader["Description"].ToString() ;
AddItem(item) ;
}
}
else
{
throw(new Exception("数据库中没有该调查相关的调查项
")) ;
}
//清场
myReader.Close() ;
myConn.Close() ;
}
catch(Exception e)
{
throw(new Exception("从数据库中读取调查失败:" +
e.ToString())) ;
}
}
/// <summary>
/// 将调查保存到数据库
/// </summary>
/// <param name="m_strSurveyID">调查编号 </param>
/// <remarks>
/// 如果m_strSurveyID不为空,则删除原纪录,用当前调查编号保存新的调查,
/// 否则就生成一个新的调查编号
/// </remarks>
public override void SaveToDatabase(string m_strSurveyID)
{
//如果没有标题或调查项则抛出异常
if (this.m_arrItems.Count == 0 || this.m_strTitle == "")
{
throw(new Exception("没有调查标题或标题项")) ;
}
MyClass.Util.MyConnection myConn = new MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.CommandType = System.Data.CommandType.Text ;
try
{
myConn.Open() ;
myCommand.ActiveConnection = myConn ;
//如果没有surveyid则生成surveyid
string strSurveyID ;
if(m_strSurveyID == "")
{
strSurveyID = DateTime.Now.Year.ToString()
+
DateTime.Now.Month.ToString()
+
DateTime.Now.Hour.ToString()
+
DateTime.Now.Minute.ToString()
+
DateTime.Now.Second.ToString()
+
DateTime.Now.Millisecond.ToString() ;
}
else //如果已有,则删除该条纪录
{
strSurveyID = m_strSurveyID ;
//删除原有纪录
myCommand.CommandText = "delete from survey where
surveyid=@#" + strSurveyID + "@#" ;
myCommand.ExecuteNonQuery() ;
}
string strSql = "insert into survey(surveyid , kind , title)
values (@#"
+ strSurveyID +"@#, 0 , @#" +
m_strTitle + "@#)\r\n" ;
for (int i = 0 ; i < m_arrItems.Count ; i ++)
{
strSql += "insert into survey(surveyid , kind ,
title) values(@#"
+ strSurveyID + "@# , 1 , @#"
+ ((SurveyItem)m_arrItems[i]).Text +
"@#)\r\n" ;
}
//插库
myCommand.CommandText = strSql ;
myCommand.ExecuteNonQuery() ;
//清场
myConn.Close() ;
}
catch(Exception e)
{
throw(new Exception("保存调查时出错:" + e.ToString())) ;
}
}
/// <summary>
/// 投票
/// </summary>
/// <param name="a_intID"> </param>
public override void Vote(int a_intID)
{
//该项计数加一
((SurveyItem)m_arrItems[a_intID]).Count += 1 ;
//数据库中改变
MyConnection myConn = new MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.CommandText = "update survey set amount=amount+1 where
id="
+
((SurveyItem)m_arrItems[a_intID]).ID.ToString() ;
myCommand.CommandType = System.Data.CommandType.Text ;
try
{
myConn.Open() ;
myCommand.ActiveConnection = myConn ;
myCommand.ExecuteNonQuery() ;
myConn.Close() ;
}
catch(Exception e)
{
throw(new Exception("更新调查项失败:" + e.ToString())) ;
}
}
/// <summary>
/// 调查列表
/// </summary>
public static ArrayList SurveyList()
{
ArrayList arrResult = new ArrayList() ;
MyClass.Util.MyConnection myConn = new MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.CommandText = "select id , surveyid , title from survey
where kind=0 order by surveyid desc" ;
myCommand.CommandType = System.Data.CommandType.Text ;
try
{
myConn.Open() ;
myCommand.ActiveConnection = myConn ;
SQLDataReader myReader ;
myCommand.Execute(out myReader) ;
while (myReader.Read())
{
FootballSurvey mySurvey = new FootballSurvey() ;
mySurvey.Title = myReader["title"].ToString() ;
mySurvey.SurveyID = myReader["surveyid"].ToString()
;
arrResult.Add(mySurvey) ;
}
myReader.Close() ;
myConn.Close() ;
}
catch(Exception e)
{
throw(new Exception("从数据库中取出调查失败:" +
e.ToString())) ;
}
//返回结果
return arrResult ;
}
/// <summary>
/// 取得激活的调查id
/// </summary>
public static string GetActiveSurveyID()
{
string strResult = "" ;
MyClass.Util.MyConnection myConn = new MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.CommandText = "select top 1 id , surveyid from survey
where active=1 order by id desc" ;
myCommand.CommandType = System.Data.CommandType.Text ;
try
{
myConn.Open() ;
myCommand.ActiveConnection = myConn ;
SQLDataReader myReader ;
myCommand.Execute(out myReader) ;
if (myReader.Read())
{
strResult = myReader["surveyid"].ToString() ;
}
else
{
throw(new Exception("没有激活的调查")) ;
}
myReader.Close() ;
}
catch(Exception e)
{
throw(new Exception("从数据库中取出调查失败:" +
e.ToString())) ;
}
finally
{
myConn.Close() ;
}
//返回结果
return strResult ;
}
/// <summary>
/// 激活调查
/// </summary>
/// <param name="a_strSurveyID">调查编号 </param>
public static void ActiveSurvey(string a_strSurveyID)
{
MyClass.Util.MyConnection myConn = new MyClass.Util.MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.CommandText = "update survey set active=0 ;"
+ "update survey set
active=1 where surveyid=@#" + a_strSurveyID + "@#" ;
myCommand.CommandType = System.Data.CommandType.Text ;
try
{
myConn.Open() ;
myCommand.ActiveConnection = myConn ;
myCommand.ExecuteNonQuery() ;
}
catch(Exception exp)
{
throw(new Exception("保存激活调查到数据库出错:" +
exp.ToString()));
}
finally
{
myConn.Close() ;
}
}
}
}
下一篇:从一个舆论调查的制作谈面向对象的编程思路(三) >>
相关文章:
- · web页面用水晶报表的例子
- · Creating Custom Portal Modules
- · 几个.net的重要问题
- · SQL命令中DateTime格式参考
- · 关于webcontrol和pagelet的一点看法
- · 关于.net的几个重要问题的bigeagle版本
- · 关于datagrid的使用以及动态修改,以及使用存储过程的介绍
- · 转阿土伯推荐的文章:在 VS.NET 中编写 Web 应用程序(附图)(推荐)
- · 转新技术网:在ASP.NET中使用.NET组件
- · 自己写的一个资料验证的asp.net程序,大家看看吧!
- · 关于在ASP.NET 中进行调试的方法(转载自itpeople),不过我个人对第三招不以为然,有了vs.net还要那个...
- · 菜鸟入门篇---有关ASP.NET的一些基本说明,解释. [页面标识]
- · 我的第一个ASP+程序,如果是新手请进来看吧。谢绝高手。:)
- · Security Hole In ASP.NET Beta 1 (from angryCoder)
- · 关于从toolbox内拖放控件到form时出错的解决办法
- · XML、DataSet、DataGrid结合写成广告管理程序(上)(转载)
- · XML、DataSet、DataGrid结合写成广告管理程序(下)(转载)
- · 我的aspx为什么无法显示中文?
- · web窗口间的互相控制
- · BigEagle的数据库结构(转载,一动手,就轻拿5分)
- · 转雨天妹妹的文章:TreeView的DHTML实现(可以实现拖动效果哟)
- · 吐血奉献:如何搞定DataGrid 分栏的大小(即DataGrid的可视化控制).
- · Is your .NET Code safe?
- · Introduction to .NET Reflection
- · .net里面的数值格式变换
- · Picture Numeric Format Strings(我很难解释大家自己看)
- · 数值变换时的格式化字符举例
- · Numeric Parse Method
- · 日期和时间的转换
- · 自定义的转换格式
- · 如何将powerpoint转换为html
- · css设定表格宽度
- · .Net边学边讲(三)
- · Calling a Button Event from a Compiled DLL
- · datagrid怎么设置分页?
- · .NET之ASP Web Application快速入门(3)(转载)
- · .NET之ASP WebApplication快速入门(4)(转载)
- · .NET之ASP WebApplication快速入门(5)(转载)
