You are on page 1of 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace conMysql
{
public partial class Form1 : Form
{
string connStr;
MySqlConnection conn;
public Form1()
{
InitializeComponent();

}
private void conecta()
{
if (conn != null)
conn.Close();

connStr = "Server=192.168.30.2; Port=3306; Database=net; Uid=faliaga


; Pwd=faliaga";
try
{
conn = new MySqlConnection(connStr);
conn.Open();

}
catch (MySqlException ex)
{
MessageBox.Show("Error connecting to the server: " + ex.Message)
;
}
}
private void grabar()
{
try
{
MySqlCommand cmd = new MySqlCommand("INSERT INTO ejemplo VALUES
(?param1, ?param2)", conn);
cmd.Parameters.Add("?param1", this.textBox1.Text);
cmd.Parameters.Add("?param2", this.textBox2.Text);
cmd.Prepare();
cmd.ExecuteNonQuery();
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
conecta();
}
private void button1_Click(object sender, EventArgs e)
{
grabar();
}
}
}

You might also like