时间:2013-10-14 作者:悬浮的青春 分类: dbbase
sql查询结果保存为list<>列表用于本地查询
这么做可以有效的减少查询数据库次数。
另外有些code和name的关联查询可以很方便的出来。
不用再次查询数据库了。
执行效率也高。
没了。下面贴代码吧。
主方法中
用下面的语句对数据库中需要保存为list的表进行查询并得到datatable
string sql = "select maptypecode, maptypename from codemaptype";
string strconn = "Data Source=121;User ID=22;Password=22";
OracleConnection oraConn = new OracleConnection(strconn);
oraConn.Open();
OracleDataAdapter Da = new OracleDataAdapter(sql, oraConn);
DataSet Ds = new DataSet();
DataTable Dt = new DataTable();
Da.Fill(Ds, "codemaptype");
Dt = Ds.Tables[0];
然后再执行list的填充操作
List<MapType> maptypelist = new List<MapType>();
int row = Dt.Rows.Count;
for (int r = 0; r < row; r++)
{
MapType thememaptype = new MapType();
thememaptype.MapTypeCode = Dt.Rows[r][0].ToString();
thememaptype.MapTypeName = Dt.Rows[r][1].ToString();
maptypelist.Add(thememaptype);
}
其中的MapType是我建的一个实体类。内容如下:
/// <summary>
/// 专题图类型
/// </summary>
public class MapType
{
private string maptypecode;
/// <summary>
/// 类型编码
/// </summary>
public string MapTypeCode
{
get { return maptypecode; }
set { maptypecode = value; }
}
private string maptypename;
/// <summary>
/// 类型名称
/// </summary>
public string MapTypeName
{
get { return maptypename; }
set { maptypename = value; }
}
}
new一个全局变量 public static List<MapType> Maptypelist; 用来保存之前遍历得到的list
Maptypelist = maptypelist;
执行通过名称查询list中的code
Console.WriteLine(findcodebyname("行政区划"));
其中findcodebyname的方法如下:
public static string findcodebyname(string maptypename)
{
MapTypeFinder mtf = new MapTypeFinder();
mtf.m_MapTypeName = maptypename;
MapType tmt = Maptypelist.Find(mtf.FindMapTypeByName);
return tmt.MapTypeCode;
}
至于findmaptypebyname方法可以自己写一个通过code获取name的方法。
标签: net