SQLHelper.cs

        /// <summary>
        ///連接數據庫字符串
        ///塗聚文 2011-09-25
        ///締友計算機信息技術有限公司
        /// </summary>
        private static string connectionString;
        
        /// <summary>
        ///
        /// </summary>
        static DBHelper()
        {
            DataSetingConfig dbconnection = new DataSetingConfig();
            dbconnection.GetConString();//讀文件內空中的連按字符串
            connectionString = DataSetingConfig.conStr;//@"Data Source=.;Initial Catalog=GeovinDuHr;User ID=sa;Password=8888888";
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="hasOpen"></param>
        /// <returns></returns>
        public static SqlConnection GetConnection(bool hasOpen)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            if (hasOpen)
                conn.Open();
            return conn;
        }
        /// <summary>
        ///
        ///塗聚文 2011-09-25
        ///締友計算機信息技術有限公司
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        public static SqlDataReader GetReader(string sql, CommandType cmdType, params SqlParameter[] sqlParams)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = cmdType;
            cmd.CommandText = sql;
            if (sqlParams != null)
                cmd.Parameters.AddRange(sqlParams);
            SqlConnection conn = GetConnection(true);
            cmd.Connection = conn;
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        /// <summary>
        ///
        ///塗聚文 2011-09-25
        ///締友計算機信息技術有限公司
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        public static Object GetObject(string sql, CommandType cmdType, params SqlParameter[] sqlParams)
        {
            object ret = null;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = cmdType;
            cmd.CommandText = sql;
            if (sqlParams != null)
                cmd.Parameters.AddRange(sqlParams);
            SqlConnection conn = GetConnection(true);
            cmd.Connection = conn;
            try
            {
                ret = cmd.ExecuteScalar();
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }
        /// <summary>
        ///
        ///塗聚文 2011-09-25
        ///締友計算機信息技術有限公司
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        public static int ExecuteSql(string sql, CommandType cmdType, params SqlParameter[] sqlParams)
        {
            int ret = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = cmdType;
            cmd.CommandText = sql;
            if (sqlParams != null)
                cmd.Parameters.AddRange(sqlParams);
            SqlConnection conn = GetConnection(true);
            cmd.Connection = conn;
            try
            {
                ret = cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }
        /// <summary>
        ///
        ///塗聚文 2011-09-25
        ///締友計算機信息技術有限公司
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        public static DataTable GetTable(string sql, CommandType cmdType, params SqlParameter[] sqlParams)
        {
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = cmdType;
            cmd.CommandText = sql;
            if (sqlParams != null)
                cmd.Parameters.AddRange(sqlParams);
            SqlConnection conn = GetConnection(true);
            cmd.Connection = conn;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }
        /// <summary>
        ///
        ///塗聚文 2011-09-25
        ///締友計算機信息技術有限公司
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        public static DataSet GetDataSet(string sql, CommandType cmdType, params SqlParameter[] sqlParams)
        {
            DataSet dt = new DataSet();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = cmdType;
            cmd.CommandText = sql;
            if (sqlParams != null)
                cmd.Parameters.AddRange(sqlParams);
            SqlConnection conn = GetConnection(true);
            cmd.Connection = conn;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }

參考: http://www.koders.com/csharp/fidD4121D6E4BCA2DAB656D770903FECBFF7427D242.aspx

posted @ 2011-09-25 18:03  ®Geovin Du Dream Park™  阅读(494)  评论(0编辑  收藏  举报