数据访问层:
给你几个我的函数吧:
public
static
void
Open()
{
if
(con.State
==
ConnectionState.Closed)
{
con.Open()
}
}
public
static
void
Close()
{
if
(con.State
==
ConnectionState.Open)
{
con.Close()
}
}
public
static
SqlCommand
CreateCommand(string
sql)
{
return
new
SqlCommand(sql,
con)
}
public
static
List<string>
RunSQLReturnList(string
sql)
{
List<string>
info
=
new
List<string>()
SqlCommand
cmd
=
CreateCommand(sql)
Open()
SqlDataReader
sdr
=
cmd.ExecuteReader()
while
(sdr.Read())
{
for
(int
i
=
0
i
<
sdr.FieldCount
i++)
{
info.Add(sdr.GetName(i).ToString())
info.Add(sdr.GetValue(i).ToString())
}
}
Close()
return
info
}
public
static
Hashtable
RunSQLReturnHashTable(string
sql)
{
List<string>
lt
=
RunSQLReturnList(sql)
Hashtable
ht
=
new
Hashtable()
for
(int
i
=
0
i
<
lt.Count
i
+=
2)
{
ht[lt[i]]
=
lt[i
+
1]
}
return
ht
}
数据访问层返回给逻辑层HashTable对象hashTable1:
user.username=hashTable1["Name"].ToString()
.........
逻辑层返回给表示层实体对象user
label1.text
=
user.UserName
给你来个实在点的例子吧。你就明白了。1.数据层
Public
class
DataBase
{
public
DataSet
getData(string
strSql)
{
SqlcConnection
conn
=new
SqlcConnection("server=.database=pubsuid=sapwd=sa")
SqlDataAdapter
sda
=
new
SqlDataAdapter(strSql,conn)
DataSet
ds
=
new
DataSet()
sda.Fill(ds)
return
ds
}
}
实体类
public
class
UserInfo
{
//我只定义这两个变量意思下,属性你自己写
private
string
userName
private
string
pwd
}
2.业务逻辑层
public
class
Operation
{
public
bool
loginValidate(UserInfo
ui)
{
DataBase
db
=
new
DataBase()
DataSet
ds
=
new
DataSet()
ds
=
db.getData("Select
*
from
UserInfo")
foreach(DataRow
dr
in
ds.Tables[0].Rows)
{
if(dr["userName"]==ui.UserName
&&
dr["pwd"]==ui.Pwd)
return
true
}
return
false
}
}
3模型层,即界面层
//定义实体类对象并为它赋值
UserInfo
ui
=
new
UserInfo()
ui.UserName=Session["userName"]
ui.Pwd=Session["Pwd"]
//实例化 *** 作类
Operation
op
=
new
Operation()
if(op.loginValidate(ui)==true)
{
//写下你想写的代码
Response.Write("登录成功")
}
这是最基础的三层模式,我想应该可以完全了解了吧。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)