1、先建立一个名叫text.mdb的access数据库
2、他它复制到access中文件下。
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();
list.Add(data);
}
conn.Close();
return list;
}
DBconnection中的代码为:
public OleDbConnection conn;
public DBconnection()
{
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "text.mdb" + ";Persist Security Info=False");
}
4、在Form窗体中放个datagridview用来显示数据库中查出来的内容。