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>
来源: http://www.bubuko.com/infodetail-2733836.html