- Back to Home »
- ASP.NET »
- MVC Web API Example code in controller ADO.NET
Posted by :
Sudhir Chekuri
Thursday, 24 October 2013
ADO.NET C#.NET code in controller to return table data from sql server database
using System;using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.SqlClient;
using System.Data;
namespace ConnectWebAPI.Controllers
{
public class ConnectController : ApiController
{
SqlConnection con = new SqlConnection("Data Source=sudhir-pc;Initial Catalog=Connect;Integrated Security=True");
// GET api/<controller>
public DataTable Get()
{
SqlCommand cmd = new SqlCommand("select * from tbl_register",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
//Circle k macs store
// GET api/<controller>/5
public string[] Get(int id)
{
SqlCommand cmd = new SqlCommand("select * from tbl_register where sno="+id+"", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
string[] s = new string[4];
for (int i = 0; i < 4; i++)
{
s[i] = ds.Tables[0].Rows[0][i + 1].ToString();
}
return s;
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
}
URL to retreive data
http://localhost:7239/api/connect/Data
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<DataTable xmlns="http://schemas.datacontract.org/2004/07/System.Data">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="sno" type="xs:int" minOccurs="0"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element name="pwd" type="xs:string" minOccurs="0"/>
<xs:element name="phoneno" type="xs:string" minOccurs="0"/>
<xs:element name="stat" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<sno>1</sno>
<name>sudhir</name>
<pwd>sudhi</pwd>
<phoneno>9985323114</phoneno>
<stat>welcome to connect</stat>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<sno>2</sno>
<name>ramu</name>
<pwd>ram</pwd>
<phoneno>9989989989</phoneno>
<stat>hello world</stat>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataTable>
URL
data
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>sudhir</string>
<string>sudhi</string>
<string>9985323114</string>
<string>welcome to connect</string>
</ArrayOfstring>
Controller code to insert data into data base table
public int Get(string name,string emailid,string pwd)
{
SqlCommand cmd = new SqlCommand("insert into tbl_register(name,emailid,pwd) values('"+name+"','"+emailid+"','"+pwd+"')", con);
con.Open();
int i = cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
return i;
}
code in webapi.config file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace ConnectWebAPI
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{name}/{emailid}/{pwd}",
defaults: new { name = RouteParameter.Optional, emailid = RouteParameter.Optional, pwd = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}