閲覧総計:11928 (本日:1 昨日:0)
データベースにデータを新規登録した「作成日時」や、データの内容を編集更新した
「更新日時」を記録に残しておく事は、データ管理上とても有用である。
MS AccessをDBとし、GridViewでデータの「作成日時」と「更新日時」を自動で登録
する方法を確認したので記録に残しておく。 09/12/01
【稼働画面】
【方法】
「作成日時」の自動登録
<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>
「更新日時」の自動登録
<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の「編集」機能でデータの「更新」を行うと「更新日時」に現在の日時が
自動登録される。
【稼働画面】
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>
【参考にしたページ】
該当なし