Sunday, August 26, 2012

C# to SQL Server's Stored Procedure

this time, I will post about how access SP in SQL Server and take it to our application like Data Grid View or Repeater..

For example, let say that I want to take all the names from the database and put them in the List of String..

public List getAllUserName()
        {
            string spName = "spr_getAllName";

            List listUser = new List();
            String name= null;

            using (SqlDataReader sqlDataReader = SqlHelper.ExecuteReader(SqlConnection, CommandType.StoredProcedure, spName))
            {
                if (sqlDataReader.HasRows)
                {
                    while (sqlDataReader.Read())
                    {
                        name= new String();
                        name= sqlDataReader["Name"].ToString();
                        listUser.Add(name);
                    }
                }
            }
            return listUser;
        }

No comments:

Post a Comment