Пример #1
0
        public static int ExecuteNonQuery(string sql, SqlParameterCollection parameterCollection)
        {
            int result = 0;

            if (_dbConnect == null)
            {
                DalHelper.GetCon();
            }
            _dbConnect.Open();
            using (SqlTransaction transaction = _dbConnect.BeginTransaction())
            {
                SqlCommand sqlCommand = new SqlCommand(sql);

                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.Connection  = _dbConnect;
                sqlCommand.Transaction = transaction;
                sqlCommand.Parameters.Add(parameterCollection);
                try
                {
                    result = sqlCommand.ExecuteNonQuery();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                }
                _dbConnect.Close();
            }

            return(result);
        }
Пример #2
0
        public static DataSet ExecuteDataSet(string sql, SqlParameterCollection parameterCollection)
        {
            DataSet ds = new DataSet();

            if (_dbConnect == null)
            {
                DalHelper.GetCon();
            }
            _dbConnect.Open();
            using (SqlTransaction transaction = _dbConnect.BeginTransaction())
            {
                SqlCommand sqlCommand = new SqlCommand(sql);
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.Connection  = _dbConnect;
                sqlCommand.Transaction = transaction;
                sqlCommand.Parameters.Add(parameterCollection);
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                try
                {
                    sqlDataAdapter.Fill(ds);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                }
                _dbConnect.Close();
            }

            return(ds);
        }