- 在asp.net建立資料庫
專案右鍵 -> 加入 -> 加入新項目 -> SQL Server資料庫 - Content SSMS
工具 -> 選項 -> 資料庫工具/資料連接 -> 複製 -> copy到SSMS - 匯入txt
SSMS -> 先建立表格 -> 複製貼上 - 匯入csv
SSMS -> 資料庫名稱右鍵 -> 工作 -> 匯入資料 -> Data source:Flat File Source -> Browse you csv file ->
next -> Destination: SQL Server Native Client -> next -> change Table name -> 編輯對應 ->
更改資料類型(中文: nvarchar) -> next -> finish - 直接拖拉資料表
- 利用網址丟資料
設定資料來源 -> 下一步 -> 指定資料表或檢視的資料行 -> 可選擇欄位 -> WHERE條件 ->
資料行:通訊地址(欄位) 運算子:LIKE 來源:QueryString(從網址列丟資料) Form(用表單丟資料) Control(用控制項丟資料),QueryString欄位:city -> 加入 -> 確定 -> 下一步 -> 測試查詢 -> 高雄市 -> 查詢 -> 完成 -> 網址列: ?city=台北 - 如果看到目錄有app_offline.htm要刪除
- 利用DropDownList丟資料
加入DropDownList,啟用AutoPostBack -> 選擇資料來源 -> 新資料來源 -> SQL資料庫 -> 確定 -> 現有連接字串 -> 下一步 -> 指定自訂SQL陳述式或預存程序 - 下一步 -> SQL陳述式: SELECT DISTINCT LEFT([通訊地址],3) AS city FROM [addresses] -> 完成 -> 針對GridView設定資料來源 -> 更改參數來源: Control, Control ID:DropDownList1, DefaultValue:% -> 下一步 -> 完成 - 利用RadioButtonList丟資料
加入RadioButtonList,啟用AutoPostBack -> 選擇資料來源 -> 新資料來源 -> SQL資料庫 -> 確定 -> 現有連接字串 -> 下一步 -> 指定資料表或檢視的資料行: 性別,傳回不重複的資料列(DISTINCT) - 下一步 -> 完成 -> 針對GridView設定資料來源 -> 更改SQL陳述式: AND [性別] = @性別 -> 下一步 -> 性別: 參數來源: Control, Control ID:RadioButtonList1, DefaultValue:% -> 完成 - 匯入Excel File
SSMS -> 資料庫名稱右鍵 -> 工作 -> 匯入資料 -> Data source:Excel -> Browse you Excel file -> next -> Destination: SQL Server Native Client -> next -> change Table name -> 編輯對應 ->
更改資料類型(中文: nvarchar), Edit SQL:可加入primary key、[id] int not null primary key identity 流水號-> next -> finish - 需加入主鍵,GridView才會有編輯與刪除
- 編輯:在GridView增加UPDATE語法
update [addresses] set [性別]=@性別, [電話]=@電話, [手機]=@手機, [郵遞 區號]=@郵遞_區號, [通訊地址]=@通訊地址, [email]=@email, [緊急聯絡人 姓名]=@緊急聯絡人_姓名, [ 緊急聯絡人 手機] =@column1, [備註]=@備註 where [sno]=@sno - 新增:在GridView增加INSERT語法
insert into [addresses] values(@性別, @電話, @手機, @郵遞_區號, @通訊地址, @email, @緊急聯絡人_姓名, @column1, @備註) where [sno]=@sno - 刪除:在GridView增加DELETE語法
delete from [addresses] where [sno]=@sno - 在GridView新增主鍵
在GridView的aspx裡輸入DateKeyName="sno" - 在資料繫結後的程序
RadioButtonList1.Items.Insert(0, new ListItem("All genders", "%"));
RadioButtonList1.SelectedIndex = 0;
DropDownList1.Items.Insert(0, new ListItem("All cites", "%"));/li> - 性別需更改成LIKE
- GridView一頁有幾筆資料:PageSize
- 加入總共有幾筆資料
DataView dv = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
Literal1.Text = "符合上面條件的資料共有: " + dv.Count + " 筆."; - TemplateField:可達到制式化、提示
aspx會有EditItemTemplate(編輯時呈現)、ItemTemplate(一般資料瀏覽呈現)、BoundField
在刪除物件底下加入OnClientClick="return confirm('Are you sure?')" - GridView頁數位置
PagerStyle -> HorizontalAlign - 單字查詢
選取GridView -> 加入新資料行 -> 欄位類型:HyperLinkField,標題文字:look_up,指定文字:yahoo,從資料欄位取得URL:word,URL格式字串:https://tw.dictionary.search.yahoo.com/search?p={0} - 下一頁的時候讓網頁自動跑到現在的位置
aspx最上面加入:MaintainScrollPositionOnPostback ="true" - 撲克牌產生
protected void Button1_Click(object sender, EventArgs e)
{
    string[] shapes = { "s", "h", "d", "c" };
    string[] points = { "a", "2", "3", "4", "5", "6", "7", "8", "9", "t", "j", "q", "k" };
    string sql = "insert into [playcards]([shape], [point]) values ";
    for (int s = 0; s < shapes.Length; s++)
    {
         for (int p = 0; p < points.Length; p++)
        {
             string rec;
             if (s == shapes.Length - 1 && p == points.Length - 1)
             {
                 rec = string.Format($"('{shapes[s]}', '{points[p]}')");
             }
             else
             {
                 rec = string.Format($"('{shapes[s]}', '{points[p]}'), ");
             }
             sql += rec;
        }
     }
    try
    {
        SqlDataSource1.InsertCommand = sql;
        SqlDataSource1.Insert();
    }
     catch(Exception ex)
     {
         Response.Output.Write($"Error:{ex.Message}{sql}" );
    }
} - 加入撲克牌圖片
GridView -> 加入新資料行 -> 欄位類型:、BoundField,標題文字:picture,資料欄位:point,打勾唯讀 -> 編輯資料行 -> picture -> 轉成TemplateField -> 確定 -> 進入aspx -> ItemTemplate改成< asp:Image ID="Label1" runat="server" ImageUrl='<%# "~/poker/"+Eval("point")+Eval("shape")+".gif" %>'> - 更改圖片大小GridView -> 編輯樣板 -> 點選圖片 -> 屬性 -> 修改Width
- 新增刪除按鈕
使用者提示: OnClientClick: return confirm('This will delete all the data. Are you sure?')
SqlDataSource1.DeleteCommand = "DELETE FROM [playcards]";
SqlDataSource1.Delete(); - 修改欄位格式(四捨五入)GridView -> 編輯資料行 -> 選取欄位 -> 屬性 -> DataFormatString:{0:F2} -> 兩位小數
- 建立中文英文資料表
CREATE TABLE [chinese]
(
[id] int not null primary key identity,
[chinese] nvarchar(500) not null,
[english] varchar(500) not null
) - 建立兩個輸入欄位及按鈕
< input type ="text" id="chinese" runat="server" required="required" />
< input type ="text" id="english" runat="server" required="required" /> < asp:Button ID ="Button1" runat="server" Text ="Insert" OnClick="Button1_Click" /> - 利用兩個textBox跟button輸入資料到Table
string ch = chinese.Value;
string en = english.Value;
string sql = $"INSERT INTO [chinese] VALUES (N'{ch}', '{en}')";
try
{
    SqlDataSource1.InsertCommand = sql;
    SqlDataSource1.Insert();
}
catch (Exception ex)
{
    Response.Output.Write($"error:{ex.Message}, sql={sql}");
}
chinese.Value = "";
english.Value = "";
chinese.Focus();
- 讓GridView編輯刪除不被擠壓
GridView -> 編輯資料行 -> TemplateField or CommandField -> ItemStyle -> Wrap: False - DetailsView
選擇資料來源 -> SQL資料庫 -> 下一步 -> 對應資料表(keywords) -> 選擇欄位 -> 下一步 -> WHERE條件 -> id = Control,GridView2 -> 加入 -> 下一步 -> 完成
加入新增刪除:設定資料來源 -> 下一步 -> 進階 -> 產生INSERT...勾選 -> 完成 - 設定欄位寬度
FieldHeaderStyle -> Wrap:False,Width: 120px - 增加選取資料行
加入新資料行 -> CommandField,選取打勾 - 再刪除時新增確認按鈕
- 在控制向最前面加入全部選項並設定選擇在哪個地方
RadioButtonList1.Items.Insert(0, new ListItem("All genders", "%"));
RadioButtonList1.SelectedIndex = 0;
DropDownList1.Items.Insert(0, new ListItem("All cites", "%"));
- 刪除資料表
SqlDataSource1.DeleteCommand = "DELETE FROM [playcards]";
SqlDataSource1.Delete(); - Datagridview日期只顯示天
編輯資料行 -> 選擇該欄位 -> DataFormatString:{0:d} - input date
< input id="date1" type="date" required="required" runat="server"/> - 將input date 轉換為 DateTime
DateTime d1 = Convert.ToDateTime(Request["date1"]);
DateTime d2 = Convert.ToDateTime(Request["date2"]);
Literal1.Text = string.Format($"介於< span style = 'color:blue'>{d1:yyyy/MM/dd}< /span>與< span style = 'color:blue'>{d2:yyyy/MM/dd}< /span>間報修的有:"); - MultiView切換畫面
< asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0" >
        < asp:View ID="View1" runat="server">< /asp:View>
        < asp:View ID="View2" runat="server">< /asp:View>
< /asp:MultiView> - 建立控制項發生錯誤
原始碼date -> text - 寫入資料
protected void Button3_Click(object sender, EventArgs e)
{
     DataView dv = (DataView)SqlDataSource2.Select(new DataSourceSelectArguments());
     DateTime dateTime = DateTime.UtcNow.Date;
     string today = dateTime.ToString("yyyy/M/d");
     string updatesql = $"INSERT INTO itemfix (who, item, report_date) VALUES (N'{input_who.Value}', '{DropDownList1.SelectedValue}', '{today}');";
     try
     {
          SqlDataSource2.UpdateCommand = updatesql;
          SqlDataSource2.Update();
     }
     catch (Exception ex)
      {
          Response.Output.Write($"error when updating scores: {ex.Message}");
      }
} - 在資料行中加入內建超連結
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.DataItem == null) return;
     Label lbl = (Label)e.Row.FindControl("lblitem");
     if (lbl != null)
          lbl.Text = "< a href='http://www.google.com.tw/search?q=" + lbl.Text + "'>" + lbl.Text + "< /a>";
} - week 13 新增資料到資料表 How to update the values of a columun in a datatable?
- week 13 Datagridview 用勾選方式選取單一及全部
- week 13 如果更改資料表欄位資料格式,在Datasource需更改資料型態
- week 13 如何利用選取功能選取全部或單一
- week 14 日期篩選Microsoft.ACE.OLEDB.12.0 提供者未登入於本機電腦上
- week 14 日期篩選Datagridview 日期格式設定 只設定天數或月數加天數
- week 14 日期篩選Datagridview 日期格式設定 在編輯欄位只顯示日期
- week 14 日期篩選加入html input data