0% found this document useful (0 votes)
28 views1 page

Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains code for connecting to a SQL database called "bdPrueba" located on server "LAB1-PC10" using integrated security. It defines a method to connect and return a SqlConnection object. It also contains a button click event handler that queries the database table "tproducto" to find a product description based on id and displays it or a message if not found.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views1 page

Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains code for connecting to a SQL database called "bdPrueba" located on server "LAB1-PC10" using integrated security. It defines a method to connect and return a SqlConnection object. It also contains a button click event handler that queries the database table "tproducto" to find a product description based on id and displays it or a message if not found.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Data.SqlClient; System.Drawing; System.Text; System.Windows.Forms;

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); } } } }

You might also like