Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Problema_1_2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string server = textBox1.Text;
- string database = "p2g2";
- string user = textBox2.Text;
- string password = textBox3.Text;
- using (SqlConnection conn = new SqlConnection())
- {
- conn.ConnectionString = string.Format("Server={0};Database={1};User Id={2};Password={3};", server, database, user, password);
- try
- {
- conn.Open();
- MessageBox.Show("Database connection successful!");
- }
- catch (SqlException ex)
- {
- Console.WriteLine(ex.ToString());
- MessageBox.Show("Database connection NOT successful!");
- }
- finally
- {
- conn.Close();
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- string server = textBox1.Text;
- string database = "p2g2";
- string user = textBox2.Text;
- string password = textBox3.Text;
- using (SqlConnection conn = new SqlConnection())
- {
- conn.ConnectionString = string.Format("Server={0};Database={1};User Id={2};Password={3};", server, database, user, password);
- try
- {
- conn.Open();
- MessageBox.Show("Database connection successful!");
- using (SqlCommand command = new SqlCommand("SELECT * FROM Hello", conn))
- using (SqlDataReader reader = command.ExecuteReader())
- {
- string msg = "";
- while (reader.Read())
- {
- msg += string.Format("{0}: {1}", reader.GetInt32(0), reader.GetString(1));
- }
- MessageBox.Show(msg);
- }
- }
- catch (SqlException ex)
- {
- Console.WriteLine(ex.ToString());
- MessageBox.Show("Database connection NOT successful!");
- }
- finally
- {
- conn.Close();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement