标签:
1、先建立一个名叫mydb.accdb的access数据库
2、他它复制到webform中,放在App_Data文件夹下。
3、在App_Code文件夹下建好封装语句,查询方法,连接语句,其中studentDA中的代码为:
private OleDbConnection conn; private OleDbCommand cmd; public studentDA() { conn = new DBconnection().conn; cmd = conn.CreateCommand(); } public List<studentdata> select() { List<studentdata> list = null; cmd.CommandText = "select *from student"; conn.Open(); OleDbDataReader dr = cmd.ExecuteReader(); if(dr.HasRows) list = new List<studentdata>(); while (dr.Read()) { studentdata data = new studentdata(); data.Code = (int)dr["code"]; data.Nam = dr["Nam"].ToString(); data.Sex = dr["Sex"].ToString(); data.Tizhong = decimal.Parse(dr["Tizhong"].ToString()); data.Age = int.Parse(dr["Age"].ToString()); data.Yuwen = decimal.Parse(dr["Yuwen"].ToString()); data.Shuxue = decimal.Parse(dr["Shuxue"].ToString()); data.Yingyu = decimal.Parse(dr["Yingyu"].ToString()); data.Banji = dr["Banji"].ToString(); list.Add(data); } conn.Close(); return list; }
DBconnection中的代码为:
public OleDbConnection conn; public DBconnection() { conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+HttpContext.Current.Server.MapPath("~/App_Data/mydb.accdb")); }
4、在网页中放个gridview用来显示数据库中查出来的内容。
标签:
原文地址:http://www.cnblogs.com/jiangshuai52511/p/5340183.html