vakho

Ado.NET Adapter

Oct 18th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 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.Windows.Forms;
  9.  
  10. using System.Data.SqlClient;
  11.  
  12. namespace WFA
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         SqlConnection con = new SqlConnection();
  17.         SqlDataAdapter adapter;
  18.         DataSet ds;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void Form1_Load(object sender, EventArgs e)
  26.         {
  27.             con.ConnectionString = "Server=LAB-418-08-PC\\STUDENT; database=TestBase; Integrated Security=true;";
  28.             try
  29.             {
  30.                 con.Open();
  31.                 SqlCommand command = new SqlCommand("SELECT * FROM Users");
  32.                 adapter = new SqlDataAdapter("SELECT * FROM Users", con);
  33.                 SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
  34.                 ds = new DataSet();
  35.                 adapter.Fill(ds, "c");
  36.                 dataGridView1.DataSource = ds.Tables["c"];
  37.             }
  38.             catch (Exception ex)
  39.             {
  40.                 MessageBox.Show(ex.ToString(), "Exception message");
  41.             }
  42.             finally
  43.             {
  44.                 con.Close();
  45.             }
  46.         }
  47.  
  48.         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  49.         {
  50.             dataGridView1.BeginEdit(true);
  51.         }
  52.  
  53.         private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
  54.         {
  55.             dataGridView1.EndEdit();
  56.             adapter.Update(ds, "c");
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment