Advertisement
Guest User

Untitled

a guest
May 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Configuration;
  8.  
  9. public class PaintingDB
  10. {
  11. static SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
  12. public static List<Painting> getAllPaintings()
  13. {
  14.  
  15. List<Painting> paintingList = new List<Painting>();
  16. try
  17. {
  18. SqlCommand command = new SqlCommand("select P.id, A.id as Aid, P.Title, P.Price, P.imagefile, A.Name from Painting P, Artist A where P.artistId=A.id");
  19. command.Connection = connection;
  20. connection.Open();
  21. SqlDataReader reader = command.ExecuteReader();
  22. while (reader.Read())
  23. {
  24. Painting p = new Painting();
  25. p.Id = Convert.ToInt32(reader["Id"]);
  26. p.Title = reader["title"].ToString();
  27. p.ImageFile = reader["imagefile"].ToString();
  28. p.Price = Convert.ToDecimal(reader["price"]);
  29. Artist a = new Artist();
  30. a.Id = reader["Aid"].ToString();
  31. a.Name = reader["name"].ToString();
  32. p.Artist = a;
  33. paintingList.Add(p);
  34. }
  35. reader.Close();
  36. }
  37. finally
  38. {
  39. connection.Close();
  40. }
  41. return paintingList;
  42. }
  43.  
  44. public static int addPainting(Painting painting)
  45. {
  46. try
  47. {
  48. SqlCommand command = new SqlCommand("insert into Painting (categoryId, title, artistId, price, imageFile, status) values (@categoryId, @title, @artistId, @price, @imageFile, @status )");
  49. command.Parameters.AddWithValue("@categoryId", painting.Category.Name);
  50. command.Parameters.AddWithValue("@title", painting.Title);
  51. command.Parameters.AddWithValue("@artistId", painting.Artist.Id);
  52. command.Parameters.AddWithValue("@price", painting.Price);
  53. command.Parameters.AddWithValue("@imageFile", painting.ImageFile);
  54. command.Parameters.AddWithValue("@status", painting.Status);
  55. command.Connection = connection;
  56. connection.Open();
  57. if (command.ExecuteNonQuery() > 0)
  58. {
  59. command.CommandText = "Select @@identity";
  60. return Convert.ToInt32(command.ExecuteScalar());
  61. }
  62. }
  63. finally
  64. {
  65. connection.Close();
  66. }
  67. return -1;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement