viernes, 5 de diciembre de 2014

Códigos para Visual Studio part 7

En esta entrada Guardaremos un registro en la base de datos desde Visual Studio.
Para esta entrada no es necesario ninguna agregar alguna herramienta en el windows Form .

Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Grabar_sin_confirmacion
{
    public partial class Form1 : Form
    {
        public static void insertelement(string Codigo, string Nombre_alumno, string Ciudad, string Apellidos)
        {
            OleDbConnection miconex = new OleDbConnection (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ @AppDomain.CurrentDomain.BaseDirectory + @"/alumnos.accdb;Persist Security Info = False");
         
            OleDbCommand comando = new OleDbCommand ("Insert into Alumnos(Codigo,Nombre_alumno,Ciudad, Apellidos) values ('" + Codigo + "','" + Nombre_alumno + "','" + Ciudad + "','" + Apellidos + "' )", miconex);
           
            OleDbDataAdapter add = new OleDbDataAdapter ();
            miconex.Open();
            add.InsertCommand = comando;
            add.InsertCommand.ExecuteNonQuery();
            miconex.Close();

        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult r;
            r = MessageBox.Show("¿Quiere abandonar la pagina?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            { Application.Exit(); }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            insertelement(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();


        }
     

    }
}



Al ejecutar:


0 comentarios :

Publicar un comentario