C sharp - CONECTAR MYSQL Y REALIZAR PRIMERAS SENTENCIAS DE MYSQL

 
Vista:
sin imagen de perfil

CONECTAR MYSQL Y REALIZAR PRIMERAS SENTENCIAS DE MYSQL

Publicado por OHC (5 intervenciones) el 07/10/2015 17:21:15
Buenos días a todos , su apoyo para saber como abrir la conexión de c# y mysql para empezar a realizar consulta de antemano se los agradeceria si me apoyan con este tema.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 MySql.Data.MySqlClient;
 
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public string cnx;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void iniciarconexion()
        {
            try
            {
                MySqlConnection conexion = new MySqlConnection();
                conexion.ConnectionString = "Server=xx.xx.xx.xx;Database=xxxxxx; Uid=xxxxxx;Pwd=xxxxxx;";
                conexion.Open();
                MessageBox.Show("Conexion con exito");
            }
 
            catch (MySqlException)
            {
                MessageBox.Show("Ocurrio un error al intentar conectarse");
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
 
            //COMO PUEDO MANDAR A LLARMAR EL METODO iniciarconexion()  PARA ABRIR LA CONEXION Y EMPESAR A REALIZAR CONSULTAS 
            CON MYSQL
            //????????
 
            //Realizamos una selecion de la tabla usuarios.
            string sql = "SELECT * FROM catclientes";
            MySqlCommand cmd = new MySqlCommand(sql,  COMO ABRIRI LA CONEXION );
 
 
        }
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

CONECTAR MYSQL Y REALIZAR PRIMERAS SENTENCIAS DE MYSQL

Publicado por Fran (2 intervenciones) el 11/10/2015 21:57:00
Espero que te sirva.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private MySqlConnection iniciarconexion()
        {
            MySqlConnection conexion = new MySqlConnection();
            conexion.ConnectionString = "Server=xxx.xx.xx.xx;Database=xxxxxx; Uid=xxxx;Pwd=xxxx;";
            try
            {
                conexion.Open();
            }
 
            catch (MySqlException)
            {
                MessageBox.Show("Ocurrio un error al intentar conectarse");
            }
            return conexion;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
        }
 
        private void button1_Click_1(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM catclientes";
            MySqlCommand cmd = new MySqlCommand(sql, iniciarconexion());
            iniciarconexion().Close();
            MessageBox.Show("Conexión realizada correctamente");
        }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

CONECTAR MYSQL Y REALIZAR PRIMERAS SENTENCIAS DE MYSQL

Publicado por OHC (5 intervenciones) el 12/10/2015 20:15:07
Buenas tardes. La verdad te lo agradezco mucho Funciono. Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

CONECTAR MYSQL Y REALIZAR PRIMERAS SENTENCIAS DE MYSQL

Publicado por Javier Lopez Vargas (21 intervenciones) el 20/05/2016 10:14:40
Espero te Sirva
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
MySqlConnection conn = new MySqlConnection(" server = localhost; Uid = root; password = 1234; database = ejemplomariadb ");
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                conn.Open();
                MySqlCommand cn = new MySqlCommand("INSERT INTO empleado(id, strNombre, strAPaterno, idComCatSexo) VALUES('" + txtid.Text +
                                                                                                                          "','" + txtNombre.Text +
                                                                                                                          "','" + txtAPaterno.Text +
                                                                                                                          "','" + txtidSexo.Text + "')", conn);
                cn.ExecuteNonQuery();
                Response.Write("GUARDADO CON EXITO");
                conn.Close();
            }
            catch(Exception _e)
            {
                Response.Write("NO SE PUDO GUARDAR");
            }
        }
 
        protected void btnConsi_Click(object sender, EventArgs e)
        {
            try
            {
                MySqlDataAdapter select = new MySqlDataAdapter("SELECT * FROM empleado",conn);
                conn.Open();
                DataTable datos = new DataTable();
                select.Fill(datos);
                dgvDatosEmpleado.DataSource = datos;
                dgvDatosEmpleado.DataBind();
                conn.Close();
            }
            catch (Exception _e)
            {
                Response.Write("NO SE PUDO CONSULTAR");
            }
        }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar