数据访问类的封装:
数据访问类分为三大类:连接类,实体类,数据访问类。
1、连接类:
public class lianjie
{ private static string connection="server=.;database=mydb;uid=sa;pwd=789234178";
public static Sqlconnection conn
{ get
{
SqlConnection conn=new Sqlconnection(connecction);
return conn; }
}
}
2、实体类: private string code; 将属性进行封装
3、数据访问类:
public class 表名DA
{
private SqlConnection conn; private Sqlcommand cmd;
private SqlDataReader dr;
public teseDA()
{
conn=lianjie.conn; cmd=conn.CreateCommand();
}
public List<表名>selectall() 查询表所有
{
cmd.CommandText="select*from 表名";
conn.Open();
dr=cmd.ExecuteReader();
List<test>list=new List<test>();
while(dr.Read())
{
test data=new test();
data.Code=dr["Code"].ToString();
data.Name=dr["Name"].ToString();
data.Pwd=dr["pwd"].ToString();
list.Add(data);
}
conn.Close(); return list;
} }