文章作者: 责任编辑:rosan 录入时间:2007-6-15 9:14:24 来源:频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意.
先说明一下我使用的命名空间的想法,我是把前后台共同的类的命名就空间命名成了 CjjerBase ,只是前台的 CJJERGUESTBOOK,后台的是 BookAdminSpace ,基本达到表意,使用不同的命名空间还是考虑到.net加载信息的优化性,不再赘言。
设计与数据库交互的基类: conn类
代码拷贝框option explicit 'option strict Imports System Imports System.Data Imports System.Data.OleDb Imports System.web 'Imports System.Xml namespace CJJERGUESTBOOK Public Class conn Private connstr as String Private conn as oledbconnection Public Sub new() connstr = System.Configuration.ConfigurationSettings.Appsettings("acconnectionstring").tostring.Trim 'connstr= configurationSettings.appsettings("acconnectionstring") connstr = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & connstr conn = new oledbconnection(connstr) End Sub '获取数据源 'writeonly Public property constr() as String Set connstr = value End Set Get return connstr End Get End property '显示打开connection对象 Public Sub open() try conn.open() catch objerror as Exception 'HttpContext.Current.Response.Write("<font color =red>Conn类异常:</font>" & objerror.Message & ":" & objerror.source) Exit sub End try End Sub '显示关闭connection链接 Public Sub close() conn.close() End Sub '返回一个datareader对象,输入的是sql语句 Public Function GetDataReader (strsql as String ) as oledbdatareader Dim objdatareader as oledbdatareader Dim objcommand as new oledbcommand(strsql,conn) try open() objdatareader = objcommand.executereader() catch close() Exit Function finally End try return objdatareader ' objcommand.executereader(CommandBehavior.CloseConnection) ' close() End Function '返回 OledbCommand 对象 public function OledbCommand(str as string) as OledbCommand Dim objcmd as new OleDbCommand(str,conn) open() return objcmd End function '返回 OledbCommand->ExecuteNonquery Public Function ExecuteNonquery(objcmd as OledbCommand) as Integer Dim xint as integer try open() xint = objcmd.ExecuteNonquery() catch objerror as Exception HttpContext.Current.Response.Write("<font color =red>Conn类异常:</font>" & objerror.Message & ":" & objerror.source) close() finally close() End try Return xint End Function '返回 OledbCommand->ExecuteNonquery Public Function ExecuteNonquery(str as string) as Integer Dim xint as integer = 2 try Dim objcmd as new OleDbCommand(str,conn) open() xint = objcmd.ExecuteNonquery() catch objerror as Exception HttpContext.Current.Response.Write("<font color =red>Conn类异常:</font>" & objerror.Message & ":" & objerror.source & "<hr/>" & str) exit Function close() finally close() End try Return xint End Function '返回一个聚集的值 Public Function ExecuteScalar(str as string) as Integer Dim xint as integer = 2 try Dim objcmd as new OleDbCommand(str,conn) open() xint = ctype(objcmd.ExecuteScalar(),integer) catch close() finally close() End try Return xint End Function '返回dataset对象 Public Function GetDataSet(str as String ,optional refstr as string = "data" ) as DataSet Dim objDataSet as new DataSet() try open() Dim objdataadapter as new oledbdataadapter(str,conn) objdataadapter.fill(objDataSet,refstr) catch HttpContext.Current.Response.Write("SQL 语句 出错") finally close() End try return objDataSet End Function '这个函数用于检测第一条数据中的第一个值 ' Function GetFirstInRow( sql as string) as string Dim objdatareader as OleDbDataReader Dim objcommand as new oledbcommand(sql,conn) try open() objdatareader = objcommand.executereader() catch objerror as Exception HttpContext.Current.Response.Write("<font color =red>Conn类异常:</font>" & objerror.Message & ":" & objerror.source) close() HttpCont此新闻共有3页 上一页 1 2 3 下一页
此新闻共有3页 上一页 1 2 3 下一页