ASP.NET高级教程(3.3):对象

本文介绍了一个简单的BBS类的设计方法,包括类的属性、构造函数等内容,并演示了如何通过构造函数从数据库中获取BBS的相关信息。
作者: bigeagle   
以上简单讲了一下如何定义类,看完这些内容,可能你就可以理解上一节我们构造的那个bbs对象,让我们再看一下它的定义:


namespace MyOwnClass

{

using System;

using System.Data.SQL ;

using System.Data ;
public class BBS

{

//私有变量

private string m_strTitle ; //bbs名称

private int m_intForumCount ; //版面数

private int m_intTopicCount ; //贴子数

private int m_intUserCount ; //注册用户数


//属性

public string Title

{

get

{

return m_strTitle ;

}

}


public int ForumCount

{

get

{

return m_intForumCount ;

}

}


public int TopicCount

{

get

{

return m_intTopicCount ;

}

}


public int UserCount

{

get

{

return m_intUserCount ;

}

}


//构造函数

public BBS(string a_strTitle)

{

//

// TODO: Add Constructor Logic here

//

m_strTitle = a_strTitle ;


//读取数据库

MyConnection myConn = new MyConnection() ;

SQLCommand myCommand = new SQLCommand() ;

myCommand.ActiveConnection = myConn ;

myCommand.CommandText = "up_GetBBSInfo" ; //调用存储过程

myCommand.CommandType = CommandType.StoredProcedure ;


try

{

myConn.Open() ;

SQLDataReader myReader ;

myCommand.Execute(out myReader) ;

if (myReader.Read())

{

m_intForumCount = (int)myReader["ForumCount"] ;

m_intTopicCount = (int)myReader["TopicCount"] ;

m_intUserCount = (int)myReader["UserCount"] ;

}

else

{

throw(new Exception("表或存储过程不存在")) ;

}


//清场

myReader.Close();

myConn.Close() ;

}

catch(SQLException e)

{

throw(new Exception("数据库出错:" + e.Message)) ;

}


}

}

}


和刚才我们讲的稍有不同,首先看第一行namespace MyOwnClass , 声明目前这个类的名字空间是MyOwnClass,名字空间就像一个包,其中可以包含很多类。再看这行: using System; 这个告诉编译器,我要引用System名字空间里的对象。然后其他的就好理解了吧?


转载于:https://www.cnblogs.com/hnlenovo/archive/2008/01/09/1031434.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值