日期:2009-09-13 浏览次数:20440 次
namespace zyq.DBMapping
{
/// <summary>
/// 对SQL server进行操作
/// </summary>
public class DataAccessSQL
{
#region class variables
private String connectionString;
private int _tablecount=-1;
private int _stroeprocedurecount=-1;
private SqlConnection conn=null;
#endregion
#region properties of class
/// <summary>
/// 属性:数据库存储过程的个数(stat>0)
/// </summary>
public int StroeProcedureCount
{
get
{
if (this._stroeprocedurecount !=-1)
{
return this._stroeprocedurecount;
}
else
{
return this.GetStroeProcedures().Count;
}
}
}
/// <summary>
/// 属性:数据库用户表的个数
/// </summary>
public int TablesCount
{
get
{
if(this._tablecount !=-1)
{
return this._tablecolscount;
}
else
{
return this.GetTables().Count;
}
}
}
#endregion
#region structure of class
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ConnectionString">数据库连接字符串,string</param>
public DataAccessSQL(string ConnectionString)
{
this.connectionString=ConnectionString;
this.conn =new SqlConnection(this.connectionString);
}
#endregion
#region Methods of class
/// <summary>
/// 获得数据库的所有表对象
/// </summary>
/// <returns>System.Data.SqlClient.SqlDataReader</returns>
public Hashtable GetTables()
{
try
{
Hashtable sptable=new Hashtable();
//验证连接
if(conn!=null && conn.State!=ConnectionState.Open)
{
conn.Open();
}
else
{
conn= new SqlConnection(this.connectionString);
conn.Open();
}
string Query = " select name, Id from sysobjects where (type='U') and (name <> 'dtproperties') order by name ";
//获得指定数据库中的所有用户表的名称和ID
SqlCommand comm= new SqlCommand(Query,conn);
SqlDataReader reader=comm.ExecuteReader(CommandBehavior.CloseConnection);
//录制Hashtable
while(reader.Read())
{
sptable.Add(reader.GetInt32(1),reader.GetString(0));
}
this._tablecount =sptable.Count;
return sptab