| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5091 人关注过本帖
标题:求ASP.NET三层结构
只看楼主 加入收藏
垃圾的沉默
Rank: 1
等 级:新手上路
威 望:2
帖 子:511
专家分:0
注 册:2007-6-25
收藏
得分:0 
是不是可以这样理解
界面--应用程序--数据库

人们笑我太疯癫...我笑人们太正经
2007-07-03 19:08
wuhongyao3
Rank: 1
等 级:新手上路
帖 子:202
专家分:0
注 册:2006-4-18
收藏
得分:0 
呵呵,正在找,看看能看懂不?

2007-11-12 15:06
yms123
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
收藏
得分:0 

using System;
using System.Data;
using System.Data.SqlClient;
namespace DataAccess
{
/// <summary>
/// SQL Server 数据库的访问类
/// </summary>
public class SqlAccess
{
private SqlConnection SqlCon;
private SqlDataAdapter SqlAdp;
private SqlCommand SqlCmd;
private SqlCommandBuilder SqlCmdB;

public SqlAccess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

protected SqlConnection getConnection()
{
if(this.SqlCon==null)
{
this.SqlCon=new SqlConnection(this.ConStr);
this.SqlCon.Open();
}
return this.SqlCon;
}

protected SqlDataAdapter getDataAdapter()
{
this.SqlCon=(SqlConnection)this.getConnection();
this.SqlAdp=new SqlDataAdapter(this.SqlStr,this.SqlCon);
return this.SqlAdp;
}

protected SqlCommand getCommand()
{
this.SqlCon=(SqlConnection)this.getConnection();
this.SqlCmd=new SqlCommand(this.SqlStr,this.SqlCon);
return this.SqlCmd;
}

protected SqlCommandBuilder getCommandBuilder()
{
this.SqlAdp=(SqlDataAdapter)this.getDataAdapter();
this.SqlCmdB=new SqlCommandBuilder(this.SqlAdp);
return this.SqlCmdB;
}

protected DataSet getDataSet()
{
this.SqlAdp=(SqlDataAdapter)this.getDataAdapter();
this.ObjDs=new DataSet();
this.SqlAdp.Fill(this.ObjDs,this.TBName);
return this.ObjDs;
}

protected void SetActionCommand()
{
switch(this.ActionMode)
{
case "Insert":
this.SqlAdp.InsertCommand=this.SqlCmdB.GetInsertCommand();
break;
case "Update":
this.SqlAdp.UpdateCommand=this.SqlCmdB.GetUpdateCommand();
break;
case "Delete":
this.SqlAdp.DeleteCommand=this.SqlCmdB.GetDeleteCommand();
break;
}
}

public DataTable getDataTable()
{
this.ObjDs=this.getDataSet();
return this.ObjDs.Tables[this.TBName];
}

public void SaveTable(DataSet srcDts)
{
this.SqlAdp=(SqlDataAdapter)this.getDataAdapter();
this.SqlCmdB=(SqlCommandBuilder)this.getCommandBuilder();
this.SetActionCommand();
this.SqlAdp.Update(srcDts,this.TBName);
}

}
}
个人的数据层常用代码,这个类的优点在于,只用一个函数就可以直接得到DataTable,个人觉得.NET里Connection对象可以用单例模式来做。

2007-11-12 17:34
cook111111
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-1-14
收藏
得分:0 
学习了~~
2007-11-13 11:09
zmfttkl
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2007-7-1
收藏
得分:0 

支持!


2007-11-14 19:01
simpson
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:863
专家分:17
注 册:2006-11-16
收藏
得分:0 
果然 有思想


全国最大的 Java专业电子书免费分享[url]http:///in.asp?id=xrmao[/url]
2007-11-14 21:26
枯牛
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-10-23
收藏
得分:0 
8楼的方法是不错,但如果我要执行的结果呢,例如select * from 表名 这个查询结果呢,你还可以用那个方法吗?
各有利弊而已

有项目的,可以和我联系,大家共同进步!
2007-11-15 14:48
快速回复:求ASP.NET三层结构
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015326 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved