免费邮箱 |加入收藏 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > .NET专区 > ASP.NET应用

ado.net快速上手实践篇(一)

时间:2011-05-21 10:43:51  来源:站内  作者:潘春会

                timeOut = sqlConn.ConnectionTimeout;
            }
            cmdTimeOut = timeOut;
        }

        #region contract method

        public DbCommand CreateDbCommd(DbConnection sqlConn, DbTransaction transaction, string sqlStr, CommandType cmdType, List<DbParameter> listParams)
        {
            DbCommand cmd = new SqlCommand();
            cmd.Connection = sqlConn;
            cmd.CommandText = sqlStr;
            cmd.CommandType = cmdType;
            if (transaction != null)
            {
                cmd.Transaction = transaction;
            }
            if (listParams != null && listParams.Count > 0)
            {
                cmd.Parameters.AddRange(listParams.ToArray());
            }
            cmd.CommandTimeout = cmdTimeOut;
            OpenConnection();
            return cmd;
        }

        public DbParameter CreateDbPrameter(string paramName, object paramValue)
        {
            SqlParameter sp = new SqlParameter(paramName, paramValue);
            return sp;
        }

        public DbDataReader ExecuteReader(string sqlStr, CommandType cmdType, List<DbParameter> listParams)
        {
            DbDataReader rdr = null;
            try
            {
                OpenConnection();
                cmd = CreateDbCommd(sqlConn, null, sqlStr, cmdType, listParams);
                rdr = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rdr;
        }

        public DataTable FillDataTable(string sqlStr, CommandType cmdType, List<DbParameter> listParams)
        {
            OpenConnection();
            DbTransaction trans = sqlConn.BeginTransaction();
            DbCommand cmd = CreateDbCommd(sqlConn, trans, sqlStr, cmdType, listParams);
            SqlDataAdapter sqlDataAdpter = new SqlDataAdapter(cmd as SqlCommand);
            DataTable dt = new DataTable();
            try

来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门