ASP. NET WebAPI Connection Database

Source: Internet
Author: User

The ASP. NET Web API is a framework for easily building HTTP services that can access a wide range of clients, including browsers and mobile devices. The ASP. NET Web API is an ideal platform for building RESTful applications on the. NET Framework. This article mainly implements the ASP. NET WebAPI Connection database to get the data and return it in JSON string format. 1. Create an ASP. 2. Select Web API; 3. Create a new project to complete; Modify the Get method in ValuesController.cs and connect to the SQL Server database to get the data back in JSON string format:

Using System;Using System.Collections.Generic;Using System.Data;Using System.Data.SqlClient;Using System.Web.Http;Using Newtonsoft.json;namespace webapplication1.controllers{PublicClass Valuescontroller:apicontroller {GET api/valuesPublic ienumerable<String>Get () {ReturnNewString[] {"Value1","Value2"}; }GET API/VALUES/5PublicStringGet (int id) {try {SqlConnection SqlConnection =New SqlConnection ("Data source=127.0.0.1;initial catalog=garyweb;integrated security=true; User Id=sa; password=123456 "); Sqlconnection.open ();String sql ="SELECT * from Users"; DataSet DataSet =New DataSet (); SqlDataAdapter SqlDataAdapter =New SqlDataAdapter (SQL, sqlConnection); Sqldataadapter.fill (DataSet);return jsonconvert.serializeobject (DataSet);} catch (Exception ex) {return ex. ToString (); }} //POST api/values public void Span class= "Hljs-title" >post ([Frombody]string value) {} Span class= "hljs-comment" >//PUT api/values/5 public void Span class= "Hljs-title" >put (int ID, [Frombody]string value) {} //DELETE api/values/5 public Span class= "Hljs-keyword" >void delete (int ID) {}}}   
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

Run project: Get back JSON string data:

{    "Table ":[        {            "UserID ":1, "UserName ":"Admin", "DisplayName ":"Admin1", "Password ":"Jzae727k08kaomksgoagzww/xvqgr/pkegimkjrcbji=", "Email ":"[Email protected]", "Status ":0, "Registrationtime ":"2017/6/1", "Logintime ":NULL, "Loginip ":null}, {"UserID ":2, "UserName ":"Admin1", "DisplayName ":"Admin1", "Password ":"Jzae727k08kaomksgoagzww/xvqgr/pkegimkjrcbji=", "Email ":"[Email protected]", "Status ":0, "Registrationtime ":"2017/6/1", "Logintime ":NULL, "Loginip ":null}, {"UserID ":3, "UserName ": "admin2", "displayname":  "admin2", "password":  "jzae727k08kaomksgoagzww/xvqgr/pkegimkjrcbji=", "email ": " [email protected] "," status ": 0,"  Registrationtime ": " 2017/6/1 ","  Logintime ": null," LoginIP ": Span class= "Hljs-value" >null}]}        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

ASP. NET WebAPI Connection Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.