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();
}

Recommended Answers

All 3 Replies

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.

which upper query tell me briefly what do you want then i can help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.