Advertisement
hexalogy

Lab 6

Apr 16th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11.  
  12. namespace Lab_6___ESoputan
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private string fileopen; // <------------
  22.  
  23.         private SqlConnection conn = new SqlConnection();
  24.  
  25.         private void Test()
  26.         {
  27.             OpenFileDialog database = new OpenFileDialog();
  28.             database.ShowDialog();
  29.  
  30.             fileopen = database.FileName; //save into the string class up
  31.         }
  32.  
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             Test();
  36.         }
  37.  
  38.         private void btnGetAll_Click(object sender, EventArgs e)
  39.         {
  40.             lstBox.Items.Clear(); //clear out listbox
  41.             int counter = 1;
  42.  
  43.             SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  44.             builder.DataSource = @"(localdb)\v11.0";
  45.             builder.AttachDBFilename = fileopen;
  46.             builder.IntegratedSecurity = true;
  47.  
  48.             conn.ConnectionString = builder.ConnectionString;
  49.  
  50.             SqlCommand cmd = new SqlCommand();
  51.             cmd.CommandType = CommandType.Text;
  52.             cmd.CommandText = "SELECT * FROM Suppliers";
  53.             cmd.Connection = conn;
  54.             conn.Open(); //open up connection
  55.  
  56.             SqlDataReader reader = cmd.ExecuteReader();
  57.  
  58.             try
  59.             {
  60.                 lstBox.Items.Add(string.Format("Suppliers Contact Name Listings:"));
  61.                 lstBox.Items.Add(string.Format("- - - - - - - - - - - - - - - - - - - - - -"));
  62.  
  63.                 while (reader.Read())
  64.                 {
  65.                     string name, count;
  66.                     name = reader["ContactName"].ToString();
  67.                     count = "Row #" + counter.ToString();
  68.                     lstBox.Items.Add(name.PadRight(40 - name.Length) + "\t\t" + count); //horizontal tab
  69.                     counter++;
  70.                 }
  71.             }
  72.             finally
  73.             {
  74.                 reader.Close();
  75.                 conn.Close();
  76.             }
  77.  
  78.         }
  79.  
  80.         private void btnGetCountry_Click(object sender, EventArgs e)
  81.         {
  82.             try
  83.             {
  84.                 lstBox.Items.Clear(); //clear out listbox
  85.                 int counter = 1;
  86.  
  87.                 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  88.                 builder.DataSource = @"(localdb)\v11.0";
  89.                 builder.AttachDBFilename = fileopen;
  90.                 builder.IntegratedSecurity = true;
  91.  
  92.                 conn.ConnectionString = builder.ConnectionString;
  93.  
  94.                 SqlCommand cmd = new SqlCommand();
  95.                 cmd.CommandType = CommandType.Text;
  96.                 cmd.Parameters.AddWithValue("@Param", this.txtCountry.Text);
  97.                 cmd.CommandText = "SELECT * FROM Suppliers WHERE Country = @Param";
  98.                 cmd.Connection = conn;
  99.                 conn.Open(); //open up connection
  100.  
  101.                 SqlDataReader reader = cmd.ExecuteReader();
  102.  
  103.                 lstBox.Items.Add(string.Format("Country= " + this.txtCountry.Text));
  104.                 lstBox.Items.Add(string.Format("- - - - - - - - - - - - - - - - - - - - - -"));
  105.  
  106.                 while (reader.Read())
  107.                 {
  108.                     string name, count;
  109.                     name = reader["ContactName"].ToString();
  110.                     count = "Row #" + counter.ToString();
  111.                     lstBox.Items.Add(name.PadRight(40 - name.Length) + "\t\t" + count); //horizontal tab
  112.                     counter++;
  113.  
  114.                 }
  115.             }
  116.             catch(Exception ex)
  117.             {
  118.                 MessageBox.Show(ex.Message, "Error");
  119.             }
  120.             finally
  121.             {
  122.                 conn.Close();
  123.             }
  124.  
  125.         }
  126.  
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement