62,051
社区成员
发帖
与我相关
我的任务
分享
public static DataView GetDataView(string strSQL)
{
//查询数据,取得数据视图
OleDbDataAdapter OleAdp = new OleDbDataAdapter(strSQL, conn);
try
{
openConnection();
DataSet Rs = new DataSet();
OleAdp.Fill(Rs);
return Rs.Tables[0].DefaultView;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
if (OleAdp != null)
{
OleAdp.Dispose();
closeConnection();
}
}
}
using(var conn = new SqlConnection(......))
{
conn.Open();
....................
}
这类代码,每一次使用是要 new 一个实例出来。
不要胡乱使用静态变量。 //公司新闻
string strqry = "Select top 5 * From T_News where Newstypeid=1 order by id desc";
DataView dv = DBFun.GetDataView(strqry);
rp_News1.DataSource = dv;
rp_News1.DataBind();
//行业新闻
strqry = "Select top 5 * From T_News where Newstypeid=2 order by id desc";
dv = DBFun.GetDataView(strqry);
rp_News2.DataSource = dv;
rp_News2.DataBind();
//友情链接
//strqry = "Select * from T_friendUrl order by sitesort asc";
//dv = DBFun.GetDataView(strqry);
//rp_Friend.DataSource = dv;
//rp_Friend.DataBind();
//最新展示
strqry = "Select top 9 * from T_Product order by id desc";
dv = DBFun.GetDataView(strqry);
rp_ProductList.DataSource = dv;
rp_ProductList.DataBind();