Ejemplo n.º 1
0
        } //ExecuteQueryToDataTable(string sql)

        /// <summary>
        /// 执行查询返回DataReader
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="Conn">连接对象</param>
        /// <returns>成功时返回Reader对象,失败时返回null</returns>
        public static System.Data.IDataReader ExecuteQuery(string sql, System.Data.Common.DbConnection Conn, System.Data.Common.DbParameter[] param = null)
        {
            System.Data.IDataReader reader = null;
            if (Conn == null)
            {
                return(null);
            }
            try
            {
                if (Conn.State == System.Data.ConnectionState.Closed)
                {
                    Conn.Open();
                }
                System.Data.IDbCommand cmd = Conn.CreateCommand();
                cmd.CommandText = sql;
                if (param != null)
                {
                    for (int i = 0; i < param.Length; i++)
                    {
                        cmd.Parameters.Add(param[i]);
                    }
                }

                reader = cmd.ExecuteReader();
                return(reader);
            }
            catch (Exception ex)
            {
                Dbhelper.ErrLog("Dbhelper.ExecuteQuery(string sql, System.Data.Common.DbConnection Conn):" + ex.Message);
                return(null);
            }
        } //ExecuteQuery(string sql)
Ejemplo n.º 2
0
        } //Execute(string sql)

        /// <summary>
        /// 执行Sql语句
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="Conn">数据库连接对象</param>
        /// <param name="param">参数</param>
        /// <returns>返回受影响行数</returns>
        public static int Execute(string sql, System.Data.Common.DbConnection Conn, System.Data.Common.DbParameter[] param)
        {
            if (Conn == null)
            {
                Dbhelper.ErrLog(
                    "Dbhelper.Execute(string sql, System.Data.Common.DbConnection Conn, System.Data.Common.DbParameter[] param):连接对象为空!");
                return(0);
            }
            System.Data.IDbCommand cmd = Conn.CreateCommand();
            cmd.CommandText = sql;
            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
            }
            try
            {
                return(cmd.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                Dbhelper.ErrLog(
                    "Dbhelper.Execute(string sql, System.Data.Common.DbConnection Conn, System.Data.Common.DbParameter[] param):" +
                    ex.Message + "/nsql=" + sql);
                return(0);
            }
        } //Execute(string sql,System.Data.IDataParameter[] param)
Ejemplo n.º 3
0
        } //Execute(string sql,System.Data.IDataParameter[] param)

        /// <summary>
        /// 执行一个事务
        /// </summary>
        /// <param name="sqls">Sql语句组</param>
        /// <returns>成功时返回true</returns>
        public static bool ExecuteTrans(string[] sqls, System.Data.Common.DbConnection Conn)
        {
            System.Data.IDbTransaction myTrans;
            if (Conn == null)
            {
                Dbhelper.ErrLog("Dbhelper.ExecuteTrans(string[] sqls):连接对象为空!");
                return(false);
            }
            System.Data.IDbCommand cmd = Conn.CreateCommand();
            myTrans         = Conn.BeginTransaction();
            cmd.Transaction = myTrans;
            try
            {
                foreach (string sql in sqls)
                {
                    if (sql != null)
                    {
                        cmd.CommandText = sql;

                        cmd.ExecuteNonQuery();
                    }
                }
                myTrans.Commit();
            }
            catch (Exception ex)
            {
                myTrans.Rollback();
                Dbhelper.ErrLog("Dbhelper.ExecuteTrans(string[] sqls):" + ex.Message);
                return(false);
            }
            return(true);
        } //Execute(string sql)
Ejemplo n.º 4
0
        /// <summary>
        /// 得到连接字符串
        /// </summary>
        /// <returns>连接字符串</returns>
        private static string getConnString(string key)
        {
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings[key].ToString();

            if (connStr == null || connStr == "")
            {
                Dbhelper.ErrLog("Dbhelper.getConnString(string key):[" + key + "]所指定的连接类型为空");
            }
            return(connStr);
        }
Ejemplo n.º 5
0
        } //ExecuteQuery(string sql)

        /// <summary>
        /// 执行Sql语句
        /// </summary>
        /// <param name="sql">Sql语句</param>
        /// <param name="Conn">数据库连接对象</param>
        /// <returns>返回受影响行数</returns>
        public static int Execute(string sql, System.Data.Common.DbConnection Conn)
        {
            if (Conn == null)
            {
                Dbhelper.ErrLog("Dbhelper.Execute(string sql, System.Data.Common.DbConnection Conn):连接对象为空!");
                return(0);
            }
            System.Data.IDbCommand cmd = Conn.CreateCommand();

            cmd.CommandText = sql;
            try
            {
                return(cmd.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                Dbhelper.ErrLog("Dbhelper.ExecuteQuery(string sql, System.Data.Common.DbConnection Conn):" + ex.Message +
                                "/nsql=" + sql);
                return(0);
            }
        } //Execute(string sql)