Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Problema_1_2
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             string server = textBox1.Text;
  24.             string database = "p2g2";
  25.             string user = textBox2.Text;
  26.             string password = textBox3.Text;
  27.  
  28.             using (SqlConnection conn = new SqlConnection())
  29.             {
  30.                 conn.ConnectionString = string.Format("Server={0};Database={1};User Id={2};Password={3};", server, database, user, password);
  31.  
  32.                 try
  33.                 {  
  34.                     conn.Open();
  35.                     MessageBox.Show("Database connection successful!");
  36.                 }
  37.                 catch (SqlException ex)
  38.                 {
  39.                     Console.WriteLine(ex.ToString());
  40.                     MessageBox.Show("Database connection NOT successful!");
  41.                 }
  42.                 finally
  43.                 {
  44.                     conn.Close();
  45.                 }
  46.             }
  47.         }
  48.  
  49.         private void button2_Click(object sender, EventArgs e)
  50.         {
  51.             string server = textBox1.Text;
  52.             string database = "p2g2";
  53.             string user = textBox2.Text;
  54.             string password = textBox3.Text;
  55.  
  56.             using (SqlConnection conn = new SqlConnection())
  57.             {
  58.                 conn.ConnectionString = string.Format("Server={0};Database={1};User Id={2};Password={3};", server, database, user, password);
  59.  
  60.                 try
  61.                 {
  62.                     conn.Open();
  63.                     MessageBox.Show("Database connection successful!");
  64.  
  65.                     using (SqlCommand command = new SqlCommand("SELECT * FROM Hello", conn))
  66.  
  67.                     using (SqlDataReader reader = command.ExecuteReader())
  68.                     {
  69.                         string msg = "";
  70.  
  71.                         while (reader.Read())
  72.                         {
  73.                             msg += string.Format("{0}: {1}", reader.GetInt32(0), reader.GetString(1));
  74.                         }
  75.  
  76.                         MessageBox.Show(msg);
  77.                     }
  78.                 }
  79.                 catch (SqlException ex)
  80.                 {
  81.                     Console.WriteLine(ex.ToString());
  82.                     MessageBox.Show("Database connection NOT successful!");
  83.                 }
  84.                 finally
  85.                 {
  86.                     conn.Close();
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement