Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r "System.Configuration"
- #r "System.Data"
- using System.Net;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Threading.Tasks;
- public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,
- TraceWriter log)
- {
- int num;
- string query = req.GetQueryNameValuePairs()
- .FirstOrDefault(q => string.Compare(q.Key, "num", true) == 0)
- .Value;
- if (query == null)
- {
- // Get request body
- dynamic data = await req.Content.ReadAsAsync<object>();
- num = data.num;
- }
- else {
- num = Convert.ToInt32(query);
- }
- int[] numRead = new int[3];
- string[] nameRead = new string[3];
- var str =
- ConfigurationManager.ConnectionStrings["sqldb_connection"].ConnectionString;
- using (SqlConnection conn = new SqlConnection(str))
- {
- conn.Open();
- var text = "select num,name from bus where num=@num;";
- using (SqlCommand cmd = new SqlCommand(text, conn))
- {
- cmd.Parameters.AddWithValue("@num", num);
- using (SqlDataReader reader = cmd.ExecuteReader())
- {
- int i = 0;
- while (reader.Read())
- {
- numRead[i] = reader.GetInt32(0);
- nameRead[i++] = reader.GetString(1);
- Console.WriteLine(numRead[i] + ":" + nameRead[i]);
- }
- }
- }
- }
- return req.CreateResponse(HttpStatusCode.OK, new {
- name = nameRead[0],
- num = numRead[0]
- });
- }
- return req.CreateResponse(HttpStatusCode.OK, new {
- "item1" : {
- name = nameRead[0],
- num = numRead[0]
- },
- "item2" : {
- name = nameRead[1],
- num = numRead[1]
- },
- "item3" : {
- name = nameRead[2],
- num = numRead[2]
- }
- });
Add Comment
Please, Sign In to add comment