public static DataSet GetDateSet(string sql)
{
try
{
sqlconn = new SqlConnection(configstring);
SqlDataAdapter sqldrp = new SqlDataAdapter(sql, sqlconn);
DataSet ds = new DataSet();
sqldrp.Fill(ds);
return ds;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
sqlconn.Close();
}
}
public List<productTable> GetExectSet()
{
string sql = "select * from productTable";
List<productTable> prolist = new List<productTable>();
DataSet det = DBHeriper.GetDateSet(sql);
foreach(DataRow dr in det.Tables[0].Rows)
{
productTable prot = new productTable();
prot.Productid = (int)dr["productid"];
prot.Producttitle = (string)dr["producttitle"];
prot.Productcontent = (string)dr["productcontent"];
prot.Producttime = (string)dr["producttime"];
prot.Cliks = (int)dr["cliks"];
prolist.Add(prot);
}
return prolist;
}
2.用DataTable+ SqlDataAdapter读取数据
public static DataTable GetTableReadAll(string sql)
{
try
{
sqlconn = new SqlConnection(configstring);
sqlconn.Open();
DataSet dst = new DataSet();
SqlDataAdapter sdp = new SqlDataAdapter(sql, sqlconn);
/*
* 1)据查询结果集的架构创建dst的架构
* 2)将查询结果集中的数据填充到dst中
*/
sdp.Fill(dst);
return dst.Tables[0];
}
catch (SqlException ex)
{
throw ex;
}
finally
{
sqlconn.Close();
}
}
public List<ManagementTable> GetExectReadall()
{
List<ManagementTable> managelist = new List<ManagementTable>();
string sql = string.Format("select *from ManagementTable");
DataTable dts = DBHrepier.GetTableReadAll(sql);
foreach(DataRow row in dts.Rows)
{
ManagementTable manage = new ManagementTable();
manage.Mentid = (int)row["mentid"];
manage.Menttitle = (string)row["menttitle"];
manage.Mentcontent = (string)row["mentcontent"];
manage.Menttime = (string)row["menttime"];
manage.Cliks=(int)row["cliks"];
managelist.Add(manage);
} return managelist;
}
3.用SqlDataReader读取数据
public static SqlDataReader GetExecReaderAll(string sql)
{
try
{
sqlcon = new SqlConnection(stringConfigs);
sqlcon.Open();
SqlCommand sqlcom = new SqlCommand(sql,sqlcon);
SqlDataReader sqldr = sqlcom.ExecuteReader();
return sqldr;
}
catch (SqlException ex)
{
throw ex;
}
}
public List<EngineeringInfo> GetExecall()
{
string sql = "select*from Engineering";
List<EngineeringInfo> englist = new List<EngineeringInfo>();
SqlDataReader sqldr = DBHerpier.GetExecReaderAll(sql);
while (sqldr.Read())
{
EngineeringInfo engingo = new EngineeringInfo();
engingo.Eid = sqldr.GetInt32(0);
engingo.Ename = sqldr.GetString(1);
engingo.Pic = sqldr.GetString(2);
engingo.Ebewrite = sqldr.GetString(3);
engingo.Formula = sqldr.GetString(4);
engingo.Completion =sqldr.GetString(5);
englist.Add(engingo);
}
return englist;
}