ð Microsoft Visual Studio 2019 : C# and PostgreSQL Tutorial.
ðĻðŦ How to Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
✅ https://youtu.be/SnpW2hAuTmk
ðĻðŦ How to connect PostgreSQL database using C#.
#END
Tags: visual studio 2019, visual basic 2019, vb.net, vb.net tutorial for
beginners visual studio 2019, visual basic, vb, visual studio 2019 visual
basic tutorial, visual studio 2019 tutorial, how to use visual studio 2019,
visual basic tutorial 2019, access, visual studio
#VisualCSharp #CSharp #PostgreSQL #CRUDOperation
In computer programming, create, read, update, and delete (CRUD) are the four basic functions of persistent storage. Alternate words are sometimes used when defining the four basic functions of CRUD, such as retrieve instead of read, modify instead of update, or destroy instead of delete.Read more: Wikipedia
Screenshot
YouTube: https://youtu.be/SnpW2hAuTmk
[FREE SOURCE CODE by iBASSKUNG]
ðŊ Souce Code: http://bit.ly/2QJNHtq
#BEGIN
Class: CRUD.cs
using System;
using System.Data;
using System.Windows.Forms;
using Npgsql;
namespace CSPostgreSQL_SMART_CRUD_V1
{
class CRUD
{
private static string getConnectionString()
{
string host = "Host=localhost;";
string port = "Port=5432;";
string db = "Database=cs_smart_crud;";
string user = "Username=postgres;";
string pass = "Password=1234;";
string conString = string.Format("{0}{1}{2}{3}{4}", host,port, db,user, pass);
return conString;
}
public static NpgsqlConnection con = new NpgsqlConnection(getConnectionString());
public static NpgsqlCommand cmd = default(NpgsqlCommand);
public static string sql = string.Empty;
public static DataTable PerformCRUD(NpgsqlCommand com)
{
NpgsqlDataAdapter da = default(NpgsqlDataAdapter);
DataTable dt = new DataTable();
try
{
da = new NpgsqlDataAdapter();
da.SelectCommand = com;
da.Fill(dt);
return dt;
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message, "Perform CRUD Operations Failed : iBasskung Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Error);
dt = null;
}
return dt;
}
}
}
using System;
using System.Data;
using System.Windows.Forms;
using Npgsql;
namespace CSPostgreSQL_SMART_CRUD_V1
{
class CRUD
{
private static string getConnectionString()
{
string host = "Host=localhost;";
string port = "Port=5432;";
string db = "Database=cs_smart_crud;";
string user = "Username=postgres;";
string pass = "Password=1234;";
string conString = string.Format("{0}{1}{2}{3}{4}", host,port, db,user, pass);
return conString;
}
public static NpgsqlConnection con = new NpgsqlConnection(getConnectionString());
public static NpgsqlCommand cmd = default(NpgsqlCommand);
public static string sql = string.Empty;
public static DataTable PerformCRUD(NpgsqlCommand com)
{
NpgsqlDataAdapter da = default(NpgsqlDataAdapter);
DataTable dt = new DataTable();
try
{
da = new NpgsqlDataAdapter();
da.SelectCommand = com;
da.Fill(dt);
return dt;
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message, "Perform CRUD Operations Failed : iBasskung Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Error);
dt = null;
}
return dt;
}
}
}
Form: Form1.cs
using System;
using System.Data;
using System.Windows.Forms;
using Npgsql;
namespace CSPostgreSQL_SMART_CRUD_V1
{
public partial class Form1 : Form
{
private string id = "";
private int intRow = 0;
public Form1()
{
InitializeComponent();
resetMe();
}
private void resetMe()
{
this.id = string.Empty;
firstNameTextBox.Text = "";
lastNameTextBox.Text = "";
if (genderComboBox.Items.Count
> 0)
{
genderComboBox.SelectedIndex = 0;
}
updateButton.Text = "Update ()";
deleteButton.Text = "Delete ()";
keywordTextBox.Clear();
if (keywordTextBox.CanSelect)
{
keywordTextBox.Select();
}
}
private void Form1_Load(object sender,
EventArgs e)
{
loadData("");
}
private void loadData(string keyword)
{
CRUD.sql = "SELECT autoid,
firstname, lastname, CONCAT(firstname, ' ', lastname) AS fullname, gender
FROM tb_smart_crud " +
"WHERE CONCAT(CAST(autoid as varchar), ' ', firstname, ' ',
lastname) LIKE @keyword::varchar " +
"OR TRIM(gender) LIKE @keyword::varchar ORDER BY autoid ASC";
string strKeyword =
string.Format("%{0}%", keyword);
CRUD.cmd = new
NpgsqlCommand(CRUD.sql, CRUD.con);
CRUD.cmd.Parameters.Clear();
CRUD.cmd.Parameters.AddWithValue("keyword", strKeyword);
DataTable dt =
CRUD.PerformCRUD(CRUD.cmd);
if (dt.Rows.Count > 0)
{
intRow =
Convert.ToInt32(dt.Rows.Count.ToString());
}
else
{
intRow = 0;
}
toolStripStatusLabel1.Text =
"Number of row(s): " + intRow.ToString();
DataGridView dgv1 =
dataGridView1;
dgv1.MultiSelect = false;
dgv1.AutoGenerateColumns = true;
dgv1.SelectionMode =
DataGridViewSelectionMode.FullRowSelect;
dgv1.DataSource = dt;
dgv1.Columns[0].HeaderText =
"ID";
dgv1.Columns[1].HeaderText =
"First Name";
dgv1.Columns[2].HeaderText =
"Last Name";
dgv1.Columns[3].HeaderText =
"Full Name";
dgv1.Columns[4].HeaderText =
"Gender";
dgv1.Columns[0].Width = 85;
dgv1.Columns[1].Width = 170;
dgv1.Columns[2].Width = 170;
dgv1.Columns[3].Width = 220;
dgv1.Columns[4].Width = 100;
}
private void execute(string mySQL, string
param)
{
CRUD.cmd = new
NpgsqlCommand(mySQL, CRUD.con);
addParameters(param);
CRUD.PerformCRUD(CRUD.cmd);
}
private void addParameters(string str)
{
CRUD.cmd.Parameters.Clear();
CRUD.cmd.Parameters.AddWithValue("firstName",
firstNameTextBox.Text.Trim());
CRUD.cmd.Parameters.AddWithValue("lastName", lastNameTextBox.Text.Trim());
CRUD.cmd.Parameters.AddWithValue("gender",
genderComboBox.SelectedItem.ToString());
if(str == "Update" || str ==
"Delete" && !string.IsNullOrEmpty(this.id))
{
CRUD.cmd.Parameters.AddWithValue("id", this.id);
}
}
private void insertButton_Click(object sender,
EventArgs e)
{
if
(string.IsNullOrEmpty(firstNameTextBox.Text.Trim()) ||
string.IsNullOrEmpty(lastNameTextBox.Text.Trim()))
{
MessageBox.Show("Please input first name and last name.", "Insert
Data : iBasskung Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
CRUD.sql = "INSERT INTO
tb_smart_crud(firstname, lastname, gender) VALUES(@firstName, @lastName,
@gender)";
execute(CRUD.sql, "Insert");
MessageBox.Show("The record has
been saved.", "Insert Data : iBasskung Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Information);
loadData("");
resetMe();
}
private void dataGridView1_CellClick(object
sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
DataGridView dgv1
= dataGridView1;
this.id =
Convert.ToString(dgv1.CurrentRow.Cells[0].Value);
updateButton.Text
= "Update (" + this.id + ")";
deleteButton.Text
= "Delete (" + this.id + ")";
firstNameTextBox.Text = Convert.ToString(dgv1.CurrentRow.Cells[1].Value);
lastNameTextBox.Text = Convert.ToString(dgv1.CurrentRow.Cells[2].Value);
genderComboBox.SelectedItem =
Convert.ToString(dgv1.CurrentRow.Cells[4].Value);
}
}
private void updateButton_Click(object sender,
EventArgs e)
{
if (dataGridView1.Rows.Count ==
0)
{
return;
}
if
(string.IsNullOrEmpty(this.id))
{
MessageBox.Show("Please select an item from the list.", "Update Data :
iBasskung Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if
(string.IsNullOrEmpty(firstNameTextBox.Text.Trim()) ||
string.IsNullOrEmpty(lastNameTextBox.Text.Trim()))
{
MessageBox.Show("Please input first name and last name.", "Insert
Data : iBasskung Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
CRUD.sql = "UPDATE tb_smart_crud
SET firstName = @firstName, lastname = @lastName, gender = @gender WHERE
autoid = @id::integer";
execute(CRUD.sql, "Update");
MessageBox.Show("The record has
been updated.", "Update Data : iBasskung Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Information);
loadData("");
resetMe();
}
private void deleteButton_Click(object sender,
EventArgs e)
{
if (dataGridView1.Rows.Count ==
0)
{
return;
}
if
(string.IsNullOrEmpty(this.id))
{
MessageBox.Show("Please select an item from the list.", "Delete Data :
iBasskung Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (MessageBox.Show("Do you want
to permanently delete the selected record?", "Delete Data : iBasskung
Tutorial",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) ==
DialogResult.Yes)
{
CRUD.sql = "DELETE
FROM tb_smart_crud WHERE autoid = @id::integer";
execute(CRUD.sql,
"Update");
MessageBox.Show("The record has been deleted.", "Delete Data : iBasskung
Tutorial",
MessageBoxButtons.OK, MessageBoxIcon.Information);
loadData("");
resetMe();
}
}
private void searchButton_Click(object sender,
EventArgs e)
{
// Let's try :)
if
(string.IsNullOrEmpty(keywordTextBox.Text.Trim()))
{
loadData("");
}
else
{
loadData(keywordTextBox.Text.Trim());
}
resetMe();
}
}
}
#END
ðŊ See more:
ðē YouTube: https://www.youtube.com/c/iBasskung
ðē Facebook: https://www.facebook.com/CodeAMinute
ðē Udemy: https://www.udemy.com/user/tanin-sangngam
ðē SkillLane: https://www.skilllane.com/instructors/iBasskung
ðŊ THANK YOU SO MUCH ðŊ
#VisualCSharp #CSharp #PostgreSQL #CRUDOperation
Comments
Post a Comment