Guest User

Untitled

a guest
Dec 9th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. namespace ProductsApp.Models
  2. {
  3. public class File
  4. {
  5. public int Id {get; set;}
  6. public string Text {get; set;}
  7. }
  8. }
  9.  
  10. public IEnumerable<File> Get()
  11. {
  12. try
  13. {
  14. command = conn.CreateCommand();
  15. command.CommandText = "SELECT * FROM Files";
  16. conn.Open();
  17.  
  18. MySqlDataReader reader = command.ExecuteReader();
  19. while (reader.Read())
  20. {
  21. //How to output the rows ????
  22. }
  23. }
  24. catch (Exception ex)
  25. {
  26. Console.WriteLine(ex.Message);
  27. }
  28.  
  29. return null; // return the list
  30. }
  31.  
  32. public IEnumerable<File> Get()
  33. {
  34. List<File> list = new List<File>();
  35. try
  36. {
  37. command = conn.CreateCommand();
  38. command.CommandText = "SELECT * FROM Files";
  39. conn.Open();
  40.  
  41. using(MySqlDataReader reader = command.ExecuteReader())
  42. {
  43. while (reader.Read())
  44. {
  45. //How to output the rows ????
  46. int id = (int) reader["Id"];//Assuming column name is 'Id' and value if typeof(int)
  47. string text = (string) reader["Text"];//Assuming column name is `Text` and typeof(string)
  48. var file = new File {Id = id, Text = text};
  49. list.Add(file);
  50. }
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. Console.WriteLine(ex.Message);
  56. }
  57.  
  58. return list; // return the list
  59. }
  60.  
  61. Response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml;charset=utf-8");
  62.  
  63. public static MySqlConnection conn()
  64. {
  65. string conn_string = "server=localhost;port=3306;database=testmvc;username=root;password=root;";
  66.  
  67.  
  68. MySqlConnection conn = new MySqlConnection(conn_string);
  69.  
  70. return conn;
  71. }
  72.  
  73. public MySqlConnection con = WebApiConfig.conn();
  74.  
  75. public IHttpActionResult GetAllProduct()
  76. {
  77. IList<product> pro = null;
  78.  
  79. try
  80. {
  81. con.Open();
  82. MySqlCommand cmd = new MySqlCommand("select * from product", con);
  83.  
  84. cmd.CommandType = CommandType.Text;
  85.  
  86. MySqlDataAdapter da = new MySqlDataAdapter(cmd);
  87. DataSet ds = new DataSet();
  88. da.Fill(ds);
  89.  
  90. pro = ds.Tables[0].AsEnumerable().Select(dataRow => new product { Pname = dataRow.Field<string>("pname"), Pid = dataRow.Field<int>("pid"), Pprice = dataRow.Field<decimal>("pprice") }).ToList();
  91.  
  92. }
  93. finally
  94. {
  95. con.Close();
  96. }
  97.  
  98.  
  99. if (pro.Count == 0)
  100. {
  101. return NotFound();
  102. }
  103.  
  104. return Ok(pro);
  105. }
  106.  
  107. public IHttpActionResult PostNewProduct(product pro)
  108. {
  109. try
  110. {
  111. con.Open();
  112.  
  113. MySqlCommand cmd = new MySqlCommand();
  114.  
  115. cmd.Connection = con;
  116. cmd.CommandText = "SELECT MAX(pid) from product";
  117.  
  118. cmd.CommandType = CommandType.Text;
  119.  
  120. int maxid = Convert.ToInt16(cmd.ExecuteScalar().ToString())+1;
  121.  
  122.  
  123. cmd.CommandText = "insert into product values(" + maxid + ",'" + pro.Pname + "'," + pro.Pprice + ")";
  124.  
  125. cmd.ExecuteNonQuery();
  126.  
  127. }
  128. finally
  129. {
  130. con.Close();
  131. }
  132.  
  133. return Ok();
  134. }
  135.  
  136. public IHttpActionResult PutOldProduct(product pro)
  137. {
  138.  
  139. string sql = "update product set pname='" + pro.Pname + "',pprice=" + pro.Pprice + " where pid=" + pro.Pid + "";
  140. try
  141. {
  142. con.Open();
  143. MySqlCommand cmd = new MySqlCommand(sql, con);
  144.  
  145. cmd.CommandType = CommandType.Text;
  146.  
  147. cmd.ExecuteNonQuery();
  148.  
  149. }
  150. finally
  151. {
  152. con.Close();
  153. }
  154.  
  155. return Ok();
  156. }
  157.  
  158. public IHttpActionResult Delete(int id)
  159. {
  160. string sql = "delete from product where pid=" + id + "";
  161. try
  162. {
  163. con.Open();
  164. MySqlCommand cmd = new MySqlCommand(sql, con);
  165.  
  166. cmd.CommandType = CommandType.Text;
  167.  
  168. cmd.ExecuteNonQuery();
  169.  
  170. }
  171. finally
  172. {
  173. con.Close();
  174. }
  175.  
  176. return Ok();
  177. }
Add Comment
Please, Sign In to add comment