using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;public abstract class DbHelper
{
//数据库连接字符串(web.config来配置)
protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
public DbHelper()
{ } #region 公用方法 public static int GetMaxID(string FieldName, string TableName)
{
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
object obj = GetSingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.Parse(obj.ToString());
}
}
public static bool Exists(string strSql, params SqlParameter[] cmdParms)
{
object obj = GetSingle(strSql, cmdParms);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
。。
#endregion
}
原来一直用public static class DBHelper
调用方便,为什么我看到有的操作类用abstract,而且里面的方面也全部是实现的,我记得抽像类里面方法不是不用实现的吗?如果定义为抽像类那么如何调用,难倒在写一个类继成它,那不是重了吗
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;public abstract class DbHelper
{
//数据库连接字符串(web.config来配置)
protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
public DbHelper()
{ } #region 公用方法 public static int GetMaxID(string FieldName, string TableName)
{
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
object obj = GetSingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.Parse(obj.ToString());
}
}
public static bool Exists(string strSql, params SqlParameter[] cmdParms)
{
object obj = GetSingle(strSql, cmdParms);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
。。
#endregion
}
原来一直用public static class DBHelper
调用方便,为什么我看到有的操作类用abstract,而且里面的方面也全部是实现的,我记得抽像类里面方法不是不用实现的吗?如果定义为抽像类那么如何调用,难倒在写一个类继成它,那不是重了吗
不可以有实现,只能是子类去override
抽像类中是可以有实现方法的,查看一个抽像类跟接口的区别就很明白了
可能开始只有一种数据库,比如access,后面又增加了sqlserver,所以就抽象出一个类来。