vakho

[ADO.NET] DataSet & SqlDataAdapter

Jan 16th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 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 WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private string connectionString = "Server=VAKHO-PC; Database=TestDB; Integrated Security=true;";
  17.         private SqlConnection con;
  18.         private SqlDataAdapter adapter;
  19.         private DataSet dataSet = null;
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void refreshTable()
  27.         {
  28.             this.dataSet = new DataSet("users");
  29.  
  30.             adapter.Fill(dataSet, "users");
  31.             dataGridView1.DataSource = dataSet.Tables[0];
  32.         }
  33.  
  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.             this.con = new SqlConnection(this.connectionString);
  37.             this.adapter = new SqlDataAdapter("SELECT * FROM dbo.Users", con);
  38.  
  39.             this.refreshTable();
  40.  
  41.             try
  42.             {
  43.                 con.Open();
  44.             }
  45.             catch (SqlException ex)
  46.             {
  47.                 MessageBox.Show(ex.Message, "Sql Exception!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  48.             }
  49.             finally
  50.             {
  51.                 MessageBox.Show(con.State.ToString(), "Sql Exception!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  52.                 con.Close();
  53.             }
  54.         }
  55.  
  56.         private void button1_Click(object sender, EventArgs e)
  57.         {
  58.             this.refreshTable();
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment