Advertisement
Guest User

Untitled

a guest
May 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 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.  
  10. public class ArtistDB
  11. {
  12. static SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
  13.  
  14. public static int insertItem(string desc)
  15. {
  16. try
  17. {
  18. SqlCommand command = new SqlCommand("insert into Item values (@desc)");
  19. command.Parameters.AddWithValue("@desc", desc);
  20. command.Connection = connection;
  21. connection.Open();
  22. if (command.ExecuteNonQuery() > 0)
  23. {
  24. command.CommandText = "Select @@identity";
  25. return Convert.ToInt32(command.ExecuteScalar());
  26. }
  27. }
  28. finally
  29. {
  30. connection.Close();
  31. }
  32. return -1;
  33. }
  34.  
  35. public static int insertArtist(Artist a)
  36. {
  37. try
  38. {
  39. SqlCommand command = new SqlCommand("insertArtist");
  40. command.CommandType = CommandType.StoredProcedure;
  41. command.Parameters.AddWithValue("@id", a.Id);
  42. command.Parameters.AddWithValue("@dob", a.Dob);
  43. command.Parameters.AddWithValue("@name", a.Name);
  44. command.Parameters.AddWithValue("@profile", a.Profile);
  45. command.Parameters.AddWithValue("@imagefile", a.ImageFile);
  46. command.Connection = connection;
  47. connection.Open();
  48. return command.ExecuteNonQuery();
  49. }
  50. finally
  51. {
  52. connection.Close();
  53. }
  54. }
  55.  
  56. public static int insertArtist1(Artist a)
  57. {
  58. try
  59. {
  60. SqlCommand command = new SqlCommand("insert into artist values (@id, @dob, @name, @profile, @imagefile)");
  61. command.Parameters.AddWithValue("@id", a.Id);
  62. command.Parameters.AddWithValue("@dob", a.Dob);
  63. command.Parameters.AddWithValue("@name", a.Name);
  64. command.Parameters.AddWithValue("@profile", a.Profile);
  65. command.Parameters.AddWithValue("@imagefile", a.ImageFile);
  66. command.Connection = connection;
  67. connection.Open();
  68. return command.ExecuteNonQuery();
  69. }
  70. finally
  71. {
  72. connection.Close();
  73. }
  74. }
  75.  
  76.  
  77.  
  78. public static List<Artist> getArtistByName(string name)
  79. {
  80. List<Artist> artistList = new List<Artist>();
  81. try
  82. {
  83. SqlCommand command = new SqlCommand("Select * from Artist where name like @name");
  84. command.Parameters.AddWithValue("@name", "%" + name + "%");
  85. command.Connection = connection;
  86. connection.Open();
  87. SqlDataReader reader = command.ExecuteReader();
  88.  
  89. while (reader.Read())
  90. {
  91. Artist a = new Artist(reader["Id"].ToString(), reader["Name"].ToString(),
  92. Convert.ToDateTime(reader["Dob"]), reader["profile"].ToString(), reader["imagefile"].ToString());
  93. artistList.Add(a);
  94. }
  95. reader.Close();
  96. }
  97. finally
  98. {
  99. connection.Close();
  100. }
  101. return artistList;
  102. }
  103.  
  104. public static List<Artist> getAllArtists()
  105. {
  106.  
  107. List<Artist> artistList = new List<Artist>();
  108. try
  109. {
  110. SqlCommand command = new SqlCommand("Select * from Artist");
  111. command.Connection = connection;
  112. connection.Open();
  113. SqlDataReader reader = command.ExecuteReader();
  114.  
  115. while (reader.Read())
  116. {
  117. Artist a = new Artist(reader["Id"].ToString(), reader["Name"].ToString(),
  118. Convert.ToDateTime(reader["Dob"]), reader["profile"].ToString(), reader["imagefile"].ToString());
  119. artistList.Add(a);
  120. }
  121. reader.Close();
  122. }
  123. finally
  124. {
  125. connection.Close();
  126. }
  127. return artistList;
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement