一段自定义DataTable代码

2019-04-15 19:18发布

DataTable sourse = new DataTable();
sourse.Columns.Add(new DataColumn("TypeID"));
sourse.Columns.Add(new DataColumn("TypeName"));
sourse.Columns.Add(new DataColumn("pic"));
string sqltype = "select distinct TypeID from Cartoon_Pic";
DataTable dttype = DataAccess.GetDataTable(sqltype);
for (int i = 0; i < dttype.Rows.Count; i++)
{
      string typeid = dttype.Rows[i]["TypeID"].ToString();
      string path = PubMethod.GetPath("72");
      string sql = "select top 1 a.TypeID,b.TypeName,'" + path + "'+b.TypeName+'/'+a.Pic as pic from Cartoon_Pic as a,Cartoon_Type as b where a.TypeID='" + typeid + "' order by a.SerialNum asc";
      DataTable dt = DataAccess.GetDataTable(sql);
      if (dt.Rows.Count > 0)
      {
           DataRow dr = sourse.NewRow();
           dr["TypeID"] = dt.Rows[0]["TypeID"].ToString();
           dr["TypeName"] = dt.Rows[0]["TypeName"].ToString();
           dr["pic"] = dt.Rows[0]["pic"].ToString();
           sourse.Rows.Add(dr);
      }
}