close
The CREATE DATABASE statement is used to create a database.
範例: CREATE DATABASE car;
The CREATE TABLE statement is used to create a table in a database.
1: CREATE TABLE table_name
2: (
3: column_name1 data_type(size),
4: column_name2 data_type(size),
5: column_name3 data_type(size),
6: ....
7: );
1: USE [car]
2: GO
3:
4: SET ANSI_NULLS ON
5: GO
6:
7: SET QUOTED_IDENTIFIER ON
8: GO
9:
10: SET ANSI_PADDING ON
11: GO
12:
13: CREATE TABLE [dbo].[Photo](
14: [Id] [int] IDENTITY(1,1) NOT NULL,
15: [CameraId] [varchar](20) NOT NULL,
16: [CreateDate] [datetime] NOT NULL,
17: [CarId] [varchar](20) NOT NULL,
18: [Type] [varchar](1) NOT NULL,
19: [Src] [nvarchar](250) NOT NULL,
20: CONSTRAINT [PK_CarPhoto] PRIMARY KEY CLUSTERED
21: (
22: [Id] ASC
23: )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
24: ) ON [PRIMARY]
25:
26: GO
27:
28: SET ANSI_PADDING OFF
29: GO
寫入資料
下拉選單
1: Dictionary<string, string> types = new Dictionary<string, string>();
2: types.Add("", "全部");
3: types.Add("0", "0.正常");
4: types.Add("1", "1.遺失");
5: comboType.DataSource = new BindingSource(types, null);
6: comboType.DisplayMember = "Value";
7: comboType.ValueMember = "Key";
Datagrid 插入一行checkbox
利用DataGridViewCheckBoxColumn建立 CheckBox 欄
1: // 建立 CheckBox 欄
2: DataGridViewCheckBoxColumn cbCol = new DataGridViewCheckBoxColumn();
3: cbCol.Width = 50; //設定寬度
4: cbCol.HeaderText = " 全選";
5: cbCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //置中
6: dataGridView1.Columns.Insert(0, cbCol);
建立矩形,等下計算 CheckBox 嵌入 GridView 的位置
1: // 建立矩形,等下計算 CheckBox 嵌入 GridView 的位置
2: Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
3: rect.X = rect.Location.X + rect.Width / 4 - 9;
4: rect.Y = rect.Location.Y + (rect.Height / 2 - 9);
5:
6: // 創建checkbox,設定在剛才矩形位置(rect)
7: CheckBox cbHeader = new CheckBox();
8: cbHeader.Name = "checkboxHeader";
9: cbHeader.Size = new Size(18, 18);
10: cbHeader.Location = rect.Location;
全選事件
1: // 全選事件
2: cbHeader.CheckedChanged += new EventHandler((object sender, EventArgs e) => {
3: foreach (DataGridViewRow dr in dataGridView1.Rows)
4: dr.Cells[0].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked;
5: });
6:
7: //將 CheckBox 加入到 dataGridView
8: dataGridView1.Controls.Add(cbHeader);
9: dataGridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
1.設計Photo資料結構
1: namespace PhotoManage.Model
2: {
3: [Table("Photo")]
4: public class Photo
5: {
6: public int Id { get; set; }
7: [DisplayName("攝影機編")]
8: public string CameraId { get; set; }
9: [DisplayName("日期")]
10: public DateTime CreateDate { get; set; }
11: [DisplayName("車牌")]
12: public string CarId { get; set; }
13: [DisplayName("類型")]
14: public string Type { get; set; }
15: [DisplayName("圖片路徑")]
16: public string Src { get; set; }
17: }
18: }
2. 設計Filter查詢條件
1: public class FilterModel
2: {
3: public string CarId { get; set; }
4: public DateTime SDate { get; set; }
5: public DateTime EDate { get; set; }
6: public string Type { get; set; }
7: public int PageIndex { get; set; }
8: }
3. 查詢圖片GetPhotos
1: public List<Photo> GetPhotos(FilterModel filter)
2: {
3: using (var cn = new SqlConnection(dbContext))
4: {
5: return cn.Query<Photo>(@"
6: SELECT *
7: FROM dbo.Photo
8: WHERE CreateDate BETWEEN @SDate AND @EDate
9: AND (@CarId='' OR CarId LIKE @CarId)
10: AND (@Type='' OR Type=@Type)",
11: new
12: {
13: SDate = filter.SDate,
14: EDate = filter.EDate,
15: CarId = filter.CarId.Replace('*', '%'),
16: Type = filter.Type
17: }).ToList();
18: }
19: }
4. 輸入: 查詢條件 filter
1: FilterModel filter = new FilterModel
2: {
3: CarId = tbCarId.Text.Trim(),
4: Type = comboType.SelectedValue.ToString(),
5: SDate = Convert.ToDateTime(string.Format("{0} {1}", datePicker1.Value.ToString("yyyy-MM-dd"), timePicker1.Value.ToString("HH:mm"))),
6: EDate = Convert.ToDateTime(string.Format("{0} {1}", datePicker2.Value.ToString("yyyy-MM-dd"), timePicker2.Value.ToString("HH:mm"))),
7: PageIndex = pager.CurrentPage
8: };
5. 輸出: 查詢結果 List<Photo>
1: var photos = daoService.GetPhotos(filter);
6. 將查詢結果顯示在dataGridView
Skip: 略過序列中指定的項目數目
Take:從序列開頭傳回指定的連續項目數目
1: dataGridView1.DataSource = null;
2: dataGridView1.DataSource = photos.Skip((filter.PageIndex - 1) * 20).Take(20).ToList();
3: pager.SetRows(photos.Count());
7. 插入一行預覽連結
1: DataGridViewLinkColumn linkCol = new DataGridViewLinkColumn();
2: linkCol.HeaderText = "預覽";
3: linkCol.DataPropertyName = "Src";
4: linkCol.Name = "srcLink";
5: linkCol.Text = "預覽";
6: dataGridView1.Columns.Insert(5, linkCol);
7: ((DataGridViewLinkColumn)dataGridView1.Columns["srcLink"]).UseColumnTextForLinkValue = true;
1: ((DataGridViewLinkColumn)dataGridView1.Columns["srcLink"]).UseColumnTextForLinkValue = false;
8. 隱藏欄位
1: // 隱藏欄位
2: dataGridView1.Columns["Id"].Visible = false;
3: dataGridView1.Columns["Type"].Visible = false;
4: dataGridView1.Columns["Src"].Visible = false;
9. 設定唯讀欄位
1: // 設定唯讀欄位
2: for (int i = 1; i < dataGridView1.Columns.Count; i++)
3: {
4: dataGridView1.Columns[i].ReadOnly = true;
5: }
參考資料
全站熱搜
留言列表