The following code demonstrates few things in C#.NET:
//CALLING THE STORED PROCEDURE IN SQL SERVER
SqlConnection conn = new SqlConnection("Server=(local);DataBase=Rohit;Integrated
Security=SSPI");
conn.Open();
int
vEmpno;
String
vEname;
int
vSalary;
//insert
command
//Pick
all the values from the textboxes
vEmpno = Convert.ToInt32(textBox1.Text);
vEname = textBox2.Text;
vSalary = Convert.ToInt32(textBox3.Text);
SqlCommand
cmd = new SqlCommand("P1", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@vEmpno", vEmpno);
cmd.Parameters.AddWithValue("@vEname", vEname);
cmd.Parameters.AddWithValue("@vSalary", vSalary);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Saved");
conn.Close();
How
To display the Employee Numbers and Enames from Table for a selected job
SqlConnection
conn = new SqlConnection("Server=(local);DataBase=Rohit;Integrated
Security=SSPI");
conn.Open();
//Get the selected job from
Listbox3
String vSelectedJob =
listBox3.SelectedItem.ToString();
String vQuery = "Select * from
Emp where job='"+vSelectedJob+"'";
SqlCommand cmd = new SqlCommand(vQuery,
conn);
SqlDataReader
vDr=cmd.ExecuteReader();
listBox1.Items.Clear();
listBox2.Items.Clear();
while (vDr.Read())
{
listBox1.Items.Add(vDr["Empno"]);
listBox2.Items.Add(vDr["Ename"]);
}
conn.Close();
//To
get all the distinct jobs from Emp
SqlConnection conn = new SqlConnection("Server=(local);DataBase=Rohit;Integrated
Security=SSPI");
conn.Open();
String vQuery = "Select
distinct(job) from Emp";
SqlCommand cmd = new SqlCommand(vQuery,
conn);
SqlDataReader vDr =
cmd.ExecuteReader();
while (vDr.Read())
{
listBox3.Items.Add(vDr[0]);
}
conn.Close();
//USE OF EXECUTE SCALAR
SqlConnection conn = new SqlConnection("Server=(local);DataBase=Rohit;Integrated
Security=SSPI");
conn.Open();
int vEmpno = Convert.ToInt32(textBox1.Text);
String vQuery = "Select ename
from emp where Empno = "+ vEmpno;
SqlCommand cmd = new SqlCommand(vQuery,
conn);
Object vobj = cmd.ExecuteScalar();
if (vobj == null)
{
MessageBox.Show("Sorry no
record found");
}
else
{
textBox2.Text =
vobj.ToString();
}
conn.Close();
Happy Coding. Keep on trying
Comments