Advertisement
KangJiYoung

C# SQL Database(SELECT,INSERT,DELETE,UPDATE)

Mar 12th, 2016
2,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Windows.Forms;
  5.  
  6. namespace ClassLibrary1
  7. {
  8.     public class SQLFunctions
  9.     {
  10.         private static readonly SqlConnection Connection = new SqlConnection("Insert your Connection string here");
  11.  
  12.         public static void Refresh(DataGridView dataGridView)
  13.         {
  14.             try
  15.             {
  16.                 Connection.Open();
  17.                 SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT * FROM [Table]", Connection);
  18.                 DataTable dataTable = new DataTable();
  19.                 sqlDataAdapter.Fill(dataTable);
  20.                 dataGridView.DataSource = dataTable;
  21.             }
  22.             catch (Exception exception)
  23.             {
  24.                 MessageBox.Show(exception.Message);
  25.             }
  26.             finally
  27.             {
  28.                 Connection.Close();
  29.             }
  30.         }
  31.  
  32.         public static void Insert(string userName)
  33.         {
  34.             try
  35.             {
  36.                 Connection.Open();
  37.                 SqlCommand sqlCommand = new SqlCommand("INSERT INTO [Table] VALUES(@username)", Connection);
  38.                 sqlCommand.Parameters.AddWithValue("@username", userName);
  39.                 sqlCommand.ExecuteNonQuery();
  40.             }
  41.             catch (Exception exception)
  42.             {
  43.                 MessageBox.Show(exception.Message);
  44.             }
  45.             finally
  46.             {
  47.                 Connection.Close();
  48.             }
  49.         }
  50.  
  51.         public static void Delete(string userName)
  52.         {
  53.             try
  54.             {
  55.                 Connection.Open();
  56.                 SqlCommand sqlCommand = new SqlCommand("DELETE FROM [Table] WHERE Username = @username", Connection);
  57.                 sqlCommand.Parameters.AddWithValue("@username", userName);
  58.                 sqlCommand.ExecuteNonQuery();
  59.             }
  60.             catch (Exception exception)
  61.             {
  62.                 MessageBox.Show(exception.Message);
  63.             }
  64.             finally
  65.             {
  66.                 Connection.Close();
  67.             }
  68.         }
  69.  
  70.         public static void Update(string oldUserName, string newUserName)
  71.         {
  72.             try
  73.             {
  74.                 Connection.Open();
  75.                 SqlCommand sqlCommand = new SqlCommand("UPDATE [Table] SET Username = @newUsername WHERE Username = @oldUsername", Connection);
  76.                 sqlCommand.Parameters.AddWithValue("@newUsername", newUserName);
  77.                 sqlCommand.Parameters.AddWithValue("@oldUsername", oldUserName);
  78.                 sqlCommand.ExecuteNonQuery();
  79.             }
  80.             catch (Exception exception)
  81.             {
  82.                 MessageBox.Show(exception.Message);
  83.             }
  84.             finally
  85.             {
  86.                 Connection.Close();
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement