• 数据库连接的多种方式(三)


    public static string ConnectionString
        {
            get
            {
                return CFunc.ConnectionString;
                //return System.Configuration.ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
            }
        }
    
        //wen
        public static int ExecuteSqlCommand(SqlCommand sqlCommand) {
            using (SqlConnection conn = new SqlConnection(CData.ConnectionString)) {
                sqlCommand.Connection = conn;
                conn.Open();
                int result = sqlCommand.ExecuteNonQuery();
                conn.Close();
                return result;
            }
        }
    
        public static object ExecuteScalar(SqlCommand sqlCommand) {
            using (SqlConnection conn = new SqlConnection(CData.ConnectionString)) {
                sqlCommand.Connection = conn;
                conn.Open();
                object obj = sqlCommand.ExecuteScalar();
                conn.Close();
                return obj;
            }
        }
    
        public static int ExecuteSql(string sql) {
            using (SqlCommand cmd = GetSqlCommand(sql)) {
                cmd.Connection.Open();
                int i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                return i;
            }
        }
    
        ///// <summary>
        ///// 执行带参数SQL语句
        ///// </summary>
        ///// <param name="sql"></param>
        ///// <param name="cmdParms"></param>
        ///// <returns></returns>
        //public static int ExecuteNonQuery(string sql, params SqlParameter[] cmdParms) {
        //    using (SqlConnection connection = new SqlConnection(ConnectionString)) {
        //        using (SqlCommand cmd = new SqlCommand(sql, connection)) {
        //            cmd.Parameters.AddRange(cmdParms);
        //            return cmd.ExecuteNonQuery();
        //        }
        //    }
        //}
        //wen
    
        public static SqlCommand GetSqlCommand(string Sql)
        {
            System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection(CData.ConnectionString);
            SqlCommand Cmd = new SqlCommand(Sql, Conn);
            return Cmd;
        }
    
        public static SqlDataReader GetSqlDataReader(string Sql)
        {
            SqlCommand Cmd = GetSqlCommand(Sql);
            SqlDataReader DataReader = null;
            try
            {
                Cmd.Connection.Open();
                DataReader = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            }
            catch (Exception e)
            {
                Cmd.Connection.Close();
                CFunc.ResponseException(e.Message);
            }
            return DataReader;
        }
    
        public static SqlDataReader GetSqlDataReader(string sql, params SqlParameter[] sqlParameter) {
            SqlCommand cmd = GetSqlCommand(sql);
            cmd.Parameters.AddRange(sqlParameter);
            SqlDataReader dataReader = null;
            try {
                cmd.Connection.Open();
                dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            }
            catch (Exception e) {
                cmd.Connection.Close();
                throw e;
            }
            return dataReader;
        }
    
        public static System.Data.DataSet GetDataSet(string Sql)
        {
    
            SqlDataAdapter Ada = new SqlDataAdapter();
            Ada.SelectCommand = GetSqlCommand(Sql);
            System.Data.DataSet ds = new System.Data.DataSet();
            try
            {
                Ada.SelectCommand.Connection.Open();
                Ada.Fill(ds);
                Ada.SelectCommand.Connection.Close();
            }
            catch (Exception e)
            {
                Ada.SelectCommand.Connection.Close();
                CFunc.ResponseException(e.Message);
            }
            return ds;
        }
    
        public static System.Data.DataSet GetDataSet(SqlCommand sqlCommand) {
            using (SqlConnection conn = new SqlConnection(CData.ConnectionString)) {
                sqlCommand.Connection = conn;
                SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
                conn.Open();
                DataSet ds = new DataSet();
                da.Fill(ds);
                conn.Close();
                return ds;
            }
        }
    
        public static System.Data.DataTable GetDataTable(string Sql)
        {
            System.Data.DataSet ds = GetDataSet(Sql);
            System.Data.DataTable Dt = ds.Tables[0];
            ds.Dispose();
            return Dt;
        }
    
        public static object ExecuteScalar(string Sql)
        {
            SqlCommand Cmd = GetSqlCommand(Sql);
            object Obj = null;
            try
            {
                Cmd.Connection.Open();
                Obj = Cmd.ExecuteScalar();
            }
            catch (Exception e)
            {
                Cmd.Connection.Close();
                throw e;
            }
            Cmd.Connection.Close();
            return Obj;
        }
    
        public static int ExecuteNonQuery(string Sql)
        {
            SqlCommand Cmd = GetSqlCommand(Sql);
            int i = -1;
            try
            {
                Cmd.Connection.Open();
                i = Cmd.ExecuteNonQuery();
                Cmd.Connection.Close();
            }
            catch (Exception e)
            {
                Cmd.Connection.Close();
                CFunc.ResponseException(e.Message + " > ");
            }
    
            return i;
        }
        public static int ExecuteNonQuery(string sql, SqlParameter[] sqlParameters)
        {
            SqlCommand cmd = GetSqlCommand(sql);
            //foreach (SqlParameter SP in Parameters)
            //{
            //    Cmd.Parameters.Add(SP);
            //}
            cmd.Parameters.AddRange(sqlParameters);
            int i = -1;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception e)
            {
                cmd.Connection.Close();
                //CFunc.ResponseException(e.Message + " > " + sql);
                throw e;
            }
    
            return i;
        }
    
        public static string[] GetRecordOne(string StrSql, int ColumnNum)
        {
            string[] Record = new string[ColumnNum];
            System.Data.SqlClient.SqlDataReader Dr = GetSqlDataReader(StrSql);
    
            if (Dr.Read())
            {
                for (int i = 0; i < ColumnNum; i++)
                {
                    Record[i] = Dr[i].ToString();
                }
            }
            else
            {
                //CFunc.ResponseException(StrSql);
                Record = null;
            }
            Dr.Close();
            return Record;
        }
    
    
  • 相关阅读:
    Weblogic魔法堂:AdminServer.lok被锁导致启动、关闭域失败
    CSS魔法堂:盒子模型简介
    .Net魔法堂:提取注释生成API文档
    CSS魔法堂:Position定位详解
    CMD魔法堂:获取进程路径和PID值的方法集
    CentOS6.5菜鸟之旅:识别NTFS分区
    CentOS6.5菜鸟之旅:安装rpmforge软件库
    CMD魔法堂:CMD进入指定目录
    CentOS6.5菜鸟之旅:VIM插件NERDtree初探
    CSS魔法堂:选择器及其优先级
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1724060.html
Copyright © 2020-2023  润新知