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.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace Lab_6___ESoputan
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private string fileopen; // <------------
- private SqlConnection conn = new SqlConnection();
- private void Test()
- {
- OpenFileDialog database = new OpenFileDialog();
- database.ShowDialog();
- fileopen = database.FileName; //save into the string class up
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Test();
- }
- private void btnGetAll_Click(object sender, EventArgs e)
- {
- lstBox.Items.Clear(); //clear out listbox
- int counter = 1;
- SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
- builder.DataSource = @"(localdb)\v11.0";
- builder.AttachDBFilename = fileopen;
- builder.IntegratedSecurity = true;
- conn.ConnectionString = builder.ConnectionString;
- SqlCommand cmd = new SqlCommand();
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = "SELECT * FROM Suppliers";
- cmd.Connection = conn;
- conn.Open(); //open up connection
- SqlDataReader reader = cmd.ExecuteReader();
- try
- {
- lstBox.Items.Add(string.Format("Suppliers Contact Name Listings:"));
- lstBox.Items.Add(string.Format("- - - - - - - - - - - - - - - - - - - - - -"));
- while (reader.Read())
- {
- string name, count;
- name = reader["ContactName"].ToString();
- count = "Row #" + counter.ToString();
- lstBox.Items.Add(name.PadRight(40 - name.Length) + "\t\t" + count); //horizontal tab
- counter++;
- }
- }
- finally
- {
- reader.Close();
- conn.Close();
- }
- }
- private void btnGetCountry_Click(object sender, EventArgs e)
- {
- try
- {
- lstBox.Items.Clear(); //clear out listbox
- int counter = 1;
- SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
- builder.DataSource = @"(localdb)\v11.0";
- builder.AttachDBFilename = fileopen;
- builder.IntegratedSecurity = true;
- conn.ConnectionString = builder.ConnectionString;
- SqlCommand cmd = new SqlCommand();
- cmd.CommandType = CommandType.Text;
- cmd.Parameters.AddWithValue("@Param", this.txtCountry.Text);
- cmd.CommandText = "SELECT * FROM Suppliers WHERE Country = @Param";
- cmd.Connection = conn;
- conn.Open(); //open up connection
- SqlDataReader reader = cmd.ExecuteReader();
- lstBox.Items.Add(string.Format("Country= " + this.txtCountry.Text));
- lstBox.Items.Add(string.Format("- - - - - - - - - - - - - - - - - - - - - -"));
- while (reader.Read())
- {
- string name, count;
- name = reader["ContactName"].ToString();
- count = "Row #" + counter.ToString();
- lstBox.Items.Add(name.PadRight(40 - name.Length) + "\t\t" + count); //horizontal tab
- counter++;
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message, "Error");
- }
- finally
- {
- conn.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement