标题:c#如何编写一公共类,用于对数据库的连接
只看楼主
lukebc
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:74
专家分:32
注 册:2009-8-18
结帖率:71.43%
 问题点数:0 回复次数:4 
c#如何编写一公共类,用于对数据库的连接
最好是数据库只连一次,然后就可以整个工程进行操作,应该怎么编写,并且怎么调用
我的数据库连接代码是这样的
 SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "server=LUKE\\SQLEXPRESS;uid=sa;pwd=sa;database=luke";
            conn.Open();
            DataTable dt = new DataTable("resouce");
            dt.Clear();
            SqlDataAdapter adp = new SqlDataAdapter("select * from text1", conn);
            adp.Fill(dt);
            dataGridView1.DataSource = dt.DefaultView;
2016-09-02 12:20
向洪林
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:重庆市奉节县
等 级:版主
威 望:9
帖 子:118
专家分:405
注 册:2015-3-10
得分:0 
public class BaseConnectionSql//这是一个连接数据库的基类,只需要其他类继承这个基类就OK,派生类只需要调用基类的属性就OK  
{
        protected string ConnectionString//封装属性
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["connectionstring"].ToString();//这里需要在web.config中配置后再用connectionstring
            }
        }
}
//配置中只需要这样添加就行
 <connectionStrings>      
      <add name="connectionstring" connectionString="server=.;database=db_MrCy;uid=sa;pwd=123456" />//这里的connectionstring就是基类中的connectionstring
 </connectionStrings>

新手上路,望大家多多照顾,谢谢!
2016-09-02 12:44
lukebc
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:74
专家分:32
注 册:2009-8-18
得分:0 
看不懂,说详细点
2016-09-02 14:19
向洪林
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:重庆市奉节县
等 级:版主
威 望:9
帖 子:118
专家分:405
注 册:2015-3-10
得分:0 
还不够详细吗?照着我说的去做不就行了吗???难道没有学继承??

新手上路,望大家多多照顾,谢谢!
2016-09-03 10:38
沉迷敲代码
Rank: 1
等 级:新手上路
帖 子:11
专家分:2
注 册:2016-8-31
得分:0 
class DBHelper
    {
        //设置链接数据库为只读
        private static readonly string conString = @"server=.\SQL2012;database=Hotel;Integrated Security=sspi";

        #region 生成链接对象 SqlConnection CreateConnection()
        /// <summary>
        /// 生成链接对象
        /// </summary>
        /// <returns>链接对象</returns>
        public static SqlConnection CreateConnection()
        {
            SqlConnection conn = new SqlConnection(conString);
            return conn;
        }

        #endregion

        #region 生成SqlCommand对象SqlCommand CreateCommand(SqlConnection conn, string cmdText, bool isProcedure, params SqlParameter[] parameters)
        /// <summary>
        /// 生成SqlCommand对象
        /// </summary>
        /// <param name="conn">SqlCommection对象</param>
        /// <param name="cmdText">需要执行的T-SQL语句或存储过程名</param>
        /// <param name="isProcedure">标识是否是存储过程,true表示存储过程,false表示T-SQL语句</param>
        /// <param name="parameters">命令对象的参数集合</param>
        /// <returns>命令对象</returns>
        public static SqlCommand CreateCommand(SqlConnection conn, string cmdText, bool isProcedure, params SqlParameter[] parameters)
        {
            SqlCommand cmd = conn.CreateCommand();
             = cmdText;
            if (isProcedure)
                = CommandType.StoredProcedure;
            if (parameters != null)
                cmd.Parameters.AddRange(parameters);
            return cmd;
        }
        #endregion


        #region  生成DataTable对象DataTable CreateDataTable(SqlCommand cmd)
        /// <summary>
        /// 生成DataTable对象
        /// </summary>
        /// <param name="cmd">命令对象</param>
        /// <returns>数据表对象</returns>
        public static DataTable CreateDataTable(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
            dataAdapter.Fill(dt);
            return dt;
        }
        #endregion


        #region 加载信息到DataGridView列表 LoadAllInfo(string cmdstr, DataGridView dgv)
        /// <summary>
        /// 加载信息
        /// </summary>
        /// <param name="cmdstr">查询字符串</param>
        /// <param name="dgv">加载的窗口</param>
        public static void LoadAllInfo(string cmdstr, DataGridView dgv)
        {
            using (SqlConnection conn = new SqlConnection(DBHelper.conString))
            {
                string sqlselectCmdText = cmdstr;
                SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlselectCmdText, conn);
                DataTable dt = new DataTable();
                sqlAdapter.Fill(dt);
                dgv.DataSource = dt;
            }
        }
          #endregion

    }
2016-09-04 15:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-468394-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012910 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved