public List<Post> findPosts(string type) { postList = new List<Post>(); using (SqlConnection cn = new SqlConnection()) {//and status = false cn.ConnectionString = @"Data Source=kraka.ucn.dk;Initial Catalog=dmai0914_2Sem_6;Persist Security Info=True;User ID=dmai0914_2Sem_6;Password=IsAllowed"; cn.Open(); string strSQL = "Select * From Post where type=@type"; using (SqlCommand myCommand = new SqlCommand(strSQL, cn)) { myCommand.Parameters.AddWithValue("@type", type); try { SqlDataReader myDataReader = myCommand.ExecuteReader(); while (myDataReader.Read()) { Post u = new Post(); u.id = (int)myDataReader["id"]; u.type = (string)myDataReader["type"]; u.details = (string)myDataReader["details"]; u.price = (string)myDataReader["price"]; u.phoneNr = (string)myDataReader["phoneNr"]; u.address = (string)myDataReader["address"]; u.postalCode = (string)myDataReader["postalCode"]; u.expiration = (DateTime)myDataReader["expiration"]; u.isReserved = (bool)myDataReader["isReserved"]; postList.Add(u); } } catch (SqlException e) { //error postList = null; } } cn.Close(); } return postList; }
public Post loadPost(int id) { Post u = new Post(); using (SqlConnection cn = new SqlConnection()) { cn.ConnectionString = @"Data Source=kraka.ucn.dk;Initial Catalog=dmai0914_2Sem_6;Persist Security Info=True;User ID=dmai0914_2Sem_6;Password=IsAllowed"; cn.Open(); string strSQL = "Select * from Post where id = @id"; using (SqlCommand myCommand = new SqlCommand(strSQL, cn)) { myCommand.Parameters.AddWithValue("@id", id); try { SqlDataReader myDataReader = myCommand.ExecuteReader(); if (myDataReader.Read()) { u.type = (string)myDataReader["type"]; u.details = (string)myDataReader["details"]; u.price = (string)myDataReader["price"]; u.phoneNr = (string)myDataReader["phoneNr"]; u.address = (string)myDataReader["address"]; u.postalCode = (string)myDataReader["postalCode"]; u.expiration = (DateTime)myDataReader["expiration"]; u.isReserved = (bool)myDataReader["isReserved"]; } } catch (SqlException e) { } } cn.Close(); } // test for null values return u; }