C# Database Commands
Microsoft Visual C# Commands for Database
To Obtain More Details View This Video
For Insert button
String
connectionstring, commandstring;
connectionstring = @".mdb";
commandstring = "INSERT INTO table1
VALUES('"+textBox1.Text+ "','"
+textBox2.Text+"','"+textBox3.Text+
"','" +comboBox1.Text+"','"+textBox4.Text+ "','" +textBox5.Text+"','"+textBox6.Text+ "','" +textBox7.Text+"')";
MessageBox.Show("OK",
"click", MessageBoxButtons.OK,
MessageBoxIcon.Error);
OleDbConnection conn = new
OleDbConnection(connectionstring);
OleDbCommand comm = new
OleDbCommand(commandstring, conn);
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("Click
OK", ".........", MessageBoxButtons.OK);
conn.Close();
For Update button
String connectionstring, commandstring;
connectionstring = @".mdb";
commandstring = "UPDATE table1 SET
name='" + textBox1.Text + "',idno='"
+ textBox2.Text + "',ps='" +
textBox3.Text + "',lt='" +
comboBox1.Text + "',tapiam='" +
textBox4.Text + "',tapbn='" +
textBox5.Text + "',aa='" +
textBox6.Text + "', tldotp='" +
textBox7.Text + "'";
MessageBox.Show("Are
u Sure", "......", MessageBoxButtons.OK, MessageBoxIcon.Error);
OleDbConnection conn = new
OleDbConnection(connectionstring);
OleDbCommand comm = new
OleDbCommand(commandstring, conn);
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("Success",
"......", MessageBoxButtons.OK,
MessageBoxIcon.Error);
conn.Close();
For Delete button
string connectionString, commandString;
connectionString = @".mdb";
commandString = "DELETE*FROM table1
WHERE name='" + textBox1.Text + "'";
if (MessageBox.Show("Are you sure, you want to delete this record?",
"Sure?", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.No)
{
return;
}
OleDbConnection conn = new
OleDbConnection(connectionString);
OleDbCommand comm = new
OleDbCommand(commandString, conn);
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("Record
Deleted Successfully");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Text = "";
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
conn.Close();
For Select button (Search button)
String connectionstring, commandstring;
connectionstring = @".mdb";
commandstring = "SELECT * FROM table1
WHERE name='" + textBox1.Text + "'";
OleDbConnection conn = new
OleDbConnection(connectionstring);
OleDbCommand comm = new
OleDbCommand(commandstring, conn);
OleDbDataReader reader = null;
try
{
conn.Open(); }
catch (Exception
ex)
{
MessageBox.Show(ex.Message); }
reader = comm.ExecuteReader();
while (reader.Read())
{
textBox2.Text = reader[1].ToString();
textBox3.Text = reader[2].ToString();
comboBox1.Text = reader[3].ToString();
textBox4.Text = reader[4].ToString();
textBox5.Text = reader[5].ToString();
textBox6.Text = reader[6].ToString();
textBox7.Text = reader[7].ToString();
}
reader.Close();