Introduction

In this article, I am performing simple operations like save, delete, update, and search operations in a Windows Forms application. At first, we should have a Database. To create a database. In this example my database name is "STUDENT" and database table is "student_detail" which has four columns as "roll_no", "s_name", "age" and "course".
Create a Windows Forms Application. Take some UI controls.
save record in ado.net
Now we write code to perform the operations described in this article.
Code for Saving Record
  1. conn = new SqlConnection(connstring);
  2. conn.Open();
  3. comm = new SqlCommand("insert into student_detail values(" + txtrn.Text + ",'" + txtname.Text + "'," + txtage.Text + ",'" + txtcourse.Text + "')", conn);
  4. try
  5. {
  6. comm.ExecuteNonQuery();
  7. MessageBox.Show("Saved...");
  8. }
  9. catch (Exception)
  10. {
  11. MessageBox.Show("Not Saved");
  12. }
  13. finally
  14. {
  15. conn.Close();
  16. }
Look at the above code. In the first line of code, an instance of a SqlConnection is created. In the next, an instance of a SqlCommand class is created and a SQL statement for inserting values into the database table is specified. Then I am calling the ExecuteNonQuery() method in a try block. In the finally block I am closing the SqlConnection by the Close() method. The same as writing SQL Statements for performing various operations. Look at the following code for performing all the operations.
  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. using System.Data.SqlClient;
  10. namespace savedeleteupdateapp
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. SqlConnection conn;
  19. SqlCommand comm;
  20. SqlDataReader dreader;
  21. string connstring = "server=localhost;database=student;user=sa;password=wintellect";
  22. private void btnsave_Click(object sender, EventArgs e)
  23. {
  24. conn = new SqlConnection(connstring);
  25. conn.Open();
  26. comm = new SqlCommand("insert into student_detail values(" + txtrn.Text + ",'" + txtname.Text + "'," + txtage.Text + ",'" + txtcourse.Text + "')", conn);
  27. try
  28. {
  29. comm.ExecuteNonQuery();
  30. MessageBox.Show("Saved...");
  31. }
  32. catch (Exception)
  33. {
  34. MessageBox.Show("Not Saved");
  35. }
  36. finally
  37. {
  38. conn.Close();
  39. }
  40. }
  41. private void btnclear_Click(object sender, EventArgs e)
  42. {
  43. txtage.Clear();
  44. txtcourse.Clear();
  45. txtname.Clear();
  46. txtrn.Clear();
  47. txtrn.Focus();
  48. }
  49. private void btndelete_Click(object sender, EventArgs e)
  50. {
  51. conn = new SqlConnection(connstring);
  52. conn.Open();
  53. comm = new SqlCommand("delete from student_detail where roll_no = " + txtrn.Text + " ", conn);
  54. try
  55. {
  56. comm.ExecuteNonQuery();
  57. MessageBox.Show("Deleted...");
  58. txtage.Clear();
  59. txtcourse.Clear();
  60. txtname.Clear();
  61. txtrn.Clear();
  62. txtrn.Focus();
  63. }
  64. catch (Exception x)
  65. {
  66. MessageBox.Show(" Not Deleted" + x.Message );
  67. }
  68. finally
  69. {
  70. conn.Close();
  71. }
  72. }
  73. private void btnsearch_Click(object sender, EventArgs e)
  74. {
  75. conn = new SqlConnection(connstring);
  76. conn.Open();
  77. comm = new SqlCommand("select * from student_detail where roll_no = " + txtrn.Text + " ", conn);
  78. try
  79. {
  80. dreader = comm.ExecuteReader();
  81. if (dreader.Read())
  82. {
  83. txtname.Text = dreader[1].ToString();
  84. txtage.Text = dreader[2].ToString();
  85. txtcourse.Text = dreader[3].ToString();
  86. }
  87. else
  88. {
  89. MessageBox.Show(" No Record");
  90. }
  91. dreader.Close();
  92. }
  93. catch (Exception)
  94. {
  95. MessageBox.Show(" No Record");
  96. }
  97. finally
  98. {
  99. conn.Close();
  100. }
  101. }
  102. private void btnupdate_Click(object sender, EventArgs e)
  103. {
  104. conn = new SqlConnection(connstring);
  105. conn.Open();
  106. comm = new SqlCommand("update student_detail set s_name= '"+txtname.Text+"', age= "+txtage.Text+" , course=' "+txtcourse.Text+"' where roll_no =
  107. "+txtrn.Text+" ", conn);
  108. try
  109. {
  110. comm.ExecuteNonQuery();
  111. MessageBox.Show("Updated..");
  112. }
  113. catch (Exception)
  114. {
  115. MessageBox.Show(" Not Updated");
  116. }
  117. finally
  118. {
  119. conn.Close();
  120. }
  121. }
  122. private void Form1_Load(object sender, EventArgs e)
  123. {
  124. txtrn.Focus();
  125. }
  126. }
  127. }
Now run the application. You can Save, Search, Delete and Update records.

Summary

In this article, you learned how to save, delete, search, and update records in ADO.NET. I hope it will be helpful for beginners.
Here are some related resource