C# 动态连接库之数据库连接

namespace ToolsLoad
{
    namespace SQL
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        public class Link
        {
            #region  代码中用到的变量
            string LINK = "";
            SqlConnection CON;
            #endregion

            #region  传入连接字符串
            /// <summary>
            /// SQL数据库的连接字符串
            /// </summary>
            /// <param name="Link">连接字符串</param>
            public Link(string Link)
            {
                LINK = Link;
                CON = new SqlConnection(LINK);
            }
            #endregion

            #region  传入连接条件
            /// <summary>
            /// SQL数据库的连接条件
            /// </summary>
            /// <param name="server">服务连接地址</param>
            /// <param name="Name">数据库名</param>
            /// <param name="uid">数据库登录名</param>
            /// <param name="pwd">数据库密码</param>
            public Link(string server, string Name, string uid, string pwd)
            {
                LINK = "server=" + server + ";database=" + Name + ";uid=" + uid + ";pwd=" + pwd;
                CON = new SqlConnection(LINK);
            }
            #endregion

            #region  传入连接条件
            /// <summary>
            /// SQL数据库的连接条件
            /// </summary>
            /// <param name="dataSource">数据源</param>
            /// <param name="Name">数据库名</param>
            /// <param name="Security"></param>
            public Link(string dataSource, string Name, string Security)
            {
                LINK = "Data Source=" + dataSource + ";Initial Catalog=" + Name + ";Integrated Security=" + Security;
                CON = new SqlConnection(LINK);
            }
            #endregion

            #region  建立与SQL数据库的连接
            /// <summary>
            /// 建立与SQL数据库的连接
            /// </summary>
            /// <returns>返回取得的连接</returns>
            public SqlConnection ConOpen()
            {
                try
                {
                    CON.Open();
                }
                catch (Exception e)
                { throw e; }
                return CON;
            }
            #endregion

            #region  断开与SQL数据库的连接
            /// <summary>
            /// 断开与SQL数据库的连接
            /// </summary>
            /// <param name="con">需要关闭的连接</param>
            public void ConClose(SqlConnection con)
            {
                try
                {
                    con.Close();
                }
                catch (Exception e)
                { throw e; }
            }
            #endregion
        }
    }
}

posted on 2010-04-13 22:30  VictorShan  阅读(396)  评论(0编辑  收藏

导航

统计