Guest User

Untitled

a guest
Nov 12th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 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. namespace Prototype3.Database_Access_Data
  10. {
  11. public class db
  12. {
  13. SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["localhost"].ConnectionString);
  14. public void SendLocation(Location cs)
  15. {
  16.  
  17. SqlCommand com = new SqlCommand("SendGPS", con);
  18.  
  19.  
  20. com.CommandType = CommandType.StoredProcedure;
  21. com.Parameters.AddWithValue("@DeviceImei", cs.DeviceImei);
  22. com.Parameters.AddWithValue("@Latitude", cs.Latitude);
  23. com.Parameters.AddWithValue("@Longitude", cs.Longitude);
  24. com.Parameters.AddWithValue("@Distance", cs.Distance);
  25. com.Parameters.AddWithValue("@LocationSend", cs.LocationSend);
  26. con.Open();
  27. com.Connection = con;
  28. com.ExecuteNonQuery();
  29. con.Close();
  30.  
  31. }
  32.  
  33. public DataTable LocationHistory(LocationHistory cs)
  34. {
  35. DataTable dt = new DataTable();
  36.  
  37. using (SqlCommand sqlCmd = new SqlCommand("GetLocationHistory", con))
  38. {
  39. sqlCmd.CommandType = CommandType.StoredProcedure;
  40. sqlCmd.Parameters.Add(new SqlParameter("DeviceImei", cs.DeviceImei));
  41.  
  42. try
  43. {
  44. con.Open();
  45. dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
  46. }
  47. catch (SqlException ex)
  48. {
  49. //Handle errors as you see fit
  50. }
  51. }
  52.  
  53. return dt;
  54. }
  55. public DataSet GetUser()
  56. {
  57. SqlCommand com = new SqlCommand("GetUser", con);
  58. com.CommandType = CommandType.StoredProcedure;
  59. SqlDataAdapter da = new SqlDataAdapter(com);
  60. DataSet ds = new DataSet();
  61. da.Fill(ds);
  62. return ds;
  63.  
  64.  
  65. }
  66. public void SendDistance(Location cs)
  67. {
  68.  
  69. SqlCommand com = new SqlCommand("SendDistance", con);
  70.  
  71.  
  72. com.CommandType = CommandType.StoredProcedure;
  73. com.Parameters.AddWithValue("@DeviceImei", cs.DeviceImei);
  74. com.Parameters.AddWithValue("@Distance", cs.Distance);
  75. com.Parameters.AddWithValue("@LocationSend", cs.LocationSend);
  76. con.Open();
  77. com.Connection = con;
  78. com.ExecuteNonQuery();
  79. con.Close();
  80. }
  81.  
  82. public DataTable FlagingDevice(FlagingDevice cs)
  83. {
  84. DataTable dt = new DataTable();
  85.  
  86. using (SqlCommand sqlCmd = new SqlCommand("FlagingDevice", con))
  87. {
  88. sqlCmd.CommandType = CommandType.StoredProcedure;
  89. sqlCmd.Parameters.Add(new SqlParameter("Login", cs.Login));
  90.  
  91. try
  92. {
  93. con.Open();
  94. dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
  95. }
  96. catch (SqlException ex)
  97. {
  98. //Handle errors as you see fit
  99. }
  100. }
  101.  
  102. return dt;
  103. }
  104. public DataTable SearchByOfficerName(SearchHistory cs)
  105. {
  106. DataTable dt = new DataTable();
  107.  
  108. using (SqlCommand sqlCmd = new SqlCommand("SearchByOfficer", con))
  109. {
  110. sqlCmd.CommandType = CommandType.StoredProcedure;
  111. sqlCmd.Parameters.Add(new SqlParameter("Name", cs.Name));
  112. sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
  113. sqlCmd.Parameters.Add(new SqlParameter("ID", cs.DivisionID));
  114. sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));
  115.  
  116. try
  117. {
  118. con.Open();
  119. dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
  120. }
  121. catch (SqlException ex)
  122. {
  123. //Handle errors as you see fit
  124. }
  125. }
  126.  
  127. return dt;
  128. }
  129. public DataTable SearchByDeviceName(SearchHistory cs)
  130. {
  131. DataTable dt = new DataTable();
  132.  
  133. using (SqlCommand sqlCmd = new SqlCommand("SearchByDeviceName", con))
  134. {
  135. sqlCmd.CommandType = CommandType.StoredProcedure;
  136. sqlCmd.Parameters.Add(new SqlParameter("Name", cs.OfficerName));
  137. sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
  138. sqlCmd.Parameters.Add(new SqlParameter("DivisionID", cs.DivisionID));
  139. sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));
  140. sqlCmd.Parameters.Add(new SqlParameter("BoxID", cs.BoxID));
  141. try
  142. {
  143. con.Open();
  144. dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
  145. }
  146. catch (SqlException ex)
  147. {
  148. //Handle errors as you see fit
  149. }
  150. }
  151.  
  152. return dt;
  153. }
  154. public DataTable SearchByElectoralDivision(SearchHistory cs)
  155. {
  156. DataTable dt = new DataTable();
  157.  
  158. using (SqlCommand sqlCmd = new SqlCommand("SearchByElectoralDivision", con))
  159. {
  160. sqlCmd.CommandType = CommandType.StoredProcedure;
  161. sqlCmd.Parameters.Add(new SqlParameter("DivisionID", cs.DivisionID));
  162. sqlCmd.Parameters.Add(new SqlParameter("Name", cs.Name));
  163. sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
  164. sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));
  165.  
  166. try
  167. {
  168. con.Open();
  169. dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
  170. }
  171. catch (SqlException ex)
  172. {
  173. //Handle errors as you see fit
  174. }
  175. }
  176.  
  177. return dt;
  178. }
  179. public DataTable SearchByPollingStation(SearchHistory cs)
  180. {
  181. DataTable dt = new DataTable();
  182.  
  183. using (SqlCommand sqlCmd = new SqlCommand("SearchByPollingStation", con))
  184. {
  185. sqlCmd.CommandType = CommandType.StoredProcedure;
  186. sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
  187. sqlCmd.Parameters.Add(new SqlParameter("OfficerName", cs.OfficerName));
  188. sqlCmd.Parameters.Add(new SqlParameter("DivisionID", cs.DivisionID));
  189. sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));
  190.  
  191. try
  192. {
  193. con.Open();
  194. dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
  195. }
  196. catch (SqlException ex)
  197. {
  198. //Handle errors as you see fit
  199. }
  200. }
  201.  
  202. return dt;
  203. }
  204. public void SendBox(Box cs)
  205. {
  206. SqlCommand com = new SqlCommand("SendBox", con);
  207.  
  208.  
  209. com.CommandType = CommandType.StoredProcedure;
  210. com.Parameters.AddWithValue("@Id", cs.Id);
  211. com.Parameters.AddWithValue("@StationID", cs.PollingStationID);
  212. com.Parameters.AddWithValue("@DeviceImei", cs.DeviceImei);
  213. con.Open();
  214. com.Connection = con;
  215. com.ExecuteNonQuery();
  216. con.Close();
  217. }
  218.  
  219. public DataSet FlagingDeviceWithoutParameters()
  220. {
  221. SqlCommand com = new SqlCommand("FlagingDevice(Without Parameters)", con);
  222. com.CommandType = CommandType.StoredProcedure;
  223. SqlDataAdapter da = new SqlDataAdapter(com);
  224. DataSet ds = new DataSet();
  225. da.Fill(ds);
  226. return ds;
  227. }
  228. public DataTable SearchByBox(SearchHistory cs)
  229. {
  230.  
  231. DataTable dt = new DataTable();
  232.  
  233. using (SqlCommand sqlCmd = new SqlCommand("SearchByBox", con))
  234. {
  235. sqlCmd.CommandType = CommandType.StoredProcedure;
  236. sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));
  237. sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
  238. sqlCmd.Parameters.Add(new SqlParameter("ElectoralDivisionID", cs.ElectoralDivisionID));
  239. sqlCmd.Parameters.Add(new SqlParameter("OfficerName", cs.OfficerName));
  240.  
  241. try
  242. {
  243. con.Open();
  244. dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
  245. }
  246. catch (SqlException ex)
  247. {
  248. }
  249. return dt;
  250. }
  251.  
  252. }
  253. }
  254. }
  255.  
  256. using Prototype3.Database_Access_Data;
  257. using System;
  258. using System.Collections.Generic;
  259. using System.Data;
  260. using System.Linq;
  261. using System.Net;
  262. using System.Net.Http;
  263. using System.Threading.Tasks;
  264. using System.Web.Http;
  265. using System.Web.Http.Description;
  266.  
  267. namespace Prototype3.Controllers
  268. {
  269. public class ValuesController : ApiController
  270. {
  271. Database_Access_Data.db dblayer = new Database_Access_Data.db();
  272.  
  273. [HttpPost]
  274. [Route("api/Values/SendLocation")]
  275. public IHttpActionResult SendLocation([FromBody]Location cs)
  276. {
  277. try
  278. {
  279. if (!ModelState.IsValid)
  280. {
  281. return BadRequest(ModelState);
  282. }
  283. dblayer.SendLocation(cs);
  284. return Ok("Success");
  285. }
  286. catch (Exception e)
  287. {
  288. return Ok("Something went Wrong" + e);
  289. }
  290.  
  291. }
  292.  
  293. [HttpPost]
  294. [Route("api/Values/GetLocationHistory")]
  295. public IHttpActionResult GetLocationHistory([FromBody]LocationHistory cs)
  296. {
  297. try
  298. {
  299. if (!ModelState.IsValid)
  300. {
  301. return BadRequest(ModelState);
  302. }
  303. var table = dblayer.LocationHistory(cs);
  304. return Ok(table);
  305. }
  306. catch (Exception e)
  307. {
  308. return Ok("Something went Wrong" + e);
  309. }
  310.  
  311. }
  312. [HttpPost]
  313. [Route("api/Values/SendDistance")]
  314. public IHttpActionResult SendDistance([FromBody]Location cs)
  315. {
  316. try
  317. {
  318. if (!ModelState.IsValid)
  319. {
  320. return BadRequest(ModelState);
  321. }
  322. dblayer.SendDistance(cs);
  323. return Ok("Success");
  324. }
  325. catch (Exception e)
  326. {
  327. return Ok("Something went Wrong" + e);
  328. }
  329.  
  330. }
  331.  
  332. [HttpGet]
  333. [Route("api/Values/GetUser")]
  334. public DataSet GetUser()
  335. {
  336. DataSet ds = dblayer.GetUser();
  337. return ds;
  338. }
  339. [HttpPost]
  340. [Route("api/Values/FlagingDevice")]
  341. public IHttpActionResult FlagingDevice([FromBody]FlagingDevice cs)
  342. {
  343. try
  344. {
  345. if (!ModelState.IsValid)
  346. {
  347. return BadRequest(ModelState);
  348. }
  349. var table = dblayer.FlagingDevice(cs);
  350. return Ok(table);
  351.  
  352. }
  353. catch (Exception e)
  354. {
  355. return Ok("Something went Wrong" + e);
  356. }
  357.  
  358. }
  359.  
  360. [HttpPost]
  361. [Route("api/Values/SendBox")]
  362. public IHttpActionResult SendBox([FromBody]Box cs)
  363. {
  364. try
  365. {
  366. if (!ModelState.IsValid)
  367. {
  368. return BadRequest(ModelState);
  369. }
  370. dblayer.SendBox(cs);
  371.  
  372. return Ok("Success");
  373. }
  374. catch (Exception e)
  375. {
  376. return Ok("Something went Wrong" + e);
  377. }
  378.  
  379. }
  380.  
  381. [HttpGet]
  382. [Route("api/Values/FlagingDevice(WithoutParameters)")]
  383. public DataSet FlagingDevice1()
  384. {
  385. DataSet ds = dblayer.FlagingDeviceWithoutParameters();
  386. return ds;
  387. }
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395. }
  396. }
Add Comment
Please, Sign In to add comment