数据操作类(二)
时间:2007-06-26 来源:chenqiubite
public abstract class TData
{
protected string FTableName;
protected string FKeyName;
protected string FKeyValue; /// <summary>
/// TData类
/// </summary>
public TData()
{
} public TData(string TableName, string KeyName, string KeyValue)
{
FTableName = TableName;
FKeyName = KeyName;
FKeyValue = KeyValue;
} /// <summary>
/// (静态)根据编号在数据库中删除记录。
/// </summary>
/// <param name="ID"></param>
/// <param name="TableName"></param>
/// <param name="KeyName"></param>
public static void DeleteByID(string ID, string TableName, string KeyName)
{
string strSql = "delete from " + TableName + " where " + KeyName + "='" + ID + "'";
TSqlDB.ExecuteNonQuery(strSql);
} #region 四个基本数据操作,需要由子类重新实现的方法 /// <summary>
/// 在数据库中检索记录(需要由子类实现的方法)。
/// </summary>
public abstract void Select(); /// <summary>
/// 在数据库中删除记录。
/// </summary>
public void Delete()
{
string strSql = GetDeleteString();
TSqlDB.ExecuteNonQuery(strSql);//这是另外一个类里实现的方法
} /// <summary>
/// 在数据库中插入记录。
/// </summary>
/// <param name="ExSql"></param>
public void Insert(string ExSql)
{
string strSql = GetInsertString(ExSql);
TSqlDB.ExecuteNonQuery(strSql);
} /// <summary>
/// 在数据库中更新记录。
/// </summary>
/// <param name="ExSql"></param>
public void Update(string ExSql)
{
string strSql = GetUpdateString(ExSql);
TSqlDB.ExecuteNonQuery(strSql);
} /// <summary>
/// 组合Update查询串。
/// </summary>
/// <param name="ExSql"></param>
/// <returns></returns>
protected string GetUpdateString(string ExSql)
{
return "update " + FTableName + " set " + ExSql + " where " + FKeyName + "='" + FKeyValue + "'";
} /// <summary>
/// 组合Insert查询串。
/// </summary>
/// <param name="ExSql"></param>
/// <returns></returns>
protected string GetInsertString(string ExSql)
{
return "insert into " + FTableName + " values(" + ExSql + ")";
} /// <summary>
/// 组合Delete查询串。
/// </summary>
/// <returns></returns>
protected string GetDeleteString()
{
return "delete from " + FTableName + " where " + FKeyName + "='" + FKeyValue +"'";
} /// <summary>
/// 组合Select查询串。
/// </summary>
/// <returns></returns>
protected string GetSelectString()
{
return "select top 1 * from " + FTableName + " where " + FKeyName + "='" + FKeyValue + "'";
}
#endregion
} 这里面注意的是:由于这里的方法Select使用前缀abstract,因此导致类定义也必须加上abstract前缀。
{
protected string FTableName;
protected string FKeyName;
protected string FKeyValue; /// <summary>
/// TData类
/// </summary>
public TData()
{
} public TData(string TableName, string KeyName, string KeyValue)
{
FTableName = TableName;
FKeyName = KeyName;
FKeyValue = KeyValue;
} /// <summary>
/// (静态)根据编号在数据库中删除记录。
/// </summary>
/// <param name="ID"></param>
/// <param name="TableName"></param>
/// <param name="KeyName"></param>
public static void DeleteByID(string ID, string TableName, string KeyName)
{
string strSql = "delete from " + TableName + " where " + KeyName + "='" + ID + "'";
TSqlDB.ExecuteNonQuery(strSql);
} #region 四个基本数据操作,需要由子类重新实现的方法 /// <summary>
/// 在数据库中检索记录(需要由子类实现的方法)。
/// </summary>
public abstract void Select(); /// <summary>
/// 在数据库中删除记录。
/// </summary>
public void Delete()
{
string strSql = GetDeleteString();
TSqlDB.ExecuteNonQuery(strSql);//这是另外一个类里实现的方法
} /// <summary>
/// 在数据库中插入记录。
/// </summary>
/// <param name="ExSql"></param>
public void Insert(string ExSql)
{
string strSql = GetInsertString(ExSql);
TSqlDB.ExecuteNonQuery(strSql);
} /// <summary>
/// 在数据库中更新记录。
/// </summary>
/// <param name="ExSql"></param>
public void Update(string ExSql)
{
string strSql = GetUpdateString(ExSql);
TSqlDB.ExecuteNonQuery(strSql);
} /// <summary>
/// 组合Update查询串。
/// </summary>
/// <param name="ExSql"></param>
/// <returns></returns>
protected string GetUpdateString(string ExSql)
{
return "update " + FTableName + " set " + ExSql + " where " + FKeyName + "='" + FKeyValue + "'";
} /// <summary>
/// 组合Insert查询串。
/// </summary>
/// <param name="ExSql"></param>
/// <returns></returns>
protected string GetInsertString(string ExSql)
{
return "insert into " + FTableName + " values(" + ExSql + ")";
} /// <summary>
/// 组合Delete查询串。
/// </summary>
/// <returns></returns>
protected string GetDeleteString()
{
return "delete from " + FTableName + " where " + FKeyName + "='" + FKeyValue +"'";
} /// <summary>
/// 组合Select查询串。
/// </summary>
/// <returns></returns>
protected string GetSelectString()
{
return "select top 1 * from " + FTableName + " where " + FKeyName + "='" + FKeyValue + "'";
}
#endregion
} 这里面注意的是:由于这里的方法Select使用前缀abstract,因此导致类定义也必须加上abstract前缀。
相关阅读 更多 +