日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

webapi+EF(增删改查)

發布時間:2024/4/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 webapi+EF(增删改查) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一步,Model建立Ado.net實體模型。

第二部,Controller建立增刪查改方法

??????? public static HttpResponseMessage toJson(Object obj)
??????? {
??????????? String str;
??????????? if (obj is String || obj is Char)
??????????? {
??????????????? str = obj.ToString();
??????????? }
??????????? else
??????????? {
??????????????? JavaScriptSerializer serializer = new JavaScriptSerializer();
??????????????? str = serializer.Serialize(obj);
??????????? }
??????????? HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"),?? "application/json")? };
??????????? return result;
??????? }


??????? public HttpResponseMessage get()
??????? {

??????????? var jg = db.user.ToList();
??????????? return toJson(jg);

??????? }

?

????? protected override void Dispose(bool disposing)
??????? {
??????????? db.Dispose();
??????????? base.Dispose(disposing);
??????? }

連接數據庫:vs界面左側服務器資源管理器中連接數據庫,在web.config中修改或添加

? <connectionStrings>
??? <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-webapitest1-20170420100509;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-webapitest1-20170420100509.mdf" />
??? <add name="testDBEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=testDB;persist security info=True;user id=sa;password=123456; MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
? </connectionStrings>

第三部:view視圖頁面 Index.cshtml

@{
??? Layout = null;
}
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<table id="customerTable" border="0" cellpadding="3">
??? <tr>
??????? <th>UserID</th>
??????? <th>login</th>
??????? <th>pwd</th>
??????? <th>Actions</th>
??? </tr>
??? <tr>
??????? <td>
??????????? <input type="text" id="txtCustomerId" size="5" />
??????? </td>
??????? <td>
??????????? <input type="text" id="txtCompanyName" />
??????? </td>
??????? <td>
??????????? <input type="text" id="txtContactName" />
??????? </td>
??????? <td>
??????????? <input type="button" name="btnInsert" value="Insert" />
??????? </td>
??? </tr>
</table>
<script type="text/javascript">
??? $(function () {
??????? $.getJSON("api/user", LoadCustomers);
??? });
??? function LoadCustomers(data) {
??????? $("#customerTable").find("tr:gt(1)").remove();
??????? $.each(data, function (key, val) {
??????????? var tableRow = '<tr>' + '<td>' + val.userId + '</td>' +
??????????? '<td><input type="text" value="' + val.login + '" /></td>' +
??????????? '<td><input type="text" value="' + val.pwd + '" /></td>' +
??????????? '<td><input type="button" name="btnUpdate" value="修改" />' +
??????????? '<input type="button" name="btnDelete" value="刪除" /></td>' +
??????????? '</tr>';
??????????? $('#customerTable').append(tableRow);
??????? });
??????? $("input[name='btnInsert']").click(OnInsert);
??????? $("input[name='btnUpdate']").click(OnUpdate);
??????? $("input[name='btnDelete']").click(OnDelete);
??? }
??? function OnInsert() {
??????? var userId = $("#txtCustomerId").val();
??????? var login = $("#txtCompanyName").val();
??????? var pwd = $("#txtContactName").val();
??????? var data = '{"userId":"' + userId + '","login":"' + login + '","pwd":"' + pwd + '"}}';
??????? $.ajax({
??????????? type: 'POST',
??????????? url: '/api/user',
??????????? data: data,
??????????? contentType: "application/json; charset=utf-8",
??????????? dataType: 'json',
??????????? success: function (result) {
??????????????? $("#txtCustomerId").val('');
??????????????? $("#txtCompanyName").val('');
??????????????? $("#txtContactName").val('');
??????????????? $("#txtCountry").val('');
??????????????? $.getJSON("api/user", LoadCustomers);
??????????????? alert('Customer Added !');
??????????? }
??????? }).fail(
??? function (xhr, textStatus, err) {
??????? alert('添加失敗,原因如下: ' + err);
??? });

??? }
??? function OnUpdate() {

??????? var userId = $(this).parent().parent().children().get(0).innerHTML;;

??????? var login = $($(this).parent().parent().children().get(1)).find("input").val();

??????? var pwd = $($(this).parent().parent().children().get(2)).find("input").val();;


??????? var data = '{"userId":"' + userId + '","login":"' + login + '","pwd":"' + pwd + '"}}';

??????? $.ajax({
??????????? type: 'PUT',
??????????? url: '/api/user/' + userId,
??????????? data: data,
??????????? contentType: "application/json; charset=utf-8",
??????????? dataType: 'json',
??????????? success: function (results) {
??????????????? $.getJSON("api/user", LoadCustomers);
??????????????? alert('Customer Updated !');
??????????? }
??????? }).fail(function (xhr, textStatus, err) {
??????????? alert('Failed update! The reason is : ' + err);
??????? });
??? }
?
??? function OnDelete() {
??????? var userId = $(this).parent().parent().children().get(0).innerHTML;
??????? $.ajax({
??????????? type: 'DELETE',
??????????? url: '/api/user/' + userId,
??????????? contentType: "application/json; charset=utf-8",
??????????? dataType: 'json',
??????????? success: function (results) {
??????????????? $.getJSON("api/user", LoadCustomers);
??????????????? alert('Customer Deleted!');
??????????? }
??????? }).fail(function (xhr, textStatus, err) {
??????????? alert("Delete error ! The reason is :" + err);
??????? });

??? }
</script>

轉載于:https://www.cnblogs.com/suan1717/p/6747134.html

總結

以上是生活随笔為你收集整理的webapi+EF(增删改查)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。