HyugaPaolo

ADO1

Mar 1st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. Jag scemo :p
  2.  
  3. //classe
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Data;
  10. using System.Data.OleDb;
  11.  
  12. namespace StudentiVoti
  13. {
  14.     class DAL
  15.     {
  16.         private OleDbConnection _conn;
  17.        
  18.         public DAL()
  19.         {
  20.  
  21.             string cns = GetConnectionString();
  22.             _conn = new OleDbConnection(cns);
  23.             _conn.Open();
  24.         }
  25.  
  26.         private string GetConnectionString()
  27.         {
  28.             string dbDirPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  29.             string cns = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\M_inf3.Quarta\Desktop\StudentiVoti.accdb";
  30.             return cns;
  31.         }
  32.  
  33.         public DataTable StudentiElenco(string CognomeFiltro)
  34.         {
  35.             DataTable tbl = new DataTable();
  36.             string qry = "SELECT * FROM Studenti WHERE Cognome=?";
  37.             OleDbCommand cmd = new OleDbCommand(qry, _conn);
  38.             cmd.Parameters.AddWithValue("?", CognomeFiltro);
  39.             OleDbDataAdapter dad = new OleDbDataAdapter(cmd);
  40.             dad.Fill(tbl);
  41.             _conn.Close();
  42.             return tbl;
  43.         }
  44.  
  45.         public DataTable VotiElenco(string IDStudente)
  46.         {
  47.             DataTable tbl = new DataTable();
  48.             string qry = "SELECT * FROM Voti WHERE IDStudente=?";
  49.             OleDbCommand cmd = new OleDbCommand(qry, _conn);
  50.             cmd.Parameters.AddWithValue("?", IDStudente);
  51.             OleDbDataAdapter dad = new OleDbDataAdapter(cmd);
  52.             dad.Fill(tbl);
  53.             _conn.Close();
  54.             return tbl;
  55.         }
  56.     }
  57. }
  58.  
  59. //classe form
  60.  
  61. using System.Data;
  62. using System.Drawing;
  63. using System.Linq;
  64. using System.Text;
  65. using System.Threading.Tasks;
  66. using System.Windows.Forms;
  67.  
  68. namespace StudentiVoti
  69. {
  70.     public partial class Form1 : Form
  71.     {
  72.         public Form1()
  73.         {
  74.             InitializeComponent();
  75.            
  76.         }
  77.  
  78.         private void button1_Click(object sender, EventArgs e)
  79.         {
  80.             DAL c = new DAL();
  81.             dgb1.DataSource = c.VotiElenco(comboBox1.Text);
  82.         }
  83.  
  84.         private void button2_Click(object sender, EventArgs e)
  85.         {
  86.             DAL c = new DAL();
  87.             dgb2.DataSource = c.StudentiElenco(txtCognome.Text);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment