Here is what we want to do
1. Create an ASP.NET Web service. This web service retrieves the data from SQL Server database table, returns it in JSON formt.
2. Call the web service using AngularJS and display employee data on the web page 

Step 1 : Create SQL Server table and insert employee data

Create table tblEmployees
(
Id int primary key identity,
Name nvarchar(50),
Gender nvarchar(10),
Salary int
)
Go Insert into tblEmployees values ('Ben', 'Male', 55000)
Insert into tblEmployees values ('Sara', 'Female', 68000)
Insert into tblEmployees values ('Mark', 'Male', 57000)
Insert into tblEmployees values ('Pam', 'Female', 53000)
Insert into tblEmployees values ('Todd', 'Male', 60000)
Go

Step 2 : Create new empty asp.net web application project. Name it Demo. 

Step 3 : Include the following settings in web.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DBCS"
connectionString="server=.;database=SampleDB; integrated security=SSPI"/>
</connectionStrings>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
</system.web>
</configuration>

Step 4 : Add a class file to the project. Name it Employee.cs. Copy and paste the following code.

namespace Demo
{
public class Employee
{
public int id { get; set; }
public string name { get; set; }
public string gender { get; set; }
public int salary { get; set; }
}
}

Step 5 : Add a new WebService (ASMX). Name it EmployeeService.asmx. Copy and paste the following code.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Services; namespace Demo
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class EmployeeService : System.Web.Services.WebService
{
[WebMethod]
public void GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>(); string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select * from tblEmployees", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.id = Convert.ToInt32(rdr["Id"]);
employee.name = rdr["Name"].ToString();
employee.gender = rdr["Gender"].ToString();
employee.salary = Convert.ToInt32(rdr["Salary"]);
listEmployees.Add(employee);
}
} JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(listEmployees));
}
}
}

Step 6 : Add a new folder to the project. Name it Scripts. Download angular.js script file from http://angularjs.org, and past it in Scripts folder. 

Step 7 : Add a new JavaScript file to the Scripts folder. Name it Script.js. Copy and paste the following code.

/// <reference path="angular.min.js" />

var app = angular
.module("myModule", [])
.controller("myController", function ($scope, $http) { $http.get("EmployeeService.asmx/GetAllEmployees")
.then(function (response) {
$scope.employees = response.data;
});
});

Step 8 : Add a new stylesheet to the project. Name it Styles.css. Copy and paste the following styles in it.

body {
font-family: Arial;
} table {
border-collapse: collapse;
} td {
border: 1px solid black;
padding: 5px;
} th {
border: 1px solid black;
padding: 5px;
text-align: left;
}

Step 9 : Add an HTML page to the ASP.NET project. Copy and paste the following HTML and Angular code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/angular.js"></script>
<script src="Scripts/Script.js"></script>
<link href="Styles.css" rel="stylesheet" />
</head>
<body ng-app="myModule">
<div ng-controller="myController">
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Gender</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">
<td>{{employee.id}}</td>
<td>{{employee.name}}</td>
<td>{{employee.gender}}</td>
<td>{{employee.salary}}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

最新文章

  1. HaProxy配置
  2. gulp-rev同时将js和css文件写在一个rev-manifest.json文件里面的方式探讨
  3. 修改linux文件权限命令:chmod(转)
  4. DOM技术实现竞赛题页面
  5. 集合1--毕向东java基础教程视频学习笔记
  6. 在Linux CentOS上编译并安装Clang 3.5.0
  7. 精通D3.js学习笔记(2)比例尺和坐标
  8. UVA 11795
  9. cocos2dx开发笔记
  10. Java中单例设计模式总结
  11. HDOJ 1846 Brave Game
  12. 管理node_modules
  13. 分析uboot中 make xxx_config过程
  14. python全栈开发day54-mysql库操作、表操作、数据类型、完整性约束
  15. php curl抓取类分享
  16. JavaScript高级程序设计学习(六)之设计模式
  17. L298 猴子进化过程
  18. PAT甲级 1129. Recommendation System (25)
  19. cocos2d-x Schedule详解
  20. jq消除网页滚动条

热门文章

  1. P4929-[模板]舞蹈链(DLX)
  2. 关于国密HTTPS 的那些事(一)
  3. webpack基本用法及原理(10000+)
  4. MySQL强人“锁”难《死磕MySQL系列 三》
  5. 设计模式如何提升 vivo 营销自动化业务扩展性 | 引擎篇01
  6. transformers---BERT
  7. Python中is与==区别
  8. SudokuSolver 1.0:用C++实现的数独解题程序 【二】
  9. spring boot log4j2 最佳实践
  10. GAN实战笔记——第一章GAN简介