Ejemplo n.º 1
0
        public static object ExecuteScalar(this IngresConnection conn, string sql, IngresTransaction tx = null)
        {
            var cmd = tx == null ? new IngresCommand(sql, conn) : new IngresCommand(sql, conn, tx);

            using (cmd)
                return(cmd.ExecuteScalar());
        }
Ejemplo n.º 2
0
        private void Delete_Click(object sender, EventArgs e)
        {
            IngresConnection con = new IngresConnection(Login.myConnectionString);

            con.Open();
            IngresCommand cmd = new IngresCommand();

            cmd.Connection  = con;
            cmd.CommandText = "rol_delete";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new IngresParameter("t_id_rol", IngresType.Decimal));
            cmd.Parameters["t_id_rol"].Value = int.Parse(this.metroTextBox1.Text);
            IngresTransaction trans = con.BeginTransaction();

            cmd.Transaction = trans;
            try
            {
                cmd.ExecuteNonQuery();
                trans.Commit();
                MetroMessageBox.Show(this, "Elemento eliminado correctamente.", "Nota", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            catch (IngresException c)
            {
                MetroMessageBox.Show(this, c.ErrorCode + c.Message, "Error");
            }
            con.Close();
            this.read();
        }
        /// <summary>
        /// Gets the Id for the current application.
        /// </summary>
        /// <param name="conn">The Ingres connection to use.</param>
        /// <param name="tran">The Ingres transaction to use.</param>
        /// <returns>The Id for the current application.</returns>
        private string GetApplicationId(IngresConnection conn, IngresTransaction tran)
        {
            string id = null;

            string sql = @"
                          SELECT  
                              ApplicationId 
                          FROM 
                              aspnet_Applications 
                          WHERE LoweredApplicationName = ?
                         ";

            // Create the new command and enrol in the current transaction
            IngresCommand cmd = new IngresCommand(sql, this.conn);

            cmd.Transaction = this.tran;

            cmd.Parameters.Add("LoweredApplicationName", DbType.String).Value = this.config.ApplicationName.ToLower();

            conn.Open();

            IngresDataReader reader = cmd.ExecuteReader();

            if (reader != null)
            {
                if (reader.HasRows)
                {
                    reader.Read();

                    // Retrieve the Id
                    id = DBUtil.ColValAsString(reader, "ApplicationId");

                    reader.Close();
                }
                else
                {
                    // Close the reader.
                    reader.Close();

                    // We don't have an application so create one.
                    this.CreateApplication(this.config.ApplicationName, out id);
                }
            }

            // Mark the application Id as current so that we don't have to fetch it from the database
            // again unless it changes.
            this.config.IsApplicationIdCurrent = true;

            // Close the connection
            conn.Close();

            return(id);
        }
        /// <summary>
        /// Initializes a new instance of the IngresRoleProviderHandler class.
        /// </summary>
        /// <param name="conn">The Ingres connection to use.</param>
        /// <param name="tran">The Ingres transaction to use.</param>
        /// <param name="config">The configuration settings to use.</param>
        /// <param name="provider">The Ingres Role Provider facade to use.</param>
        internal IngresRoleProviderHandler(IngresConnection conn, IngresTransaction tran, IngresRoleProviderConfiguration config, IngresRoleProvider provider)
        {
            if (conn == null || tran == null)
            {
                throw new Exception();
            }

            this.tran = tran;
            this.conn = conn;

            this.config   = config;
            this.provider = provider;
        }
        /// <summary>
        /// This method takes a role name and determines whether the role exists.
        /// </summary>
        /// <remarks>
        /// This method overrides a method from the <c>System.Web.Security.RoleProvider</c> class to provide an Ingres
        /// specific implementation.
        /// </remarks>
        /// <param name="roleName">Role name to check the existence of.</param>
        /// <returns>Whether the given role exists.</returns>
        public override bool RoleExists(string roleName)
        {
            bool result = false;

            IngresTransaction tran = null;

            try
            {
                using (IngresConnection conn = new IngresConnection(this.config.ConnectionString))
                {
                    // Open the connection and start a new transaction
                    conn.Open();

                    tran = conn.BeginTransaction();

                    // Call the implementation of the method
                    result = this.GetHandler(conn, tran).RoleExists(roleName);

                    // Commit the transaction
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                // Attempt to rollback
                try
                {
                    if (tran != null && tran.Connection != null)
                    {
                        tran.Rollback();
                    }
                }
                catch
                {
                    // Add the rollback error.
                    ExceptionHandler.LogRollbackWarning(RoleProviderMethod.RoleExists);
                }

                // Handle the exception appropriately
                ExceptionHandler.HandleException(ex, RoleProviderMethod.RoleExists);
            }

            return(result);
        }
        /// <summary>
        /// Takes an array of user names and an array of role names and removes the specified users
        /// from the specified roles.
        /// </summary>
        /// <remarks>
        /// <para>
        /// <c>RemoveUsersFromRoles</c> throws a <c>ProviderException</c> if any of the users or roles do not
        /// exist, or if any user specified in the call does not belong to the role from which he
        /// or she is being removed.
        /// </para>
        /// <para>
        /// This method overrides a method from the <c>System.Web.Security.RoleProvider</c> class to provide an Ingres
        /// specific implementation.
        /// </para>
        /// </remarks>
        /// <param name="usernames">The array of usernames.</param>
        /// <param name="roleNames">The array of roles.</param>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            IngresTransaction tran = null;

            try
            {
                using (IngresConnection conn = new IngresConnection(this.config.ConnectionString))
                {
                    // Open the connection and start a new transaction
                    conn.Open();

                    tran = conn.BeginTransaction();

                    // Call the implementation of the method
                    this.GetHandler(conn, tran).RemoveUsersFromRoles(usernames, roleNames);

                    // Commit the transaction
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                // Attempt to rollback
                try
                {
                    if (tran != null && tran.Connection != null)
                    {
                        tran.Rollback();
                    }
                }
                catch
                {
                    // Add the rollback error.
                    ExceptionHandler.LogRollbackWarning(RoleProviderMethod.RemoveUsersFromRoles);
                }

                // Handle the exception appropriately
                ExceptionHandler.HandleException(ex, RoleProviderMethod.RemoveUsersFromRoles);
            }
        }
        /// <summary>
        /// Gets a handler for the Ingres membership provider.
        /// </summary>
        /// <param name="conn">The Ingres connection.</param>
        /// <param name="tran">The Ingres transaction to use.</param>
        /// <returns>The handler</returns>
        private IngresRoleProviderHandler GetHandler(IngresConnection conn, IngresTransaction tran)
        {
            IngresRoleProviderHandler handler = new IngresRoleProviderHandler(conn, tran, this.config, this);

            return(handler);
        }
Ejemplo n.º 8
0
        public static async Task <object> ExecuteScalarAsync(this IngresConnection conn, string sql, IngresTransaction tx = null)
        {
            var cmd = tx == null ? new IngresCommand(sql, conn) : new IngresCommand(sql, conn, tx);

            using (cmd)
                return(await cmd.ExecuteScalarAsync());
        }
        /// <summary>
        /// Creates a role in the database.
        /// </summary>
        /// <param name="roleName">The rolename to create.</param>
        /// <param name="roleid">The role id.</param>
        /// <param name="conn">The Ingres connection to use.</param>
        /// <param name="tran">The Ingres transaction to use.</param>
        private void CreateRole(string roleName, out string roleid, IngresConnection conn, IngresTransaction tran)
        {
            // Validate the roleName
            ValidationUtil.CheckParameterIsOK(ref roleName, true, true, true, 256, "roleName");

            // Ensure that the proposed roleName does not already exist
            if (this.RoleExists(roleName))
            {
                throw new ProviderException(string.Format(Messages.RoleAlreadyExists, roleName));
            }

            string sql = @"
                            INSERT INTO aspnet_Roles
                               (ApplicationId,
                                RoleId,
                                RoleName,
                                LoweredRoleName,
                                Description)  
                            VALUES
                               (?,
                                ?,
                                ?,
                                ?,
                                NULL)
                         ";

            // Create the command with the current connection and enrol in the transaction
            IngresCommand cmd = new IngresCommand(sql, this.conn);

            cmd.Transaction    = this.tran;
            cmd.CommandTimeout = this.config.CommandTimeout;

            // Generate a new role Id - this will be the sent out
            roleid = Guid.NewGuid().ToString().ToLower();

            // Add the required parameters
            cmd.Parameters.Add("ApplicationId", DbType.String).Value   = this.ApplicationId;
            cmd.Parameters.Add("RoleId", DbType.String).Value          = roleid;
            cmd.Parameters.Add("RoleName", DbType.String).Value        = roleName;
            cmd.Parameters.Add("LoweredRoleName", DbType.String).Value = roleName.ToLower();

            // Execute the query
            int rows = cmd.ExecuteNonQuery();

            // Validate that the query affected the correct numbber of rows.
            if (rows != 1)
            {
                throw new ProviderException(string.Format(Messages.UnknownError));
            }
        }