ASP.NET高级教程:对象-2

王朝asp·作者佚名  2006-11-24
宽屏版  字体: |||超大  

以上简单讲了一下如何定义类,看完这些内容,可能你就可以理解上一节我们构造的那个bbs对象,让我们再看一下它的定义:

namespace MyOwnClass

{

using System;

using System.Data.SQL ;

using System.Data ;

////////////////////////////////////////////////////////

//

// Class Name : BBS

//

// Description: 论坛类,构造一个论坛对象

//

// date: 2000/02/03

//

/// ////////////////////////////////////////////////////////

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名字空间里的对象。然后其他的就好理解了吧?

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有