使用工厂方法模式实现多数据库WinForm手机号码查询器
2010-04-08 00:00:00 来源:WEB开发网IDAL声明数据访问的接口方法
public interface Ilist
{
/*添加对象*/
int Add(CuteMobileSearch.Model.list objList);
/*返回所有*/
List<CuteMobileSearch.Model.list> GetAll();
/*根据号码查询*/
List<Model.list> GetListByNum(string num);
/*根据查询条件*/
List<CuteMobileSearch.Model.list> GetListByWhere(string strWhere);
}
SqliteDAL继承IDAL,实现对数据库的访问和操作,来访问实体类Model
/// <summary>
/// 基于Sqlite的数据访问类库
/// </summary>
public class listServices : IDAL.Ilist
{
/// <summary>
/// 添加
/// </summary>
/// <returns>返回添加数目</returns>
public int Add(CuteMobileSearch.Model.list objList)
{
StringBuilder sbSql = new StringBuilder();
sbSql.Append("INSERT INTO list(");
sbSql.Append("num,code,city,cardtype)");
sbSql.Append(" VALUES(");
sbSql.Append("@num,@code,@city,@cardtype)");
sbSql.Append(";SELECT @@IDENTITY");
SQLiteParameter[] paras ={
new SQLiteParameter("@num",DbType.String,8),
new SQLiteParameter("@code",DbType.String,8),
new SQLiteParameter("@city",DbType.String,16),
new SQLiteParameter("@cardtype",DbType.String,16)
};
paras[0].Value = objList.Num;
paras[1].Value = objList.Code;
paras[2].Value = objList.City;
paras[3].Value = objList.Cardtype;
int num = Wang.DBUtility.SqliteHelper.ExecuteCommand(sbSql.ToString());
return num;
}
/// <summary>
/// 返回List<>
/// </summary>
/// <returns></returns>
public List<CuteMobileSearch.Model.list> GetAll()
{
return GetListByWhere("");
}
/// <summary>
/// 根据号码查询
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public List<Model.list> GetListByNum(string num)
{
string strWhere = "num LIKE '" + num + "%'";
return GetListByWhere(strWhere);
}
/// <summary>
/// 根据条件返回List<>
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public List<CuteMobileSearch.Model.list> GetListByWhere(string strWhere)
{
List<CuteMobileSearch.Model.list> listList = new List<CuteMobileSearch.Model.list>();
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT * FROM list");
if (strWhere != "")
{
strSql.Append(" WHERE ");
strSql.Append(strWhere);
}
SQLiteDataReader reader = Wang.DBUtility.SqliteHelper.GetReader(strSql.ToString());
while (reader.Read())
{
CuteMobileSearch.Model.list objList = new CuteMobileSearch.Model.list();
if (reader["id"].ToString() != String.Empty)
{
objList.Id = int.Parse(reader["id"].ToString());
}
objList.Num = reader["num"].ToString();
objList.Code = reader["code"].ToString();
objList.City = reader["city"].ToString();
objList.Cardtype = reader["cardtype"].ToString();
listList.Add(objList);
}
reader.Close();
return listList;
}
}
更多精彩
赞助商链接