#freeze 閲覧総計:&counter(); (本日:&counter(today); 昨日:&counter(yesterday);) データベースにデータを新規登録した「作成日時」や、データの内容を編集更新した 「更新日時」を記録に残しておく事は、データ管理上とても有用である。 MS AccessをDBとし、GridViewでデータの「作成日時」と「更新日時」を自動で登録 する方法を確認したので記録に残しておく。 09/12/01 【稼働画面】 #ref(AutoDate01.JPG) 【方法】 「作成日時」の自動登録 <script runat="server"> Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) If e.CommandName = "Insert" Then AccessDataSource1.InsertParameters.Clear() For Each key As String In Request.Form.AllKeys If key.Contains("$TextBox2") Then AccessDataSource1.InsertParameters.Add(New ControlParameter("名前", TypeCode.String, key, "Text")) End If Next AccessDataSource1.Insert() End If End Sub </script> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\MyDB.mdb" InsertCommand="INSERT INTO `名簿2` (`名前`, `作成日時`, `更新日時`) VALUES (?, Now(), Now())" <InsertParameters> <asp:Parameter Name="名前" Type="String" /> <asp:Parameter Name="作成日時" Type="DateTime" /> <asp:Parameter Name="更新日時" Type="DateTime" /> </InsertParameters> </asp:AccessDataSource> 「更新日時」の自動登録 #ref(AutoDate02.JPG) <asp:TemplateField HeaderText="更新日時" SortExpression="更新日時"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("更新日時", "{0: yy/MM/dd HH:mm}") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\MyDB.mdb" UpdateCommand="UPDATE `名簿2` SET `名前` = ?, `作成日時` = ?, `更新日時` = Now() WHERE `ID` = ?"> <UpdateParameters> <asp:Parameter Name="名前" Type="String" /> <asp:Parameter Name="作成日時" Type="DateTime" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> </asp:AccessDataSource> 【機能】データの新規挿入を行うと「作成日時」と「更新日時」に現在の日時が自動 登録される。 GridViewの「編集」機能でデータの「更新」を行うと「更新日時」に現在の日時が 自動登録される。 【稼働画面】 #ref(AutoDate01.JPG) Accessデータベース名:MyDB.mdb テーブル名:名簿2 |~フィールド名|~データ型|~フィールドサイズ| |ID (主キー)|オートナンバー型|長整数型| |名前|テキスト型|50| |作成日時|日付/時刻型|| |更新日時|日付/時刻型|| ※VWD2005、ASP.NET2.0ではAccess2007のDBファイル形式(*.accdb)は利用できない。 DBファイルは「Acces2002-2003形式(*.mdb)」で保存してASP.NETで利用する。 開発環境:VWD2005 + Access2003 サーバ:ASP.NET2.0 + Access2003 【AutoDate01.aspx】 <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) If e.CommandName = "Insert" Then AccessDataSource1.InsertParameters.Clear() For Each key As String In Request.Form.AllKeys If key.Contains("$TextBox2") Then AccessDataSource1.InsertParameters.Add(New ControlParameter("名前", TypeCode.String, key, "Text")) End If Next AccessDataSource1.Insert() End If End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>データの作成日時、更新日時を自動登録する</title> </head> <body> <form id="form1" runat="server"> <div> データの作成日時、更新日時を自動登録する 09/11/30<br /> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="AccessDataSource1" EmptyDataText="表示するデータ レコードがありません。" ShowFooter="True" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="更新" /> <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="キャンセル" /> </EditItemTemplate> <FooterTemplate> <asp:Button ID="Button3" runat="server" CommandName="Insert" Text="挿入" /> </FooterTemplate> <ItemTemplate> <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="編集" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" /> <asp:TemplateField HeaderText="名前" SortExpression="名前"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("名前") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="TextBox2" runat="server" Width="60px"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("名前") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="作成日時" DataFormatString="{0: yy/MM/dd HH:mm}" HeaderText="作成日時" SortExpression="作成日時" /> <asp:TemplateField HeaderText="更新日時" SortExpression="更新日時"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("更新日時", "{0: yy/MM/dd HH:mm}") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\MyDB.mdb" DeleteCommand="DELETE FROM `名簿2` WHERE `ID` = ?" InsertCommand="INSERT INTO `名簿2` (`名前`, `作成日時`, `更新日時`) VALUES (?, Now(), Now())" SelectCommand="SELECT `ID`, `名前`, `作成日時`, `更新日時` FROM `名簿2`" UpdateCommand="UPDATE `名簿2` SET `名前` = ?, `作成日時` = ?, `更新日時` = Now() WHERE `ID` = ?"> <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="名前" Type="String" /> <asp:Parameter Name="作成日時" Type="DateTime" /> <asp:Parameter Name="更新日時" Type="DateTime" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="名前" Type="String" /> <asp:Parameter Name="作成日時" Type="DateTime" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> </asp:AccessDataSource> <br /> </div> </form> </body> </html> 【参考にしたページ】 該当なし - 1 -- [[1]] &new{2012-01-25 (水) 13:33:54}; - 1 -- [[1]] &new{2012-01-25 (水) 13:33:56}; - 1 -- [[-1']] &new{2012-01-25 (水) 13:33:58}; -- 1' -- [[1]] &new{2012-01-25 (水) 13:33:59}; - 1 -- [[1]] &new{2012-01-25 (水) 13:34:00}; - 1 -- [[1]] &new{2012-02-20 (月) 08:44:07}; - 1 -- [[1]] &new{2012-02-20 (月) 08:44:09}; - 1 -- [[-1']] &new{2012-02-20 (月) 08:44:10}; -- 1' -- [[1]] &new{2012-02-20 (月) 08:44:12}; - 1 -- [[1]] &new{2012-02-20 (月) 08:44:13}; - 1 -- [[1]] &new{2012-03-12 (月) 18:39:27}; - 1 -- [[1]] &new{2012-03-12 (月) 18:39:30}; - 1 -- [[-1']] &new{2012-03-12 (月) 18:39:31}; -- 1' -- [[1]] &new{2012-03-12 (月) 18:39:32}; - 1 -- [[1]] &new{2012-03-12 (月) 18:39:33}; - 1 -- [[1]] &new{2012-03-13 (火) 15:18:07}; - 1 -- [[1]] &new{2012-03-13 (火) 15:18:10}; - 1 -- [[-1']] &new{2012-03-13 (火) 15:18:11}; -- 1' -- [[1]] &new{2012-03-13 (火) 15:18:12}; - 1 -- [[1]] &new{2012-03-13 (火) 15:18:13}; - 1 -- [[1]] &new{2012-03-28 (水) 21:39:41}; - 1 -- [[1]] &new{2012-03-28 (水) 21:39:45}; - 1 -- [[-1']] &new{2012-03-28 (水) 21:39:46}; -- 1' -- [[1]] &new{2012-03-28 (水) 21:39:47}; - 1 -- [[1]] &new{2012-03-28 (水) 21:39:48}; - fdさd -- [[gdさ]] &new{2012-04-19 (木) 16:21:08}; - 1 -- [[1]] &new{2012-05-10 (木) 02:17:06}; - 1 -- [[1]] &new{2012-05-10 (木) 02:17:09}; - 1 -- [[-1']] &new{2012-05-10 (木) 02:17:10}; -- 1' -- [[1]] &new{2012-05-10 (木) 02:17:11}; - 1 -- [[1]] &new{2012-05-10 (木) 02:17:12}; - 1 -- [[1]] &new{2012-05-16 (水) 09:22:02}; - 1 -- [[1]] &new{2012-05-16 (水) 09:22:06}; - 1 -- [[-1']] &new{2012-05-16 (水) 09:22:07}; -- 1' -- [[1]] &new{2012-05-16 (水) 09:22:08}; - 1 -- [[1]] &new{2012-05-16 (水) 09:22:09}; - 1 -- [[1]] &new{2012-05-17 (木) 06:51:25}; - 1 -- [[1]] &new{2012-05-17 (木) 06:51:30}; - 1 -- [[-1']] &new{2012-05-17 (木) 06:51:31}; -- 1' -- [[1]] &new{2012-05-17 (木) 06:51:32}; - 1 -- [[1]] &new{2012-05-17 (木) 06:51:34}; - 1 -- [[1]] &new{2012-05-19 (土) 22:00:35}; - 1 -- [[1]] &new{2012-05-19 (土) 22:00:38}; - 1 -- [[-1']] &new{2012-05-19 (土) 22:00:40}; -- 1' -- [[1]] &new{2012-05-19 (土) 22:00:41}; - 1 -- [[1]] &new{2012-05-19 (土) 22:00:42}; #comment_nospam - 1 -- [[1]] &new{2012-05-19 (土) 22:00:37}; - 1 -- [[1]] &new{2012-05-17 (木) 06:51:29}; - 1 -- [[1]] &new{2012-05-16 (水) 09:22:04}; - 1 -- [[1]] &new{2012-05-10 (木) 02:17:08}; - 1 -- [[1]] &new{2012-03-28 (水) 21:39:43}; - 1 -- [[1]] &new{2012-03-13 (火) 15:18:09}; - 1 -- [[1]] &new{2012-03-12 (月) 18:39:29}; - 1 -- [[1]] &new{2012-02-20 (月) 08:44:08}; - 1 -- [[1]] &new{2012-01-25 (水) 13:33:55}; #vote(参考になった[4],ふつう[0],参考にならなかった[0])