利用c#制作简单的留言板(1)
留言板分三个模块:列出留言列表、显示详细内容、发表留言
notepage.cs
namespace notpage
{
using System;
using System.Data.SQL ;
using System.Data ;
using System.Collections ;
/// <summary>
/// Summary description for notepage.
/// </summary>
public class notepage
{
//私有变量
private int n_intID ; //ID编号
private string n_strTitle ; //主题
private string n_strAuthor ; //留言人
private string n_strContent ; //留言内容
private DateTime n_dateTime ; //留言时间
//属性
public int ID
{
get
{
return n_intID ;
}
set
{
n_intID = value;
}
}
public string Title
{
get
{
return n_strTitle ;
}
set
{
n_strTitle = value;
}
}
public string Author
{
get
{
return n_strAuthor ;
}
set
{
n_strAuthor = value ;
}
}
public string Content
{
get
{
return n_strContent ;
}
set
{
n_strContent = value ;
}
}
public DateTime adddate
{
get
{
return n_dateTime;
}
set
{
n_dateTime = value;
}
}
//构造函数
public notepage()
{
//
// TODO: Add Constructor Logic here
//
this.n_intID = 0 ;
this.n_strTitle = "" ;
this.n_strAuthor = "" ;
this.n_strContent = "" ;
this.n_dateTime = System.DateTime.Now;
}
/// <summary>
///
/// 取得留言的内容
///
/// </summary>
/// <param name="a_intID"> </param>
public notepage GetTopic(int a_intID)
{
//
// TODO: Add Constructor Logic here
//
//读取数据库
myconn myConn = new myconn();
SQLCommand myCommand = new SQLCommand() ;
myCommand.ActiveConnection = myConn ;
myCommand.CommandText = "n_GetTopicInfo" ; //调用存储过程
myCommand.CommandType = CommandType.StoredProcedure ;
myCommand.Parameters.Add(new SQLParameter("@a_intTopicID" , SQLDataType.Int)) ;
myCommand.Parameters["@a_intTopicID"].Value = a_intID ;
notepage objNp = new notepage();
try
{
myConn.Open() ;
SQLDataReader myReader ;
myCommand.Execute(out myReader) ;
if (myReader.Read())
{
objNp.ID = (int)myReader["ID"] ;
objNp.Title = (string)myReader["Title"] ;
objNp.Author = (string)myReader["Author"] ;
objNp.Content = (string)myReader["Content"];
objNp.adddate = (DateTime)myReader["adddate"];
}
//清场
myReader.Close();
myConn.Close() ;
}
catch(Exception e)
{
throw(new Exception("取贴子失败:" + e.ToString())) ;
}
return objNp;
}
/// <summary>
///
/// 目的:将留言的内容入库
///
/// 利用构造函数来传递信息
///
/// </summary>
/// <param name="n_Topic"> </param>
public bool AddTopic(notepage n_Topic)
{
//
// TODO: Add Constructor Logic here
//
//读取数据库
myconn myConn = new myconn();
SQLCommand myCommand = new SQLCommand() ;
myCommand.ActiveConnection = myConn ;
myCommand.CommandText = "n_addTopic" ; //调用存储过程
myCommand.CommandType = CommandType.StoredProcedure ;
myCommand.Parameters.Add(new SQLParameter("@a_strTitle" , SQLDataType.VarChar,100)) ;
myCommand.Parameters["@a_strTitle"].Value = n_Topic.Title ;
myCommand.Parameters.Add(new SQLParameter("@a_strAuthor" , SQLDataType.VarChar,50)) ;
myCommand.Parameters["@a_strAuthor"].Value = n_Topic.Author ;
myCommand.Parameters.Add(new SQLParameter("@a_strContent" , SQLDataType.VarChar,2000)) ;
myCommand.Parameters["@a_strContent"].Value = n_Topic.Content ;
try
{
myConn.Open() ;
myCommand.ExecuteNonQuery() ;
//清场
myConn.Close() ;
}
catch(Exception e)
{
throw(new Exception("取贴子失败:" + e.ToString())) ;
}
return true;
}
/// <summary>
/// 取的贴子列表
/// </summary>
/// <remarks>
/// 返回一个Topic数组
/// </remarks>
public ArrayList GetTopicList()
{
//定义一个forum数组做为返回值
ArrayList arrForumList =new ArrayList() ;
//从数据库中读取留言列表
myconn myConn = new myconn();
SQLCommand myCommand = new SQLCommand() ;
myCommand.ActiveConnection = myConn ;
myCommand.CommandText = "n_GetTopicList" ; //调用存储过程
myCommand.CommandType = CommandType.StoredProcedure ;
try
{
myConn.Open() ;
SQLDataReader myReader ;
myCommand.Execute(out myReader) ;
for (int i = 0 ; myReader.Read() ; i++)
{
notepage objItem = new notepage() ;
objItem.ID = myReader["ID"].ToString().ToInt32() ;
objItem.Title = myReader["Title"].ToString() ;
objItem.Author = myReader["Author"].ToString() ;
objItem.adddate = myReader["adddate"].ToString().ToDateTime();
objItem.Content = myReader["Content"].ToString();
arrForumList.Add(objItem) ;
}
//清场
myReader.Close();
myConn.Close() ;
}
catch(SQLException e)
{
throw(new Exception("数据库出错:" + e.ToString())) ;
//return null ;
}
return arrForumList ;
}
}
}
- · 不要忽略c#中的using和as操作符
- · C#中关于GDI+输出的问题
- · 几个C# PROGRAMS
- · 几个C# PROGRAMS (2)
- · c#中的interface abstract与virtual
- · MapX从数据库读取数据形成新图层(C#)
- · C#对XML操作:建立XML(2)
- · C#对XML操作:建立XML(1)
- · C#调用父类的父类的方法
- · .NET中C#实现C/S架构下的TREEVIEW只需要输入表名,父ID,节点ID,节点名就可以得到树型结构
- · what is new in c sharp 2.0--study from msdn
- · C#设计模式之建造者(Builder)模式示例源代码
- · UBB(c#完整版)
- · c#中结构与类的区别
- · 三层结构
- · Destructors in C#
- · 链表类具有哈希表的功能
- · C#中委托,事件理解入门
- · Introduce event delegate
- · 替换以|分割的相同字符串
- · IOCP Thread Pooling in C#
- · C# struct class Differences
- · C#冒泡算法!
- · C#写的数据库操作类!
- · 快速开发vs.net+c#程序(-)
- · C#对XML操作:编辑XML文件内容
- · 用C#生成Excel文件的方法和Excel.dll组件生成的方法
- · 用C#实现Web文件的上传
- · C#编写的生成缩略图程序
- · 用一个留言簿说明C#操作XML的完全过程
- · 递归枚举排列、组合的C#源码
- · C#反编译微软MSDN2003的帮助文档,并将反编译结果保存到一个SQLSERVER数据库中
- · 用C#轻松在DOTNET中实现缩略图
- · c#中结构与类的区别
- · DES加密算法在C#下的实现
- · C#对XML操作:建立XML(3)
- · 上传图片画带阴影的水印.(C#)
- · Destructors in C#
