一份礼物: 自动填充SqlCommand.Parameters的类(1)

//---------------------------------------------------
//日期: 2002.1.10
//作者: raxzhang
//版权: raxzhang
//环境: Microsoft Visual Studio.Net 7.0
//语言: Visual C#
//类型: 类文件,编译后为.dll文件
//描述: 这是作为对数据操作的最常用的属性和方法类。
//       是一个基础类。可以被继承和扩展。
//注意: 使用这个类的条件是-1.dbo用户。2.存储过程的
//       参数名与表的字段名相差一个@
//---------------------------------------------------
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;   

namespace zyq.DBMapping
{
    ///
    /// 对SQL server进行操作
    ///

    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
        ///
        /// 属性:数据库存储过程的个数(stat>0)
        ///

        public int StroeProcedureCount
        {
            get
            {
                if (this._stroeprocedurecount !=-1)
                {
                    return this._stroeprocedurecount;
                }
                else
                {
                    return this.GetStroeProcedures().Count;
                }
            }
        }
        ///
        /// 属性:数据库用户表的个数
        ///

        public int TablesCount
        {
            get
            {
                if(this._tablecount !=-1)
                {
                    return this._tablecolscount;
                }
                else
                {
                    return this.GetTables().Count;
                }
             }

        }
        #endregion
        #region structure of class
        ///
        /// 构造函数
        ///

        /// 数据库连接字符串,string
        public DataAccessSQL(string ConnectionString)
        {
            this.connectionString=ConnectionString;
            this.conn =new SqlConnection(this.connectionString);
        }
        #endregion
        #region Methods of class  
        ///
        /// 获得数据库的所有表对象
        ///

        /// System.Data.SqlClient.SqlDataReader
        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 sptable;
            }
            catch(SqlException se)
            {
                throw(se);
            }
        }
        ///
        /// 获得数据库的存储过程的名称及ID列表
        ///

        /// HasTable
        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) ";
                //获得指定数据库中的所有用户存储过程的名称和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();
            }
        }
        ///
        ///获得数据库的指定表的列对象定义
        ///

        /// 表名称
        /// DataSet
        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();
            }

        }
        ///
        /// 获得数据库的指定表的列对象定义的列数组
        ///

        /// 表名称 共3页: 上一页 1 [2] [3] 下一页

你可能感兴趣的:(.net,数据库,exception,string,query,microsoft,sql,server)