發表文章

.Net Core Web Api_筆記16_api結合ADO.NET資料庫操作part4_資料編輯提交更新

圖片
  編輯則是會By特定編號撈取指定資料去做編輯 所以需要先把資料回填到表單 修改Show.html 這裡改一下標題為操作(包含編輯,刪除兩種操作類型) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 56 57 58 59 60 61 62 <!DOCTYPE html> <html> <head> <meta charset= "utf-8" /> <title> Show NewsType </title> <link href= "../css/bootstrap.min.css" rel= "stylesheet" /> <script src= "../js/jquery/jquery.min.js" ></script> </head> <body style= "margin:20px;" > <table id= "tbNewsType" class= "table table-bordered" > <thead> <tr> <td> 文章ID </td> <td> 分類 </td> <td> 是否啟用 </td> <td> 操作 </td> </tr> </the...

.Net Core Web Api_筆記15_api結合ADO.NET資料庫操作part3_資料刪除

圖片
  我們在上一篇的Show.html 已經完成了資料查詢呈現 這裡要多出操作(比方像是編輯、刪除...) 先撰寫好刪除的action method 刪除很單純就是針對id作條件式指定的HttpDelete操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [HttpDelete("Delete")] public ActionResult< int > DeleteNewsType( int? id) { if (id == null ) { return NotFound (); } string strSQL = @"delete from NewsType where NewsTypeId=@Id" ; Hashtable htParms = new Hashtable(); htParms.Add( "@Id" , id); int RowCount = MSSQLHelper.ExecuteNonQuery(strSQL, htParms); return RowCount; } 在Show.html下我們可以擴充一個欄位呈現刪除的操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 56 57 58 59 60 61 <!DOCTYPE html> <html> <head> <meta charset= "utf-8" /> <title> Show NewsType </title> <link href= "../css/bootstrap.min.css" rel= "stylesheet...

.Net Core Web Api_筆記14_api結合ADO.NET資料庫操作part2_資料查詢呈現

圖片
  在上一篇辛辛苦苦地完成了專案前置準備 並寫好新增功能的api呼叫(透過POST方式) 現在資料庫中有存入一些剛存進來的內容 我想把他們查詢呈現在畫面上 此時改用GET方式 藉由ado.net查詢回來存入 List泛型 傳入自定義的model class  也就是每一筆都為NewsType.cs 這個DTO class 在NewsTypeController新增 一個Action Method命名為ShowNewsType 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [HttpGet("Show")] public ActionResult<List<NewsType>> ShowNewsType() { string strSQL = @"select * from NewsType" ; Hashtable htParms = new Hashtable(); SqlDataReader dataReader = MSSQLHelper.GetSqlDataReader(strSQL); if (!dataReader.HasRows) return NotFound (); List<NewsType> lsNewsType = new List<NewsType>(); while (dataReader.Read()) { lsNewsType.Add( new NewsType() { NewsTypeId = dataReader.GetInt32( 0 ), NewsTypeName = dataReader.GetString( 1 ), isEnabled = dataReader.GetBoolean( 2 ) }); } dataReader.Close(); return lsNewsType; } 在瀏覽器或PostMa...

.Net Core Web Api_筆記13_api結合ADO.NET資料庫操作part1_專案前置準備到資料新增

圖片
  專案前置準備 新建好資料庫以及資料表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 create table NewsType ( NewsTypeId int identity primary key , NewsTypeName nvarchar( 200 ), isEnabled bit ) create table NewsInfo ( NewsId int identity primary key , NewsTitle nvarchar( 300 ), NewsContent nvarchar( max ), CreateDate datetime, NewsTypeId int foreign key references NewsType(NewsTypeId) ) 建立並配置好visual studio .net core web api專案 配置好資料庫連線設定(位在appsetting.json中) 安裝 Microsoft.Extensions.Configuration.Json 我們會藉由它提供的api做組態檔案讀取跟設置 新增Models、Utility跟Services三目錄 在Services目錄新建一個Class 命名為 AppConfigurationService.cs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 using Microsoft.Extensions.Configuration ; using Microsoft.Extensions.Configuration.Json ; using System ; using System.Collections.Generic ; using System.Linq ; using System.Threading.Tasks ; namespace MyNet5ApiAdoTest.Services { public class AppConfigurationService { public static IConfiguratio...

Git取消已經被追蹤的檔案_Web.config

圖片
  每次都會不小心在commit時候把web.config  給commit跟push進來 當中包含了一些機密性資料 發信依賴的gmail帳號密碼跟DB連線字串帳密等等 真的還滿頭疼的>~< 後來都要頻繁去更新密碼 趕緊記錄起來不要再犯這類錯誤 開啟git bash 然後cd到專案目錄 可以直接到Web.config所在目錄 下這行指令使其不再被版控追蹤 git rm --cached web.config 記得把.config補加進.ignore Ref: .gitignore not ignoring web.config https://stackoverflow.com/questions/22038600/gitignore-not-ignoring-web-config How to change folder with git bash? https://stackoverflow.com/questions/8961334/how-to-change-folder-with-git-bash