標籤:主鍵 取資料 als pki fine 資料庫表 顯示 資料 model
bit值儲存為1/0,1代表true,0代表false
讀取資料庫資料時,可以直接用bool型讀取該欄位,會直接轉換為true/false
資料庫表結構
CREATE TABLE [dbo].[BitTable]( [PKID] [int] IDENTITY(1,1) NOT NULL, [IsDelete] [bit] NULL,PRIMARY KEY CLUSTERED ( [PKID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
實體類BitModel
public class BitModel{ public int PKID { get; set; } public bool IsDelete { get; set; }}
讀取資料
採用dapper,封裝方法
public class DbManager<T> where T : class{ private static DbManager<T> instance; private static object _lock = new object(); private SqlConnection connection; public static DbManager<T> Instance { get { lock (_lock) { if (instance == null) { instance = new DbManager<T>(); } } return instance; } } public DbManager() { connection = new SqlConnection("Server=;DataBase=;Uid=;pwd=;"); connection.Open(); } public IEnumerable<T> QueryBySQL(string sql) { return connection.Query<T>(sql); } public bool ExecuteOne(string sql) { if (connection.Execute(sql) != 0) return true; return false; }}
public ActionResult Bit(){ List<BitModel> list = new BLL.AboutDBManager().GetBitModel(); return View(list);}
顯示資料
<div> <table> <thead> <tr> <th>主鍵</th> <th>是否刪除</th> </tr> </thead> <tbody> @{ foreach(var item in Model) { <tr> <td>@item.PKID</td> <td>@item.IsDelete</td> </tr> } } </tbody> </table></div>
SQL Server bit資料類型