private void BtEliminar_Click_1(object sender, EventArgs e) { try { DataBaseControl DBControl = new DataBaseControl(DataBaseControl.cPath, "ProjectDataBase.mdf"); string[] para = { "@user" }; string[] ele = { TxtUserName.Text }; if (DBControl.Eliminar("Delete from [Users] where [Username] = @user", para, ele)) { MessageBox.Show("Usuario eliminado con exito"); } else { MessageBox.Show("Error al eliminar usuario"); } DBControl.Close(); } catch (Exception) { MessageBox.Show("Por favor, verificar los datos"); } }
private void TxtUserName_TextChanged(object sender, EventArgs e) { if (TxtUserName.Text.Length > 0) { DataBaseControl DBControl = new DataBaseControl(DataBaseControl.cPath, "ProjectDataBase.mdf"); LVUsuarios.Clear(); string[] _parameters = { "@userN" }; string[] _element = { TxtUserName.Text }; string[] _returned = { "UserName" }; foreach (string[] item in DBControl.Buscar("Select [UserName] from [Users] where [Username] like '%' + @userN + '%'", _parameters, _element, 1)) { foreach (var it in item) { LVUsuarios.Items.Add(new ListViewItem(it)); } } DBControl.Close(); } }
private void BtRegistrar_Click(object sender, EventArgs e) { try { if (TxtPass.Text == TxtUserName.Text) { MessageBox.Show("El usuario y al contraseña no pueden ser iguales"); TxtUserName.Clear(); TxtPass.Clear(); return; } DataBaseControl DBControl = new DataBaseControl(DataBaseControl.cPath, "ProjectDataBase.mdf"); string[] parameters = { "@userName", "@pass", "cat" }; string[] elements = { TxtUserName.Text, TxtPass.Text, CBCat.SelectedItem.ToString() }; if (DBControl.Insertar("insert into Users values(@userName,@pass,@cat)", parameters, elements)) { string[] para = { "@id" }; string[] val = { System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString() }; int activos = int.Parse(DBControl.BuscarElemento("select [registros] from Activos where [ID] = @id", para, val)); para = new string[] { "@id", "@nums" }; string[] nums = { System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(), (++activos).ToString(), }; DBControl.Insertar("update [Activos] set [registros]=@nums where [id]=@id", para, nums); MessageBox.Show("Usuario agregado"); this.Close(); } else { MessageBox.Show("El usuario no pudo ser agregado"); } DBControl.Close(); } catch (Exception) { MessageBox.Show("Por favor, verifique los datos"); } }