The code bellow is a sql querry, made in code. I would like to know how to do such stored procedure:

string[] NameSeperated = FullName.Split(' ');
string querryName = @"SELECT StudentID FROM Students WHERE Name = @name";
if (NameSeperated.Length > 1)
    querryName += " AND LastName = @lastName";

using (SqlCommand cmd = new SqlCommand(querryName, sqlConn))
{
    cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = NameSeperated[0].Trim();
    if (NameSeperated.Length == 2)
        cmd.Parameters.Add("@lastName", SqlDbType.VarChar, 50).Value = NameSeperated[1].Trim();
    else if (NameSeperated.Length == 3)
        cmd.Parameters.Add("@lastName", SqlDbType.VarChar, 50).Value = NameSeperated[1].Trim() + " " + NameSeperated[2].Trim();
}

Edited 6 Years Ago by Mitja Bonca: n/a

hey Mitja do you have created procedure for that. first create the procedure and then write your code as per below for as per the DMl query.

SqlConnection con = new SqlConnection("");//give your connection string to open the connection
            con.Open();
            SqlCommand cmd = new SqlCommand("proc_new_test",con);//proc_new_test is my sp name
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", "8");// passing the value for that
            cmd.Parameters.AddWithValue("@name", "rajesh");
//you can pass your parameter like textbox and all that
            cmd.Parameters.AddWithValue("@mobile", "7894561");
            cmd.Parameters.AddWithValue("@total", "7000");
            cmd.ExecuteNonQuery();
            con.Close();

Thats what I want to do. I would like to create a procedure from that upper querry.
But I do not know how to do it.
Here I`d need some help.

This article has been dead for over six months. Start a new discussion instead.