Using Using Using Using Using Using Using Using Namespace Public Partial Class Public
Using Using Using Using Using Using Using Using Namespace Public Partial Class Public
namespace Prueba { public partial class Form1 : Form { public Form1() { InitializeComponent(); conectar(); } private SqlConnection conectar() { try { SqlConnection cn = new SqlConnection(); cn.ConnectionString = "Data Source=LAB1-PC10;Initial Catalog=bdPrueba;Integrated Security=True"; cn.Open(); return cn; } catch (SqlException ex) { MessageBox.Show(ex.Message); return null; } } private void button1_Click(object sender, EventArgs e) { try { SqlConnection cn = conectar(); SqlCommand cmd = cn.CreateCommand(); cmd.CommandText = "SELECT idProducto, descripcion FROM tproducto WHERE idProducto = 003"; SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { txtProducto.Text = dr.GetString(1); } else { MessageBox.Show("No existe dicho producto"); } dr.Close(); } catch (SqlException ex) { MessageBox.Show(ex.Message); } } } }