直播中
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>
/// 屬性:數(shù)據(jù)庫存儲過程的個數(shù)(stat>0)
/// </summary>
public int StroeProcedureCount
{
get
{
if (this._stroeprocedurecount !=-1)
{
return this._stroeprocedurecount;
}
else
{
return this.GetStroeProcedures().Count;
}
}
}
/// <summary>
/// 屬性:數(shù)據(jù)庫用戶表的個數(shù)
/// </summary>
public int TablesCount
{
get
{
if(this._tablecount !=-1)
{
return this._tablecolscount;
}
else
{
return this.GetTables().Count;
}
}
}
#endregion
#region structure of class
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="ConnectionString">數(shù)據(jù)庫連接字符串,string</param>
public DataAccessSQL(string ConnectionString)
{
this.connectionString=ConnectionString;
this.conn =new SqlConnection(this.connectionString);
}
#endregion
#region Methods of class
/// <summary>
/// 獲得數(shù)據(jù)庫的所有表對象
/// </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 ";
//獲得指定數(shù)據(jù)庫中的所有用戶表的名稱和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 sptable;
}
catch(SqlException se)
{
throw(se);
}
}
/// <summary>
/// 獲得數(shù)據(jù)庫的存儲過程的名稱及ID列表
/// </summary>
/// <returns>HasTable</returns>
public Hashtable GetStroeProcedures()
{
try
{
//驗證連接
if(conn!=null && conn.State!=ConnectionState.Closed)
{
conn.Open();
}
else
{
conn= new SqlConnection(this.connectionString);
conn.Open();
}
Hashtable sptable=new Hashtable();
string Query = " SELECT name, id FROM sysobjects WHERE (type = 'p') AND (status > 0) ";
//獲得指定數(shù)據(jù)庫中的所有用戶存儲過程的名稱和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._stroeprocedurecount =sptable.Count;
return sptable;
}
catch(SqlException se)
{
throw(se);
}
catch(Exception e)
{
throw(e);
}
finally
{
if(conn.State==ConnectionState.Open )
conn.Close();
}
}
/// <summary>
///獲得數(shù)據(jù)庫的指定表的列對象定義
/// </summary>
/// <param name="spname">表名稱</param>
/// <returns>DataSet</returns>
public DataSet getTableColumns(string spname)
{
try
{
Int32 spid=-1; //指定表的ID號初始
//驗證連接
if(conn!=null && conn.State!=ConnectionState.Closed)
{
conn.Open();
}
else
{
conn= new SqlConnection(this.connectionString);
conn.Open();
}
//獲取指定表名的ID號
SqlCommand comm= new SqlCommand("SELECT id FROM dbo.sysobjects WHERE name = '"+spname,conn);
SqlDataReader reader=comm.ExecuteReader(CommandBehavior.CloseConnection);
while(reader.Read())
{
spid = reader.GetInt32(0);
}
reader.Close();
//驗證ID
if(spid==0 ||spid==-1)
throw new Exception ("StroedProcedure is not existed!");
//獲得表的列定義
return getTableColumns(spid);
}
catch(SqlException se)
{
throw(se);
}
finally
{
if(conn.State ==ConnectionState.Open)
conn.Close();
}
}
/// <summary>
/// 獲得數(shù)據(jù)庫的指定表的列對象定義的列數(shù)組
/// </summary>
/// <param name="spid">表名稱</param>
/// <returns>DataSet</returns>
public DataSet getTableColumns(Int32 spid)
{
try
{
DataSet myDataSet=new DataSet();
//驗證連接
if(conn!=null && conn.State!=ConnectionState.Closed)
{
conn.Open();
}
else
{
conn= new SqlConnection(this.connectionString);
conn.Open();
}
SqlDataAdapter comm= new SqlDataAdapter("SELECT dbo.syscolumns.name, dbo.systypes.name AS Type, dbo.syscolumns.length,dbo.syscolumns.isoutparam, dbo.syscolumns.isnullable FROM dbo.syscolumns INNER JOIN dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xtype WHERE dbo.syscolumns.id ='"+spid+"'",conn);
//獲取指定表的列定義
comm.Fill(myDataSet,"dbo.systypes");
comm.Fill(myDataSet,"dbo.systypes");
/*this._tablecolscount=myDataSet.Tables[0].Rows.Count;
DataColumn[] dcols=new DataColumn[5];
dcols[0]=myDataSet.Tables["dbo.syscolumns"].Columns["name"];
dcols[1]=myDataSet.Tables["dbo.systypes"].Columns["Type"];
dcols[2]=myDataSet.Tables["dbo.syscolumns"].Columns["length"];
dcols[3]=myDataSet.Tables["dbo.syscolumns"].Columns["isoutparam"];
dcols[3]=myDataSet.Tables["dbo.syscolumns"].Columns["isnullable"];*/
return myDataSet;
}
catch(SqlException se)
{
throw(se);
}
finally
{
if(conn.State ==ConnectionState.Open)
{
conn.Close();
}
}
}
/// <summary>
/// 為傳入SqlCommand對象建立存儲過程的參數(shù)數(shù)組
/// </summary>
/// <remarks >參數(shù)只付值ParameterName,SqlDbType,IsNullable,Direction</remarks>
/// <param name="sqlcommand">SqlCommand</param>
/// <returns>SqlCommand</returns>
public SqlCommand getStroeProcedureParamsByName(SqlCommand sqlcommand)//, string spname)
{
try
{
Int32 spid=-1; //初始化存儲過程的ID
//驗證連接
if(conn!=null && conn.State!=ConnectionState.Open)
{
conn.Open();
}
else
{
conn= new SqlConnection(this.connectionString);
conn.Open();
}
//獲取存儲過程的名稱
string spname=sqlcommand.CommandText;
//獲取存儲過程的ID號
SqlCommand comm= new SqlCommand("SELECT id FROM dbo.sysobjects WHERE name = '"+spname+"'",conn);
SqlDataReader reader=comm.ExecuteReader(CommandBehavior.CloseConnection);
while(reader.Read())
{
spid = reader.GetInt32(0);
}
//驗證ID號
if(spid==0 ||spid==-1)
throw new Exception ("StroedProcedure is not existed!");
//創(chuàng)建參數(shù)數(shù)組
return getStroeProcedureParamsByID( sqlcommand ,spid);
}
catch(SqlException se)
{
throw(se);
}
finally
{
if(conn.State ==ConnectionState.Open)
{
conn.Close();
}
}
}
/// <summary>
///為傳入SqlCommand對象建立存儲過程的參數(shù)數(shù)組
/// </summary>
/// <remarks >參數(shù)只付值ParameterName,SqlDbType,IsNullable,Direction</remarks>
/// <param name="sqlcommand">SqlCommand</param>
/// <param name="spid">存儲過程ID</param>
/// <returns>SqlCommand</returns>
public SqlCommand getStroeProcedureParamsByID(SqlCommand sqlcommand, Int32 spid)
{
try
{
//獲取存儲過程相關(guān)表的isnullable定義
Hashtable dependtble=this.SPDependonTable(spid);
DataSet myDataSet=new DataSet();
//驗證連接
if(conn!=null && conn.State!=ConnectionState.Open)
{
conn.Open();
}
else
{
conn= new SqlConnection(this.connectionString);
conn.Open();
}
//獲取指定存儲過程的參數(shù)內(nèi)容
SqlDataAdapter comm= new SqlDataAdapter("SELECT dbo.syscolumns.name, dbo.systypes.name AS Type, dbo.syscolumns.length,dbo.syscolumns.isoutparam FROM dbo.syscolumns INNER JOIN dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xtype WHERE dbo.syscolumns.id ="+spid,conn);//, dbo.syscolumns.isnullable
comm.Fill(myDataSet,"dbo.syscolumns");
comm.Fill(myDataSet,"dbo.systypes");
int paramcount = myDataSet.Tables[0].Rows.Count;
for(int i=0;i<paramcount;i++)
{
//參數(shù)名稱
string pname=myDataSet.Tables["dbo.syscolumns"].Rows[i]["name"].ToString();//.ToString();
//參數(shù)的SqlDBType類型定義
SqlDbType ptp=this.getSqlDbType(myDataSet.Tables["dbo.systypes"].Rows[i]["Type"].ToString());
//參數(shù)的DBType類型定義
//DbType dtp=this.getDbType(myDataSet.Tables["dbo.systypes"].Rows[i]["Type"].ToString());
//參數(shù)的長度定義
int flength=Convert.ToInt32(myDataSet.Tables["dbo.syscolumns"].Rows[i]["length"]);
//創(chuàng)建一個參數(shù)
sqlcommand.Parameters.Add(pname,ptp,flength);
//定義參數(shù)可否為空值,由相關(guān)表的isnullable定義得到
sqlcommand.Parameters[pname].IsNullable =(Boolean)dependtble[pname];
//sqlcommand.Parameters[pname].DbType =dtp;
//定義參數(shù)的INPUT和OUTPUT
if((int)(myDataSet.Tables["dbo.syscolumns"].Rows[i]["isoutparam"])==1)
{
sqlcommand.Parameters[pname].Direction =ParameterDirection.Output;
}
else
{
sqlcommand.Parameters[pname].Direction =ParameterDirection.Input;
}
}
this._spparamcount=paramcount;
return sqlcommand;
}
catch(SqlException se)
{
throw(se);
}
finally
{
if(conn.State ==ConnectionState.Open)
{
conn.Close();
}
}
}