找回密码
 注册
搜索
热搜: 回贴

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

2009-12-13 13:00| 发布者: admin| 查看: 34| 评论: 0|原作者: 云天青

▲以上简单讲了一下如何定义类,看完这些内容......


以上简单讲了一下如何定义类,看完这些内容,可能你就可以理解上一节我们构造的那个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名字空间里的对象。然后其他的就好理解了吧?

最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 07:21 , Processed in 0.171956 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部