Introduction

The SqlParameter class is found in the "System.Data.SqlClient" namespace. It is a class of a connected architecture of the .NET framework. It represents parameters. To work with the SqlParameter class we should have a database. In this example, I am using a Database "student" which has a "student_detail" table. "RollNo", "Name" and "City" are column names. I will save and retrieve records using the SqlParameter class. Here is a list of important properties of the SqlParameter class which will be used in this example.
  1. SqlDbType: It is used to set the SQL Server Datatypes for a given parameter.
  2. ParameterName: It is used to specify a parameter name.
  3. Direction: It is used for setting the direction of a SqlParameter. It is Input or Output or both (InputOutput).
  4. Size: It is used to set the maximum size of the value of the parameter.
  5. Value: It is used for assigning or getting the value of the parameter.
Now, take a Windows Forms application in Visual Studio 2010. Take some UI Controls and arrange them as shown in the figure below
Clipboard01.gif
Write the following code for saving a record into the database.
  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 WorkWithSqlParameterClass {
  11. public partial class Form1: Form {
  12. public Form1() {
  13. InitializeComponent();
  14. }
  15. SqlConnection conn;
  16. SqlCommand comm;
  17. string connstring = "database=student;server=.;user=sa;password=wintellect";
  18. private void btnsave_Click(object sender, EventArgs e) {
  19. conn = new SqlConnection(connstring);
  20. conn.Open();
  21. comm = new SqlCommand();
  22. comm.Connection = conn;
  23. //Creating instance of SqlParameter
  24. SqlParameter PmtrRollNo = new SqlParameter();
  25. PmtrRollNo.ParameterName = "@rn"; // Defining Name
  26. PmtrRollNo.SqlDbType = SqlDbType.Int; // Defining DataType
  27. PmtrRollNo.Direction = ParameterDirection.Input; // Setting the direction
  28. //Creating instance of SqlParameter
  29. SqlParameter PmtrName = new SqlParameter();
  30. PmtrName.ParameterName = "@nm"; // Defining Name
  31. PmtrName.SqlDbType = SqlDbType.VarChar; // Defining DataType
  32. PmtrName.Direction = ParameterDirection.Input; // Setting the direction
  33. //Creating instance of SqlParameter
  34. SqlParameter PmtrCity = new SqlParameter();
  35. PmtrCity.ParameterName = "@ct"; // Defining Name
  36. PmtrCity.SqlDbType = SqlDbType.VarChar; // Defining DataType
  37. PmtrCity.Direction = ParameterDirection.Input; // Setting the direction
  38. // Adding Parameter instances to sqlcommand
  39. comm.Parameters.Add(PmtrRollNo);
  40. comm.Parameters.Add(PmtrName);
  41. comm.Parameters.Add(PmtrCity);
  42. // Setting values of Parameter
  43. PmtrRollNo.Value = Convert.ToInt32(txtrollno.Text);
  44. PmtrName.Value = txtname.Text;
  45. PmtrCity.Value = txtcity.Text;
  46. comm.CommandText = "insert into student_detail values(@rn,@nm,@ct)";
  47. try {
  48. comm.ExecuteNonQuery();
  49. MessageBox.Show("Saved");
  50. } catch (Exception) {
  51. MessageBox.Show("Not Saved");
  52. } finally {
  53. conn.Close();
  54. }
  55. }
  56. }
  57. }
Run the application.
Output
Clipboard02.gif
Fill in the form and click the "Save" button. The record will be saved to the database and a message box will be displayed with a confirmation message.
Clipboard03.gif
Now we retrieve records from the database. Take another button and set its text property as "Show". Add the following code for the "Show" button.
  1. private void btnshow_Click(object sender, EventArgs e) {
  2. conn = new SqlConnection(connstring);
  3. conn.Open();
  4. comm = new SqlCommand();
  5. comm.Connection = conn;
  6. //Creating instance of SqlParameter
  7. SqlParameter PmtrRollNo = new SqlParameter();
  8. PmtrRollNo.ParameterName = "@rn"; // Defining Name
  9. PmtrRollNo.SqlDbType = SqlDbType.Int; // Defining DataType
  10. PmtrRollNo.Direction = ParameterDirection.Input; // Setting the direction
  11. //Creating instance of SqlParameter
  12. SqlParameter PmtrName = new SqlParameter();
  13. PmtrName.ParameterName = "@nm"; // Defining Name
  14. PmtrName.SqlDbType = SqlDbType.VarChar; // Defining DataType
  15. PmtrName.Size = 30;
  16. PmtrName.Direction = ParameterDirection.Output; // Setting the direction
  17. //Creating instance of SqlParameter
  18. SqlParameter PmtrCity = new SqlParameter("@ct", SqlDbType.VarChar, 20);
  19. PmtrCity.Direction = ParameterDirection.Output; // Setting the direction
  20. // Adding Parameter instances to sqlcommand
  21. comm.Parameters.Add(PmtrRollNo);
  22. comm.Parameters.Add(PmtrName);
  23. comm.Parameters.Add(PmtrCity);
  24. // Setting values of Parameter
  25. PmtrRollNo.Value = Convert.ToInt32(txtrollno.Text);
  26. PmtrName.Value = txtname.Text;
  27. PmtrCity.Value = txtcity.Text;
  28. comm.CommandText = "select @nm=name,@ct=city from student_detail where rollno=@rn";
  29. try {
  30. comm.ExecuteNonQuery();
  31. txtname.Text = PmtrName.Value.ToString();
  32. txtcity.Text = PmtrCity.Value.ToString();
  33. } catch (Exception) {
  34. MessageBox.Show("Not Found");
  35. } finally {
  36. conn.Close();
  37. }
  38. }
Run the application.
Output
Clipboard04.gif
Enter a roll number and click the "Show" button. It will show all the related information of the student having the given roll number.
Clipboard05.gif
Here are some related resources.