分享
 
 
 

我的网络兼职MVC项目开发-细说三层

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: |||超大  

今天我们来聊一下网站的三层如何搭建起来的(网站第一版的框架)。参考网站:革西网络

在我刚接触开发的时候,三层是用的最多的框架,没有之一。借助一些代码生成器,我们能够很容易的建立三层来,其中的增删改的方法已经有了,我们只是需要在这些基础之上,根据项目的具体的业务逻辑进行一些代码的编写就能实现了。

首先来看DAL,涵盖了在项目中,我们所需要的大部分的方法。看代码,是这样写的:

1publicclassDAL2{3#regionBaseDAL4SqlDataPRovider SQLProvider =newSqlDataProvider(SqlHelper.GetConnSting());//TODO:这个动态类是单例模式?5///<summary>6///得到最大ID7///</summary>8///<param name="table">表名</param>9///<returns></returns>10publicintGetMaxId(stringtable)11{12returnSQLProvider.GetMaxID("Id", table);13}1415///<summary>16///是否存在该记录17///</summary>18///<param name="Table">表名</param>19///<param name="Id">编号Id</param>20///<returns></returns>21publicboolExists(stringTable,intId)22{23StringBuilder strSql =newStringBuilder();24strSql.AppendFormat("select count(1) from {0}", Table);25strSql.Append("where Id=@Id");26SqlParameter[] parameters ={27newSqlParameter("@Id", SqlDbType.Int,4)28};29parameters[0].Value =Id;3031returnSQLProvider.Exists(strSql.ToString(), parameters);32}3334///<summary>35///增加一条数据36///</summary>37///<param name="Field">要插入的字段</param>38///<param name="Models">插入字段的参数</param>39///<returns></returns>40publicintAdd(stringTable,stringfield,string[] Models)41{42StringBuilder strSql =newStringBuilder();43strSql.AppendFormat("insert into {0}(", Table);44strSql.AppendFormat("{0})", field);45strSql.Append("values (");46strSql.AppendFormat("{0})", SqlParameterHelp.SqlParaValues(field));47strSql.Append(";select @@IDENTITY");4849objectobj =SQLProvider.Execute(strSql.ToString(), SqlParameterHelp.SqlPara(field, Models));50if(obj ==null)51{52return0;53}54else55{56returnConvert.ToInt32(obj);57}58}59///<summary>60///更新一条数据61///</summary>62///<param name="id">根据id修改</param>63///<param name="table">表名</param>64///<param name="fields">修改字段如Id=@Id</param>65///<returns></returns>66publicboolUpdate(stringid,stringtable,stringfields,string[] Models)67{68StringBuilder strSql =newStringBuilder();69strSql.AppendFormat("update {0} set", table);70strSql.AppendFormat("{0}", SqlParameterHelp.SqlParaValuesUpdate(fields));71strSql.AppendFormat("where Id={0}", id);727374introws =SQLProvider.Execute(strSql.ToString(), SqlParameterHelp.SqlPara(fields, Models));75if(rows >0)76{77returntrue;78}79else80{81returnfalse;82}83}8485publicboolUpdate(stringid,stringtable,stringfields,stringModels)86{87StringBuilder strSql =newStringBuilder();88strSql.AppendFormat("update {0} set {1}={2} where Id={3}", table, fields, Models,id);89introws =SQLProvider.Execute(strSql.ToString());90if(rows >0)91{92returntrue;93}94else95{96returnfalse;97}98}99100///<summary>101///删除一条数据102///</summary>103///<param name="Table">表名</param>104///<param name="Id">编号</param>105///<returns></returns>106publicboolDelete(stringTable,intId)107{108109StringBuilder strSql =newStringBuilder();110strSql.AppendFormat("delete from {0}", Table);111strSql.Append("where Id=@Id");112SqlParameter[] parameters ={113newSqlParameter("@Id", SqlDbType.Int,4)114};115parameters[0].Value =Id;116117introws =SQLProvider.Execute(strSql.ToString(), parameters);118if(rows >0)119{120returntrue;121}122else123{124returnfalse;125}126}127///<summary>128///批量删除数据129///</summary>130publicboolDeleteList(stringtable,stringIdlist)131{132StringBuilder strSql =newStringBuilder();133strSql.AppendFormat("delete from {0}", table);134strSql.Append("where Id in ("+ Idlist +")");135introws =SQLProvider.Execute(strSql.ToString());136if(rows >0)137{138returntrue;139}140else141{142returnfalse;143}144}145146147///<summary>148///得到一个对象实体149///</summary>150///<typeparam name="T">实体类型</typeparam>151///<param name="Table">表名</param>152///<param name="Id">Id</param>153///<returns></returns>154publicT GetModles<T>(stringTable,intId)whereT :new()155{156StringBuilder strSql =newStringBuilder();157strSql.AppendFormat("select top 1 * from {0}", Table);158strSql.Append("where Id=@Id");159SqlParameter[] parameters ={160newSqlParameter("@Id", SqlDbType.Int,4)161};162parameters[0].Value =Id;163164T model =newT();165DataSet ds =SQLProvider.GetDataset(strSql.ToString(), parameters);166if(ds.Tables[0].Rows.Count >0)167{168returnDataConvert.DataRowToModel<T>(ds.Tables[0].Rows[0]);169}170else171{172returndefault(T);173}174}175176177178///<summary>179///sql获得数据列表180///</summary>181///<param name="strWhere">sql条件</param>182///<returns></returns>183publicDataSet GetListBySql(stringstrWhere,stringtable)184{185returnSQLProvider.GetDataset(strWhere.ToString());186}187///<summary>188///获得数据列表189///</summary>190///<param name="strWhere">sql条件</param>191///<returns></returns>192publicDataSet GetList(stringstrWhere,stringtable)193{194StringBuilder strSql =newStringBuilder();195strSql.Append("select *");196strSql.AppendFormat("from {0}", table);197if(strWhere.Trim() !="")198{199strSql.Append("where"+strWhere);200}201returnSQLProvider.GetDataset(strSql.ToString());202}203204///<summary>205///分页得到相应数据206///</summary>207///<param name="strWhere"></param>208///<param name="table"></param>209///<param name="strorder"></param>210///<param name="start"></param>211///<param name="end"></param>212///<returns></returns>213publicDataSet GetList(stringstrWhere,stringtable,stringstrorder,intstart,intend)214{215stringsql ="";216if(strWhere.Trim() !="")217{218sql ="select * from(select *,ROW_NUMBER() over(order by"+ strorder +") as Num from"+ table +"where"+ strWhere +") as newTable where Num between"+ start +"and"+ end +"";219}220else221{222sql ="select * from(select *,ROW_NUMBER() over(order by"+ strorder +") as Num from"+ table +") as newTable where Num between"+ start +"and"+ end +"";223}224returnSQLProvider.GetDataset(sql.ToString());225}226227///<summary>228///非常规排序229///</summary>230///<param name="strTop"></param>231///<param name="strWhere"></param>232///<param name="table"></param>233///<param name="rank"></param>234///<returns></returns>235publicDataSet GetTopListOne(stringstrTop,stringstrs,stringtable,stringrank)236{237StringBuilder strSql =newStringBuilder();238strSql.Append("select top("+ strTop +")*");239strSql.AppendFormat("from {0}", table);240if(strs.Trim() !="")241{242strSql.Append(""+strs);243}244strSql.Append("order by"+ rank +"");245returnSQLProvider.GetDataset(strSql.ToString());246}247248///<summary>249///获取前几行数据 用于首页显示 方便250///</summary>251///<typeparam name="T"></typeparam>252///<param name="strTop"></param>253///<param name="strWhere"></param>254///<param name="table"></param>255///<returns></returns>256publicDataSet GetTopList(stringstrTop,stringstrWhere,stringtable,stringrank)257{258StringBuilder strSql =newStringBuilder();259strSql.Append("select top("+ strTop +")*");260strSql.AppendFormat("from {0}", table);261if(strWhere.Trim() !="")262{263strSql.Append("where"+strWhere);264}265strSql.Append("order by"+ rank +"");266returnSQLProvider.GetDataset(strSql.ToString());267}268269270publicDataSet GetTopList2(stringstrTop,stringstrWhere,stringtable,stringrank)271{272StringBuilder strSql =newStringBuilder();273strSql.Append("select top("+ strTop +") id,title,ImgInfo,Author,Time");274strSql.AppendFormat("from {0}", table);275if(strWhere.Trim() !="")276{277strSql.Append("where"+strWhere);278}279strSql.Append("order by"+ rank +"");280returnSQLProvider.GetDataset(strSql.ToString());281}282283///<summary>284///获得前几行数据285///</summary>286///<param name="Top">几行</param>287///<param name="strWhere">sql语句</param>288///<param name="filedOrder">排序</param>289///<param name="table">表名</param>290///<returns></returns>291publicDataSet GetList(intTop,stringstrWhere,stringfiledOrder,stringtable)292{293StringBuilder strSql =newStringBuilder();294strSql.Append("select");295if(Top >0)296{297strSql.Append("top"+Top.ToString());298}299strSql.Append("*");300strSql.AppendFormat("from {0}", table);301if(strWhere.Trim() !="")302{303strSql.Append("where"+strWhere);304}305strSql.Append("order by"+filedOrder);306returnSQLProvider.GetDataset(strSql.ToString());307}308309///<summary>310///获取记录总数311///</summary>312///<param name="strWhere">sql查询条件</param>313///<param name="table">表名</param>314///<returns></returns>315publicintGetRecordCount(stringstrWhere,stringtable)316{317StringBuilder strSql =newStringBuilder();318strSql.AppendFormat("select count(1) from {0}", table);319if(strWhere.Trim() !="")320{321strSql.Append("where"+strWhere);322}323objectobj =SQLProvider.GetScalar(strSql.ToString());324if(obj ==null)325{326return0;327}328else329{330returnConvert.ToInt32(obj);331}332}333///<summary>334///分页获取数据列表335///</summary>336///<param name="strWhere">分页条件</param>337///<param name="orderby">排序字段desc</param>338///<param name="startIndex">开始页</param>339///<param name="endIndex">结束页</param>340///<param name="table">表名</param>341///<returns></returns>342publicDataSet GetListByPage(stringstrWhere,stringorderby,intstartIndex,intendIndex,stringtable)343{344StringBuilder strSql =newStringBuilder();345strSql.Append("SELECT * FROM (");346strSql.Append("SELECT ROW_NUMBER() OVER (");347if(!string.IsNullOrEmpty(orderby.Trim()))348{349strSql.Append("order by T."+orderby);350}351else352{353strSql.Append("order by T.Id desc");354}355strSql.AppendFormat(")AS Row, T.* from {0} T", table);356if(!string.IsNullOrEmpty(strWhere.Trim()))357{358strSql.Append("WHERE"+strWhere);359}360strSql.Append(") TT");361strSql.AppendFormat("WHERE TT.Row between {0} and {1}", startIndex, endIndex);362returnSQLProvider.GetDataset(strSql.ToString());363}364365366#endregionBasicMethod367}

View Code

再来看看BLL基类:

1publicclassBLL2{3privatereadonlyDAL dal =newDAL();45///<summary>6///得到最大ID7///</summary>8///<param name="table">表名</param>9///<returns></returns>10publicintGetMaxId(stringtable)11{12returndal.GetMaxId(table);13}14///<summary>15///是否存在该记录16///</summary>17///<param name="Table">表名</param>18///<param name="Id">编号Id</param>19///<returns></returns>20publicboolExists(stringtable,intId)21{22returndal.Exists(table, Id);23}24///<summary>25///增加一条数据26///</summary>27///<param name="Field">要插入的字段</param>28///<param name="Models">插入字段的参数</param>29///<returns></returns>30publicintAdd(stringTable,stringField,string[] Models)31{32returndal.Add(Table, Field, Models);33}34///<summary>35///更新一条数据36///</summary>37///<param name="id">根据id修改</param>38///<param name="table">表名</param>39///<param name="fields">修改字段</param>40///<returns></returns>41publicboolUpdate(stringid,stringtable,stringfields,string[] models)42{43returndal.Update(id, table, fields, models);44}4546publicboolUpdate(stringid,stringtable,stringfields,stringmodels)47{48returndal.Update(id, table, fields, models);49}50///<summary>51///删除一条数据52///</summary>53///<param name="Table">表名</param>54///<param name="Id">编号</param>55///<returns></returns>56publicboolDelete(stringTable,intId)57{58returndal.Delete(Table, Id);59}60///<summary>61///获得数据列表62///</summary>63///<param name="strWhere">sql条件</param>64///<returns></returns>65publicDataSet GetList(stringstrWhere,stringtable)66{67returndal.GetList(strWhere, table);68}6970#region利用泛型类获取实体类71///<summary>72///获得数据列表73///</summary>74publicList<T> GetModelList<T>(stringstrWhere,stringtable)whereT :new()75{76DataSet ds =dal.GetList(strWhere, table);77returnDataTableToList<T>(ds.Tables[0]);78}7980///<summary>81///纯sql获得数据列表82///</summary>83publicList<T> GetModelListBySql<T>(stringsql,stringtable)whereT :new()84{85DataSet ds =dal.GetListBySql(sql, table);86returnDataTableToList<T>(ds.Tables[0]);87}8889publicList<T> GetModelList<T>(stringstrWhere,stringtable,stringstrorder,intstart,intend)whereT :new()90{91DataSet ds =dal.GetList(strWhere, table, strorder, start, end);92returnDataTableToList<T>(ds.Tables[0]);93}9495///<summary>96///获取前几行数据 用于首页显示 方便97///</summary>98///<typeparam name="T"></typeparam>99///<param name="strTop"></param>100///<param name="strWhere"></param>101///<param name="table"></param>102///<returns></returns>103publicList<T> GetModelTopList<T>(stringstrTop,stringstrWhere,stringtable,stringrank)whereT :new()104{105DataSet ds =dal.GetTopList(strTop, strWhere, table, rank);106returnDataTableToList<T>(ds.Tables[0]);107}108109110publicList<T> GetModelTopList2<T>(stringstrTop,stringstrWhere,stringtable,stringrank)whereT :new()111{112DataSet ds =dal.GetTopList2(strTop, strWhere, table, rank);113returnDataTableToList<T>(ds.Tables[0]);114}115116117///<summary>118///根据国家文章的前几篇排序 非常规排序119///</summary>120///<typeparam name="T"></typeparam>121///<param name="strTop"></param>122///<param name="strs"></param>123///<param name="table"></param>124///<param name="rank"></param>125///<returns></returns>126publicList<T> GetModelTopListOne<T>(stringstrTop,stringstrs,stringtable,stringrank)whereT :new()127{128DataSet ds =dal.GetTopListOne(strTop, strs, table, rank);129returnDataTableToList<T>(ds.Tables[0]);130}131132///<summary>133///获得数据列表134///</summary>135publicList<T> DataTableToList<T>(DataTable dt)whereT :new()136{137List<T> modelList =newList<T>();138introwsCount =dt.Rows.Count;139if(rowsCount >0)140{141T model;142for(intn =0; n < rowsCount; n++)143{144model = DataConvert.DataRowToModel<T>(dt.Rows[n]);145if(model !=null)146{147modelList.Add(model);148}149}150}151returnmodelList;152}153154155SqlDataProvider SQLProvider =newSqlDataProvider(SqlHelper.GetConnSting());156///<summary>157///得到一个对象实体158///</summary>159///<typeparam name="T">实体类型</typeparam>160///<param name="Table">表名</param>161///<param name="Id">Id</param>162///<returns></returns>163publicT GetModles<T>(stringTable,intId)whereT :new()164{165StringBuilder strSql =newStringBuilder();166strSql.AppendFormat("select top 1 * from {0}", Table);167strSql.Append("where Id=@Id");168SqlParameter[] parameters ={169newSqlParameter("@Id", SqlDbType.Int,4)170};171parameters[0].Value =Id;172173T model =newT();174DataSet ds =SQLProvider.GetDataset(strSql.ToString(), parameters);175if(ds.Tables[0].Rows.Count >0)176{177returnDataConvert.DataRowToModel<T>(ds.Tables[0].Rows[0]);178}179else180{181returndefault(T);182}183}184185///<summary>186///得到一个对象实体187///</summary>188///<typeparam name="T">实体类型</typeparam>189///<param name="Table">表名</param>190///<param name="Id">Id</param>191///<returns></returns>192publicT GetModles<T>(stringTable,stringfields,stringmodels)whereT :new()193{194StringBuilder strSql =newStringBuilder();195strSql.AppendFormat("select top 1 * from {0}", Table);196strSql.AppendFormat("where {0}=@{1}", fields, fields);197SqlParameter[] parameters ={198newSqlParameter("@"+fields+"", models)199};200201T model =newT();202DataSet ds =SQLProvider.GetDataset(strSql.ToString(), parameters);203if(ds.Tables[0].Rows.Count >0)204{205returnDataConvert.DataRowToModel<T>(ds.Tables[0].Rows[0]);206}207else208{209returndefault(T);210}211}212213///<summary>214///得到一个对象实体 sql215///</summary>216///<typeparam name="T">实体类型</typeparam>217///<param name="Table">表名</param>218///<param name="strWhere">Id</param>219///<returns></returns>220publicT GetModle<T>(stringTable,stringstrWhere)whereT :new()221{222StringBuilder strSql =newStringBuilder();223strSql.AppendFormat("select top 1 * from {0}", Table);224if(strWhere.Trim() !="")225{226strSql.Append("where"+strWhere);227}228229T model =newT();230DataSet ds =SQLProvider.GetDataset(strSql.ToString());231if(ds.Tables[0].Rows.Count >0)232{233returnDataConvert.DataRowToModel<T>(ds.Tables[0].Rows[0]);234}235else236{237returndefault(T);238}239}240///<summary>241///得到一个对象实体242///</summary>243///<typeparam name="T">实体类型</typeparam>244///<param name="Table">表名</param>245///<param name="Name">Name</param>246///<returns></returns>247publicT GetModles<T>(stringTable,stringName)whereT :new()248{249StringBuilder strSql =newStringBuilder();250strSql.AppendFormat("select top 1 * from {0}", Table);251strSql.Append("where UserEmail=@Name");252SqlParameter[] parameters ={253newSqlParameter("@Name", SqlDbType.NVarChar,50)254};255parameters[0].Value =Name;256257T model =newT();258DataSet ds =SQLProvider.GetDataset(strSql.ToString(), parameters);259if(ds.Tables[0].Rows.Count >0)260{261returnDataConvert.DataRowToModel<T>(ds.Tables[0].Rows[0]);262}263else264{265returndefault(T);266}267}268#endregion269270///<summary>271///获得数据列表272///</summary>273publicDataSet GetAllList(stringtable)274{275returnGetList("", table);276}277278///<summary>279///获得前几行数据280///</summary>281///<param name="Top">几行</param>282///<param name="strWhere">sql语句</param>283///<param name="filedOrder">排序</param>284///<param name="table">表名</param>285///<returns></returns>286publicDataSet GetList(intTop,stringstrWhere,stringfiledOrder,stringtable)287{288returndal.GetList(Top, strWhere, filedOrder, table);289}290///<summary>291///获取记录总数292///</summary>293///<param name="strWhere">sql查询条件</param>294///<param name="table">表名</param>295///<returns></returns>296publicintGetRecordCount(stringstrWhere,stringtable)297{298returndal.GetRecordCount(strWhere, table);299}300///<summary>301///分页获取数据列表302///</summary>303///<param name="strWhere">分页条件</param>304///<param name="orderby">排序字段desc</param>305///<param name="startIndex">开始页</param>306///<param name="endIndex">结束页</param>307///<param name="table">表名</param>308///<returns></returns>309publicDataSet GetListByPage(stringstrWhere,stringorderby,intstartIndex,intendIndex,stringtable)310{311returndal.GetListByPage(strWhere,orderby, startIndex, endIndex, table);312}313314315///<summary>316///关键词下拉查询317///</summary>318///<param name="kw">关键词</param>319///<returns></returns>320publicIEnumerable<SearchSum> GetSuggestion(stringkw)321{322DataTable dt = SQLProvider.ExecuteDataTable(@"select top 5 KeyWords,count(*) as searchcount from SearchDetails323where datediff(day,searchdatetime,getdate())<7324and keywords like @keyword325group by Keywords order by count(*) desc",newSqlParameter("@keyword","%"+ kw +"%"));326List<SearchSum> list =newList<SearchSum>();327if(dt !=null&& dt.Rows !=null&& dt.Rows.Count >0)328{329foreach(DataRow rowindt.Rows)330{331SearchSum oneModel =newSearchSum();332oneModel.Keyword = Convert.ToString(row["keywords"]);333oneModel.SearchCount = Convert.ToInt32(row["SearchCount"]);334list.Add(oneModel);335}336}337returnlist;338}339#region热度查询+缓存340///<summary>341///缓存342///</summary>343///<returns></returns>344publicIEnumerable<SearchDetails>GetHotWords()345{346//缓存347vardata = HttpRuntime.Cache["hotwords"];348if(data ==null)349{350IEnumerable<SearchDetails> hotWords =DoSelect();351HttpRuntime.Cache.Insert("hotwords", hotWords,null, DateTime.Now.AddMilliseconds(30), TimeSpan.Zero);352returnhotWords;353}354return(IEnumerable<SearchDetails>)data;355}356///<summary>357///查询热度358///</summary>359///<returns></returns>360privateIEnumerable<SearchDetails>DoSelect()361{362stringsql =@"select top 5 Keywords,count(*) as searchcount from SearchDetails363where datediff(day,searchdatetime,getdate())<7364group by Keywords order by count(*) desc";365List<SearchDetails> list =newList<SearchDetails>();366367list = GetModelListBySql<SearchDetails>(sql,"SearchDetails");368returnlist;369}370#endregion371}

BLL类

最后看看Model,其中的新闻类:

1[Serializable]2publicclassNewsInfo3{4publicNewsInfo()5{ }6#regionModel7privateint_id;8privatestring_title;9privatestring_keyword;10privatestring_summary;11privatestring_source;12privatestring_sourceUrl;13privatestring_author;14privateDateTime _time;15privateint_hits;16privatestring_newsinfo;17privateint_ishotspot;18privateint_newstypeid;19privatestring_newstypename;20privateint_isdelete;21privateint_sort;22privatestring_imgInfo;23privatestring_imgDec;24privateint_price;25privateint_review;26privatestring_creatUserId;27publicstringCreatUserId28{29get{return_creatUserId; }30set{ _creatUserId =value; }31}32privatestringcheckState;33publicstringCheckState34{35get{returncheckState; }36set{ checkState =value; }37}3839///<summary>40///评论41///</summary>42publicintReview43{44get{return_review; }45set{ _review =value; }46}4748publicintPrice49{50get{return_price; }51set{ _price =value; }52}53publicstringImgDec54{55get{return_imgDec; }56set{ _imgDec =value; }57}5859///<summary>60///编号61///</summary>62publicintId63{64set{ _id =value; }65get{return_id; }66}6768///<summary>69///标题70///</summary>71publicstringTitle72{73set{ _title =value; }74get{return_title; }75}76///<summary>77///关键字78///</summary>79publicstringKeyWord80{81set{ _keyword =value; }82get{return_keyword; }83}84///<summary>85///摘要86///</summary>87publicstringSummary88{89set{ _summary =value; }90get{return_summary; }91}92///<summary>93///来源94///</summary>95publicstringSource96{97set{ _source =value; }98get{return_source; }99}100///<summary>101///来源url102///</summary>103publicstringSourceUrl104{105set{ _sourceUrl =value; }106get{return_sourceUrl; }107}108///<summary>109///作者110///</summary>111publicstringAuthor112{113set{ _author =value; }114get{return_author; }115}116///<summary>117///发表时间118///</summary>119publicDateTime Time120{121set{ _time =value; }122get{return_time; }123}124///<summary>125///点击量126///</summary>127publicintHits128{129set{ _hits =value; }130get{return_hits; }131}132133///<summary>134///新闻内容135///</summary>136publicstringNewsinfo137{138get{return_newsinfo; }139set{ _newsinfo =value; }140}141142///<summary>143///是否是热点144///</summary>145publicintIsHotSpot146{147set{ _ishotspot =value; }148get{return_ishotspot; }149}150151///<summary>152///新闻类型编号153///</summary>154publicintNewsTypeId155{156set{ _newstypeid =value; }157get{return_newstypeid; }158}159///<summary>160///新闻类型名称161///</summary>162publicstringNewsTypeName163{164set{ _newstypename =value; }165get{return_newstypename; }166}167168///<summary>169///是否删除170///</summary>171publicintIsDelete172{173set{ _isdelete =value; }174get{return_isdelete; }175}176177///<summary>178///新闻排序179///</summary>180publicintSort181{182set{ _sort =value; }183get{return_sort; }184}185186///<summary>187///图片路径188///</summary>189publicstringImgInfo190{191get{return_imgInfo; }192set{ _imgInfo =value; }193}194#endregionModel195}

News Model

Model类使用东软的代码生成器生成的。目前为止,我们简单的三层的主要的代码已经出来了。但是怎么能把这些连在一起呢,看代码:

1publicabstractclassDataProvider2{3privatestaticDataProvider _instance =null;4publicstaticDataProvider Instance5{6get7{8if(_instance ==null)9{10String strConnectionString = ConfigurationManager.ConnectionStrings["ConnnectionString"].ConnectionString;1112String strConnectionType = ConfigurationManager.ConnectionStrings["ConnectionType"].ConnectionString;13if(strConnectionType.ToLower() =="sqlclient")14{15strConnectionType ="DAL.SqlDataProvider, DAL";16}1718Type t =Type.GetType(strConnectionType);19Type[] paramTypes =newType[1];20paramTypes[0] =typeof(String);2122Object[] paramArray =newObject[1];23paramArray[0] =strConnectionString;2425_instance =(DataProvider)((ConstructorInfo)t.GetConstructor(paramTypes)).Invoke(paramArray);26}27return_instance;28}29}3031#region抽象属性和方法3233#region数据库链接串34publicabstractString ConnectionString {get; }35#endregion3637#region执行SQL语句38publicvirtualboolExists(stringstrSql,paramsSqlParameter[] cmdParms) {returntrue; }39publicvirtualintGetMaxID(stringFieldName,stringTableName) {return0; }40publicabstractObject GetScalar(String selectSql);41publicabstractDataSet GetDataset(String selectSql);42publicvirtualDataSet GetDatasetTrans(SqlTransaction trans, String selectSql) {returnnull; }43publicvirtualDataSet GetDatasetTrans(OleDbTransaction trans, String selectSql) {returnnull; }44publicvirtualDataSet GetDataset(String selectSql, SqlParameter[] para) {returnnull; }45publicvirtualDataSet GetDataset(String selectSql, OleDbParameter[] para) {returnnull; }46publicvirtualDataSet GetDatasetTrans(SqlTransaction trans, String selectSql, SqlParameter[] para) {returnnull; }47publicvirtualDataSet GetDatasetTrans(OleDbTransaction trans, String selectSql, OleDbParameter[] para) {returnnull; }48publicabstractintExecute(String noneQuery);49publicvirtualintExecute(String noneQuery, SqlParameter[] para) {return0; }50publicvirtualintExecute(String noneQuery, OleDbParameter[] para) {return0; }51publicabstractIDataReader GetReader(String selectSql);52publicvirtualIDataReader GetReaderTrans(SqlTransaction trans, String selectSql) {returnnull; }53publicvirtualIDataReader GetReaderTrans(OleDbTransaction trans, String selectSql) {returnnull; }54publicvirtualIDataReader GetReader(String selectSql, SqlParameter[] para) {returnnull; }55publicvirtualIDataReader GetReader(String selectSql, OleDbParameter[] para) {returnnull; }56publicvirtualIDataReader GetReaderTrans(SqlTransaction trans, String selectSql, SqlParameter[] para) {returnnull; }57publicvirtualIDataReader GetReaderTrans(OleDbTransaction trans, String selectSql, OleDbParameter[] para) {returnnull; }58#endregion5960#region执行存储过程61publicabstractvoidExecuteSp(String spName);62publicvirtualvoidExecuteSp(SqlTransaction trans, String spName) { }63publicvirtualvoidExecuteSp(OleDbTransaction trans, String spName) { }64publicvirtualvoidExecuteSp(String spName, SqlParameter[] para) { }65publicvirtualvoidExecuteSp(String spName, OleDbParameter[] para) { }66publicvirtualvoidExecuteSp(SqlTransaction trans, String spName, SqlParameter[] para) { }67publicvirtualvoidExecuteSp(OleDbTransaction trans, String spName, OleDbParameter[] para) { }68publicabstractIDataReader GetReaderBySp(String spName);69publicvirtualIDataReader GetReaderBySp(String spName, SqlParameter[] para) {returnnull; }70publicvirtualIDataReader GetReaderBySp(String spName, OleDbParameter[] para) {returnnull; }71publicabstractObject GetScalarBySp(String spName);72publicvirtualObject GetScalarBySp(String spName, SqlParameter[] para) {returnnull; }73publicvirtualObject GetScalarBySp(String spName, OleDbParameter[] para) {returnnull; }74publicabstractDataSet GetDatasetBySp(String spName);75publicvirtualDataSet GetDatasetBySp(String spName, SqlParameter[] para) {returnnull; }76publicvirtualDataSet GetDatasetBySp(String spName, OleDbParameter[] para) {returnnull; }77#endregion7879#endregion80}

View Code

另外记得在Web.config文件中,修改数据库的链接地址哟。

到目前为止,三层的东西已经都全了。再接下来,咱们一块看看,到底是如何利用三层进行数据的增删改查的。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有