<h2>
景點清單查詢
</h2>
<h3>
程式要求:
</h3>
A: 點選工具列的「匯入」按鈕,會跳出開啟檔案對話方塊,請設定只能開啟副檔名為 JSON
檔案。
B: 將 JSON 文字檔透過程式定義的 Deserialize 函式,指定轉成 Travel 物件集合。
C: 新增一個 ADO.NET 實體資料模型,建立 visit.mdf 資料庫的【景點】資料表。
D: 將 Travel 物件集合使用自行建立的 Model 加到景點物件,回存到資料庫完成存檔後顯示匯
入筆數訊息,並將資料進行載入,自動產生所有景點的表格欄位。
E: 點選工具列的「查詢」按鈕,使用 BindingSource 的條件篩選,進行所有欄位關鍵字資料的
查詢,並顯示篩選結果於 DataGridView 控制項中。
F: 按下「取消查詢」按鈕,清除 BindingSource 條件篩選,以及所輸入的關鍵字,重新載入並
顯示所有資料。
<h3>
介面設計:
</h3>
使用bindingNavigator當作上排工具列
使用dataGridView顯示資料行

<h2>
我的作法:
</h2>
<h3>
匯入功能
</h3>
加入Newtonsoft.Json套件,並使用它
```csharp=
using Newtonsoft.Json;
```
創建一個資料庫並建立一個資料表
路徑為:專案>加入新項目>C#>資料>服務架構資料庫
建立好之後,創建一個資料表
再創建一個ADO.NET實體資料模型
路徑為:專案>加入新項目>C#>資料>ADO.NET實體資料模型
這檢查用戶是否選擇了文件並在文件對話框中單擊了“確定”按鈕
```csharp=
openFileDialog1.ShowDialog() == DialogResult.OK:
```
如果用戶選擇了文件,這一行獲取所選文件的路徑
```csharp=
string jsonFile = openFileDialog1.FileName;
```
初始化一個新的Table對象列表
```csharp=
List<Table> travelist = new List<Table>();
```
讀取所選JSON文件的全部內容並將其存儲為字符串
```csharp=
string json_txt = File.ReadAllText(jsonFile);
```
使用System.Text.Json將JSON字符串反序列化為Table類型的對象列表
```csharp=
var jsonfile = JsonSerializer.Deserialize<List<booking>>(file);
```
幫每個對象分配一個新id值
```csharp=
int i = 0;
foreach (Table t in list)
{
t.Id = i;
db1.Table.Add(t);
i++;
}
```
然後將變更儲存在資料庫
```csharp=
db1.SaveChanges();
```
創建一個DataTable顯示資料
```csharp=
DataTable dataTable = new DataTable();
```
添加datatable資料列
```csharp=
dataTable.Columns.Add("name");
dataTable.Columns.Add("about");
dataTable.Columns.Add("address");
dataTable.Columns.Add("traffic");
dataTable.Columns.Add("time");
```
將資料庫中的table的物件屬性加入到database中
```csharp=
foreach (var t in db1.Table)
{
dataTable.Rows.Add(t.name, t.about, t.address, t.traffic, t.time);
}
```
將 DataTable 指定為 BindingSource 的資料來源
```csharp=
bindingSource1.DataSource = dataTable;
```
計算總共匯入幾筆資料
```csharp=
int irc = db1.Table.Count();
```
messageBox顯示匯入幾筆資料,並讓使用者按下ok鍵
```csharp=
MessageBox.Show($"匯入 {irc} 筆資料", "匯入成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
```
否則顯示No folder selected
```csharp=
MessageBox.Show("No folder selected");
```
<h3>
匯入功能程式碼
</h3>
```csharp=
private void toolStripLabel1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string jsonFile = openFileDialog1.FileName;
List<Table> travelist = new List<Table>();
string json_txt = File.ReadAllText(jsonFile);
List<Table> list = JsonConvert.DeserializeObject<List<Table>>(json_txt);
int i = 0;
foreach (Table t in list)
{
t.Id = i;
db1.Table.Add(t);
i++;
}
db1.SaveChanges();
DataTable dataTable = new DataTable();
dataTable.Columns.Add("name");
dataTable.Columns.Add("about");
dataTable.Columns.Add("address");
dataTable.Columns.Add("traffic");
dataTable.Columns.Add("time");
foreach (var t in db1.Table)
{
dataTable.Rows.Add(t.name, t.about, t.address, t.traffic, t.time);
}
bindingSource1.DataSource = dataTable;
int irc = db1.Table.Count();
MessageBox.Show($"匯入 {irc} 筆資料", "匯入成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("No folder selected");
}
}
```
<h2>
查詢&取消查詢
</h2>
<h3>
查詢功能
</h3>
透過bindingSource1的Filter屬性,使用SQL模式的LIKE運算子,使用包含在toolStripTextBox1 中的文字作為搜尋條件篩選資料
```csharp=
private void toolStripLabel3_Click(object sender, EventArgs e)
{
bindingSource1.Filter = $"name LIKE'%{toolStripTextBox1.Text}%' OR about LIKE'%{toolStripTextBox1.Text}%' OR address LIKE'%{toolStripTextBox1.Text}%' OR traffic LIKE'%{toolStripTextBox1.Text}%' OR time LIKE'%{toolStripTextBox1.Text}%'";
}
```
<h3>
取消查詢
</h3>
取消toolStripTextBox1的搜尋文字 並取消bindingource1的搜尋
```csharp=
private void toolStripLabel4_Click(object sender, EventArgs e)
{
toolStripTextBox1.Clear();
bindingSource1.RemoveFilter();
}
```