Ver Mensaje Individual
  #6 (permalink)  
Antiguo 11/11/2011, 09:00
gus_anomaly
 
Fecha de Ingreso: noviembre-2009
Mensajes: 381
Antigüedad: 15 años, 4 meses
Puntos: 6
Respuesta: INSERT con C# y MySQL

Estoy probando cosas porque es para una capacitación, aqui el code:

Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class Formulario : Form
    {

        public Formulario()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Server=localhost;Database=gustavo_test;User ID=root;Password=root;Pooling=false;";
            MySqlConnection conn = new MySqlConnection(connectionString);

            this.listBox1.Items.Add("Titulo:" + this.textBox1.Text);
            this.listBox1.Items.Add("Contenido:" + this.textBox2.Text);
            this.textBox1.Text = string.Empty;
            this.textBox2.Text = string.Empty;

            string valor1 = this.textBox1.Text;
            string valor2 = this.textBox2.Text;

            string query = "INSERT INTO post (id, titulo, contenido, id_categoria) VALUES (null, 'valor1', 'valor2', 1)";
            conn.Open();
            MySqlCommand myCommand = new MySqlCommand(query, conn);
            
            myCommand.Dispose();
            conn.Close();

            textBox1.Focus();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void borrar_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            textBox1.Focus();
        }

        private void BotonSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string connectionString = "Server=localhost;Database=gustavo_test;User ID=root;Password=root;Pooling=false;";
            MySqlConnection conn = new MySqlConnection(connectionString);

            string query = "SELECT titulo, contenido FROM post";

            conn.Open();

            MySqlCommand myCommand = new MySqlCommand(query, conn);
            MySqlDataReader myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                this.listBox1.Items.Add("Nombre:" + myReader["titulo"].ToString());
                this.listBox1.Items.Add("Apellido:" + myReader["contenido"].ToString());
            }

            myReader.Close();
            myCommand.Dispose();
            conn.Close();
            textBox1.Focus();
        }
    }
}
El Main:

Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// Punto de entrada principal para la aplicación.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Formulario());

            string connectionString = "Server=localhost;Database=gustavo_test;User ID=root;Password=root;Pooling=false;";
            MySqlConnection conn = new MySqlConnection(connectionString);
        }
    }
}
Muchas gracias!
Gustavo.