Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Windows.Forms;
- namespace ClassLibrary1
- {
- public class SQLFunctions
- {
- private static readonly SqlConnection Connection = new SqlConnection("Insert your Connection string here");
- public static void Refresh(DataGridView dataGridView)
- {
- try
- {
- Connection.Open();
- SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT * FROM [Table]", Connection);
- DataTable dataTable = new DataTable();
- sqlDataAdapter.Fill(dataTable);
- dataGridView.DataSource = dataTable;
- }
- catch (Exception exception)
- {
- MessageBox.Show(exception.Message);
- }
- finally
- {
- Connection.Close();
- }
- }
- public static void Insert(string userName)
- {
- try
- {
- Connection.Open();
- SqlCommand sqlCommand = new SqlCommand("INSERT INTO [Table] VALUES(@username)", Connection);
- sqlCommand.Parameters.AddWithValue("@username", userName);
- sqlCommand.ExecuteNonQuery();
- }
- catch (Exception exception)
- {
- MessageBox.Show(exception.Message);
- }
- finally
- {
- Connection.Close();
- }
- }
- public static void Delete(string userName)
- {
- try
- {
- Connection.Open();
- SqlCommand sqlCommand = new SqlCommand("DELETE FROM [Table] WHERE Username = @username", Connection);
- sqlCommand.Parameters.AddWithValue("@username", userName);
- sqlCommand.ExecuteNonQuery();
- }
- catch (Exception exception)
- {
- MessageBox.Show(exception.Message);
- }
- finally
- {
- Connection.Close();
- }
- }
- public static void Update(string oldUserName, string newUserName)
- {
- try
- {
- Connection.Open();
- SqlCommand sqlCommand = new SqlCommand("UPDATE [Table] SET Username = @newUsername WHERE Username = @oldUsername", Connection);
- sqlCommand.Parameters.AddWithValue("@newUsername", newUserName);
- sqlCommand.Parameters.AddWithValue("@oldUsername", oldUserName);
- sqlCommand.ExecuteNonQuery();
- }
- catch (Exception exception)
- {
- MessageBox.Show(exception.Message);
- }
- finally
- {
- Connection.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement