ObjectDataSource.UpdateMethod プロパティ
アセンブリ: System.Web (system.web.dll 内)

Dim instance As ObjectDataSource Dim value As String value = instance.UpdateMethod instance.UpdateMethod = value
/** @property */ public String get_UpdateMethod () /** @property */ public void set_UpdateMethod (String value)
ObjectDataSource がデータを更新するために使用するメソッドまたは関数の名前を表す文字列。既定値は空の文字列です。

ObjectDataSource コントロールは、UpdateMethod プロパティで識別されるメソッドが、バッチではなく、一度に 1 つずつ更新を実行することを前提としています。
UpdateMethod プロパティは、ObjectDataSource コントロールに関連付けられた ObjectDataSourceView オブジェクトの UpdateMethod プロパティに処理を代行させます。
オブジェクトの有効期間UpdateMethod プロパティで識別されるメソッドには、インスタンス メソッドまたは static (Visual Basic の場合は Shared) メソッドを指定できます。インスタンス メソッドの場合、UpdateMethod プロパティで指定されたメソッドが呼び出されるたびにビジネス オブジェクトが作成され、破棄されます。ObjectCreated イベントと ObjectCreating イベントを処理することで、UpdateMethod プロパティで指定されたメソッドが呼び出される前に、ビジネス オブジェクトを使用できます。UpdateMethod プロパティで指定されたメソッドが呼び出された後に発生する ObjectDisposing イベントを処理することもできます。ビジネス オブジェクトが IDisposable インターフェイスを実装している場合は、オブジェクトが破棄される前に Dispose メソッドが呼び出されます。メソッドが static (Visual Basic では Shared) の場合、ビジネス オブジェクトは作成されず、ObjectCreated、ObjectCreating、および ObjectDisposing の各イベントを処理できません。
パラメータは、次の 3 つのソースから DeleteParameters コレクションに追加されます。
まず、データ バインド コントロールから生成されたパラメータが UpdateParameters コレクションに追加されます。たとえば、ObjectDataSource コントロールが Name 列と Number 列を持つ GridView コントロールにバインドされている場合、Name および Number に対応するパラメータがコレクションに追加されます。パラメータの正確な名前は、OldValuesParameterFormatString プロパティによって決まります。これらのパラメータのデータ型は string です。次に、UpdateParameters 要素にリストされたパラメータが追加されます。UpdateParameters 要素のパラメータが UpdateParameters コレクション内の既存のパラメータと同じ名前で存在する場合、UpdateParameters 要素で指定されたパラメータと一致するように既存のパラメータが変更されます。通常、これはパラメータのデータの型を変更するために使用されます。最後に、Updating イベントでプログラムによってパラメータを追加および削除できます。このイベントは、Update メソッドが実行される前に発生します。メソッドは、パラメータがマージされた後に解決されます。メソッドの解決については、次のセクションで説明します。
Update メソッドが呼び出されると、データ バインド コントロールのデータ フィールド、UpdateParameters 要素で宣言によって作成されたパラメータ、および Updating イベント ハンドラで追加されたパラメータがすべてマージされます (詳細については、前のセクションを参照してください)。次に、ObjectDataSource コントロールは、呼び出すメソッドの検索を試みます。まず、UpdateMethod プロパティで指定された名前を持つ 1 つ以上のメソッドが検索されます。一致するメソッドが見つからない場合、InvalidOperationException 例外がスローされます。一致するメソッドが見つかった場合は、一致するパラメータ名が検索されます。たとえば、TypeName プロパティで指定された型に UpdateARecord という名前のメソッドが 2 つあるとします。一方の UpdateARecord には、ID というパラメータが 1 つあり、もう一方の UpdateARecord には、Name と Number という 2 つのパラメータがあります。UpdateParameters コレクションに ID という名前のパラメータが 1 つしかない場合、ID パラメータだけを持つ UpdateARecord メソッドが呼び出されます。メソッドの解決時に、パラメータの型はチェックされません。パラメータの順序は重要ではありません。
DataObjectTypeName プロパティが設定されている場合には、メソッドは別の方法で解決されます。ObjectDataSource は、DataObjectTypeName プロパティで指定された型のパラメータを 1 つ受け取る UpdateMethod プロパティで指定された名前のメソッドを検索します。この場合、パラメータの名前は重要ではありません。

このセクションには、2 つのコード例が含まれています。1 つ目のコード例では、DropDownList コントロール、TextBox コントロール、および複数の ObjectDataSource オブジェクトを使用してデータを更新する方法を示します。2 つ目のコード例では、1 つ目のコード例で使用されている EmployeeLogic クラスを示します。
DropDownList コントロール、TextBox コントロール、および複数の ObjectDataSource コントロールを使用してデータを更新する方法を次のコード例に示します。TextBox コントロールがアドレス情報の入力と更新に使用されるのに対し、DropDownList は、Northwind の従業員の名前を表示します。UpdateParameters コレクションには、DropDownList で選択された値にバインドされた ControlParameter オブジェクトが格納されているため、Update 操作を発生させるボタンは、従業員を選択するまで有効になりません。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %> <%@ Page language="vb" %> <%@ Import namespace="Samples.AspNet.VB" %> <Script runat="server"> ' Add parameters and initialize the user interface ' only if an employee is selected. Private Sub Page_Load(sender As Object, e As EventArgs) ' Be sure the text boxes are initialized with ' data from the currently selected employee. Dim selectedEmployee As NorthwindEmployee selectedEmployee = EmployeeLogic.GetEmployee(DropDownList1.SelectedValue) If Not selectedEmployee Is Nothing Then AddressBox.Text = selectedEmployee.Address CityBox.Text = selectedEmployee.City PostalCodeBox.Text = selectedEmployee.PostalCode Button1.Enabled = True Else Button1.Enabled = False End If End Sub ' Page_Load ' Press the button to update. Private Sub Btn_UpdateEmployee (sender As Object, e As CommandEventArgs ) ObjectDataSource2.Update() End Sub ' Btn_UpdateEmployee </Script> <html> <head> <title>ObjectDataSource - VB Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <!-- The DropDownList is bound to the first ObjectDataSource. --> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetAllEmployees" typename="Samples.AspNet.VB.EmployeeLogic" /> <p><asp:dropdownlist id="DropDownList1" runat="server" datasourceid="ObjectDataSource1" datatextfield="FullName" datavaluefield="EmpID" autopostback="True" /></p> <!-- The second ObjectDataSource performs the Update. This preserves the state of the DropDownList, which otherwise would rebind when the DataSourceChanged event is raised as a result of an Update operation. --> <!-- Security Note: The ObjectDataSource uses a FormParameter, Security Note: which does not perform validation of input from the client. Security Note: To validate the value of the FormParameter, Security Note: handle the Updating event. --> <asp:objectdatasource id="ObjectDataSource2" runat="server" updatemethod="UpdateEmployeeWrapper" typename="Samples.AspNet.VB.EmployeeLogic"> <updateparameters> <asp:controlparameter name="anID" controlid="DropDownList1" propertyname="SelectedValue" /> <asp:formparameter name="anAddress" formfield="AddressBox" /> <asp:formparameter name="aCity" formfield="CityBox" /> <asp:formparameter name="aPostalCode" formfield="PostalCodeBox" /> </updateparameters> </asp:objectdatasource> <p><asp:textbox id="AddressBox" runat="server" /></p> <p><asp:textbox id="CityBox" runat="server" /></p> <p><asp:textbox id="PostalCodeBox" runat="server" /></p> <asp:button id="Button1" runat="server" text="Update Employee" oncommand="Btn_UpdateEmployee" /> </form> </body> </html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %> <%@ Page language="c#" %> <%@ Import namespace="Samples.AspNet.CS" %> <Script runat="server"> // Add parameters and initialize the user interface // only if an employee is selected. private void Page_Load(object sender, EventArgs e) { // Be sure the text boxes are initialized with // data from the currently selected employee. NorthwindEmployee selectedEmployee = EmployeeLogic.GetEmployee(DropDownList1.SelectedValue); if (selectedEmployee != null) { AddressBox.Text = selectedEmployee.Address; CityBox.Text = selectedEmployee.City; PostalCodeBox.Text = selectedEmployee.PostalCode; Button1.Enabled = true; } else { Button1.Enabled = false; } } // Press the button to update. private void Btn_UpdateEmployee (object sender, CommandEventArgs e) { ObjectDataSource2.Update(); } </Script> <html> <head> <title>ObjectDataSource - C# Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <!-- The DropDownList is bound to the first ObjectDataSource. --> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetAllEmployees" typename="Samples.AspNet.CS.EmployeeLogic" /> <p><asp:dropdownlist id="DropDownList1" runat="server" datasourceid="ObjectDataSource1" datatextfield="FullName" datavaluefield="EmpID" autopostback="True" /></p> <!-- The second ObjectDataSource performs the Update. This preserves the state of the DropDownList, which otherwise would rebind when the DataSourceChanged event is raised as a result of an Update operation. --> <!-- Security Note: The ObjectDataSource uses a FormParameter, Security Note: which does not perform validation of input from the client. Security Note: To validate the value of the FormParameter, Security Note: handle the Updating event. --> <asp:objectdatasource id="ObjectDataSource2" runat="server" updatemethod="UpdateEmployeeWrapper" typename="Samples.AspNet.CS.EmployeeLogic"> <updateparameters> <asp:controlparameter name="anID" controlid="DropDownList1" propertyname="SelectedValue" /> <asp:formparameter name="anAddress" formfield="AddressBox" /> <asp:formparameter name="aCity" formfield="CityBox" /> <asp:formparameter name="aPostalCode" formfield="PostalCodeBox" /> </updateparameters> </asp:objectdatasource> <p><asp:textbox id="AddressBox" runat="server" /></p> <p><asp:textbox id="CityBox" runat="server" /></p> <p><asp:textbox id="PostalCodeBox" runat="server" /></p> <asp:button id="Button1" runat="server" text="Update Employee" oncommand="Btn_UpdateEmployee" /> </form> </body> </html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %> <%@ Page language="VJ#" %> <%@ Import namespace="Samples.AspNet.JSL" %> <Script runat="server"> // Add parameters and initialize the user interface // only if an employee is selected. private void Page_Load(Object sender, EventArgs e) throws NorthwindDataException { // Be sure the text boxes are initialized with // data from the currently selected employee. NorthwindEmployee selectedEmployee = EmployeeLogic.GetEmployee(DropDownList1.get_SelectedValue()); if (selectedEmployee != null) { AddressBox.set_Text(selectedEmployee.get_Address()); CityBox.set_Text(selectedEmployee.get_City()); PostalCodeBox.set_Text(selectedEmployee.get_PostalCode()); Button1.set_Enabled(true); } else { Button1.set_Enabled(false); } } //Page_Load // Press the button to update. private void Btn_UpdateEmployee(Object sender, CommandEventArgs e) { ObjectDataSource2.Update(); } //Btn_UpdateEmployee </Script> <html> <head> <title>ObjectDataSource - VJ# Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <!-- The DropDownList is bound to the first ObjectDataSource. --> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetAllEmployees" typename="Samples.AspNet.JSL.EmployeeLogic" /> <p><asp:dropdownlist id="DropDownList1" runat="server" datasourceid="ObjectDataSource1" datatextfield="FullName" datavaluefield="EmpID" autopostback="True" /></p> <!-- The second ObjectDataSource performs the Update. This preserves the state of the DropDownList, which otherwise would rebind when the DataSourceChanged event is raised as a result of an Update operation. --> <asp:objectdatasource id="ObjectDataSource2" runat="server" updatemethod="UpdateEmployeeWrapper" typename="Samples.AspNet.JSL.EmployeeLogic"> <updateparameters> <asp:controlparameter name="anID" controlid="DropDownList1" propertyname="SelectedValue" /> <asp:formparameter name="anAddress" formfield="AddressBox" /> <asp:formparameter name="aCity" formfield="CityBox" /> <asp:formparameter name="aPostalCode" formfield="PostalCodeBox" /> </updateparameters> </asp:objectdatasource> <p><asp:textbox id="AddressBox" runat="server" /></p> <p><asp:textbox id="CityBox" runat="server" /></p> <p><asp:textbox id="PostalCodeBox" runat="server" /></p> <asp:button id="Button1" runat="server" text="Update Employee" oncommand="Btn_UpdateEmployee" /> </form> </body> </html>
前のコード例で使用されている EmployeeLogic クラスを次のコード例に示します。
Imports System Imports System.Collections Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Namespace Samples.AspNet.VB ' ' EmployeeLogic is a stateless business object that encapsulates ' the operations one can perform on a NorthwindEmployee object. ' Public Class EmployeeLogic ' Returns a collection of NorthwindEmployee objects. Public Shared Function GetAllEmployees() As ICollection Dim al As New ArrayList() ' Use the SqlDataSource class to wrap the ' ADO.NET code required to query the database. Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection") Dim sds As New SqlDataSource(cts.ConnectionString, _ "SELECT EmployeeID FROM Employees") Try Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty) ' Iterate through the Enumeration and create a ' NorthwindEmployee object for each ID. Dim enumerator As IEnumerator = IDs.GetEnumerator() While enumerator.MoveNext() ' The IEnumerable contains DataRowView objects. Dim row As DataRowView = CType(enumerator.Current ,DataRowView) Dim id As String = row("EmployeeID").ToString() Dim nwe As New NorthwindEmployee(id) ' Add the NorthwindEmployee object to the collection. al.Add(nwe) End While Finally ' If anything strange happens, clean up. sds.Dispose() End Try Return al End Function 'GetAllEmployees Public Shared Function GetEmployee(anID As Object) As NorthwindEmployee Dim al As ArrayList = CType(GetAllEmployees(), ArrayList) Dim enumerator As IEnumerator = al.GetEnumerator() While enumerator.MoveNext() ' The IEnumerable contains initialized NorthwindEmployee objects. Dim ne As NorthwindEmployee = CType(enumerator.Current ,NorthwindEmployee) If ne.EmpID.Equals(anID.ToString()) Then Return ne End If End While Return Nothing End Function 'GetEmployee Public Shared Sub UpdateEmployee(ne As NorthwindEmployee) Dim retval As Boolean = ne.Update() If Not retval Then Throw New NorthwindDataException("Employee update failed.") End If End Sub 'UpdateEmployee ' This method is added as a conveniece wrapper on the original ' implementation. Public Shared Sub UpdateEmployeeWrapper(anID As String, _ anAddress As String, _ aCity As String, _ aPostalCode As String) Dim ne As New NorthwindEmployee(anID) ne.Address = anAddress ne.City = aCity ne.PostalCode = aPostalCode UpdateEmployee(ne) End Sub 'UpdateEmployeeWrapper ' And so on... End Class 'EmployeeLogic Public Class NorthwindEmployee Public Sub New(anID As Object) Me.ID = anID Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection") Dim conn As New SqlConnection(cts.ConnectionString) Dim sc As New SqlCommand(" SELECT FirstName,LastName,Address,City,PostalCode " & _ " FROM Employees " & _ " WHERE EmployeeID = @empId", conn) ' Add the employee ID parameter and set its value. sc.Parameters.Add(New SqlParameter("@empId", SqlDbType.Int)).Value = Int32.Parse(anID.ToString()) Dim sdr As SqlDataReader = Nothing Try conn.Open() sdr = sc.ExecuteReader() ' This is not a while loop. It only loops once. If Not (sdr Is Nothing) AndAlso sdr.Read() Then ' The IEnumerable contains DataRowView objects. Me.aFirstName = sdr("FirstName").ToString() Me.aLastName = sdr("LastName").ToString() Me.aAddress = sdr("Address").ToString() Me.aCity = sdr("City").ToString() Me.aPostalCode = sdr("PostalCode").ToString() Else Throw New NorthwindDataException("Data not loaded for employee id.") End If Finally Try If Not (sdr Is Nothing) Then sdr.Close() End If conn.Close() Catch se As SqlException ' Log an event in the Application Event Log. Throw End Try End Try End Sub 'New Private ID As Object Public ReadOnly Property EmpID() As Object Get Return ID End Get End Property Private aLastName As String Public WriteOnly Property LastName() As String Set aLastName = value End Set End Property Private aFirstName As String Public WriteOnly Property FirstName() As String Set aFirstName = value End Set End Property Public ReadOnly Property FullName() As String Get Return aFirstName & " " & aLastName End Get End Property Private aAddress As String Public Property Address() As String Get Return aAddress End Get Set aAddress = value End Set End Property Private aCity As String Public Property City() As String Get Return aCity End Get Set aCity = value End Set End Property Private aPostalCode As String Public Property PostalCode() As String Get Return aPostalCode End Get Set aPostalCode = value End Set End Property Public Function Update() As Boolean ' Implement Update logic. Return True End Function 'Update End Class 'NorthwindEmployee Friend Class NorthwindDataException Inherits Exception Public Sub New(msg As String) MyBase.New(msg) End Sub 'New End Class 'NorthwindDataException End Namespace
namespace Samples.AspNet.CS { using System; using System.Collections; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.UI; using System.Web.UI.WebControls; // // EmployeeLogic is a stateless business object that encapsulates // the operations one can perform on a NorthwindEmployee object. // public class EmployeeLogic { // Returns a collection of NorthwindEmployee objects. public static ICollection GetAllEmployees () { ArrayList al = new ArrayList(); // Use the SqlDataSource class to wrap the // ADO.NET code required to query the database. ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"]; SqlDataSource sds = new SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees"); try { IEnumerable IDs = sds.Select(DataSourceSelectArguments.Empty); // Iterate through the Enumeration and create a // NorthwindEmployee object for each ID. IEnumerator enumerator = IDs.GetEnumerator(); while (enumerator.MoveNext()) { // The IEnumerable contains DataRowView objects. DataRowView row = enumerator.Current as DataRowView; string id = row["EmployeeID"].ToString(); NorthwindEmployee nwe = new NorthwindEmployee(id); // Add the NorthwindEmployee object to the collection. al.Add(nwe); } } finally { // If anything strange happens, clean up. sds.Dispose(); } return al; } public static NorthwindEmployee GetEmployee(object anID) { ArrayList al = GetAllEmployees() as ArrayList; IEnumerator enumerator = al.GetEnumerator(); while (enumerator.MoveNext()) { // The IEnumerable contains initialized NorthwindEmployee objects. NorthwindEmployee ne = enumerator.Current as NorthwindEmployee; if (ne.EmpID.Equals(anID.ToString())) { return ne; } } return null; } public static void UpdateEmployee(NorthwindEmployee ne) { bool retval = ne.Update(); if (! retval) { throw new NorthwindDataException("Employee update failed."); } } // This method is added as a conveniece wrapper on the original // implementation. public static void UpdateEmployeeWrapper(string anID, string anAddress , string aCity, string aPostalCode) { NorthwindEmployee ne = new NorthwindEmployee(anID); ne.Address = anAddress; ne.City = aCity; ne.PostalCode = aPostalCode; UpdateEmployee(ne); } // And so on... } public class NorthwindEmployee { public NorthwindEmployee (object anID) { this.ID = anID; ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"]; SqlConnection conn = new SqlConnection (cts.ConnectionString); SqlCommand sc = new SqlCommand(" SELECT FirstName,LastName,Address,City,PostalCode " + " FROM Employees " + " WHERE EmployeeID = @empId", conn); // Add the employee ID parameter and set its value. sc.Parameters.Add(new SqlParameter("@empId",SqlDbType.Int)).Value = Int32.Parse(anID.ToString()); SqlDataReader sdr = null; try { conn.Open(); sdr = sc.ExecuteReader(); // This is not a while loop. It only loops once. if (sdr != null && sdr.Read()) { // The IEnumerable contains DataRowView objects. this.firstName = sdr["FirstName"].ToString(); this.lastName = sdr["LastName"].ToString(); this.address = sdr["Address"].ToString(); this.city = sdr["City"].ToString(); this.postalCode = sdr["PostalCode"].ToString(); } else { throw new NorthwindDataException("Data not loaded for employee id."); } } finally { try { if (sdr != null) sdr.Close(); conn.Close(); } catch (SqlException) { // Log an event in the Application Event Log. throw; } } } private object ID; public object EmpID { get { return ID; } } private string lastName; public string LastName { set { lastName = value; } } private string firstName; public string FirstName { set { firstName = value; } } public string FullName { get { return firstName + " " + lastName; } } private string address; public string Address { get { return address; } set { address = value; } } private string city; public string City { get { return city; } set { city = value; } } private string postalCode; public string PostalCode { get { return postalCode; } set { postalCode = value; } } public bool Update () { // Implement Update logic. return true; } } internal class NorthwindDataException: Exception { public NorthwindDataException(string msg) : base (msg) { } } }
package Samples.AspNet.JSL; import System.*; import System.Collections.*; import System.Configuration.*; import System.Data.*; import System.Data.SqlClient.*; import System.Web.UI.*; import System.Web.UI.WebControls.*; // // EmployeeLogic is a stateless business object that encapsulates // the operations one can perform on a NorthwindEmployee object. // public class EmployeeLogic { // Returns a collection of NorthwindEmployee objects. public static ICollection GetAllEmployees() throws NorthwindDataException { ArrayList al = new ArrayList(); // Use the SqlDataSource class to wrap the // ADO.NET code required to query the database. ConnectionStringSettings cts = ConfigurationManager.get_ConnectionStrings(). get_Item("NorthwindConnection"); SqlDataSource sds = new SqlDataSource(cts.get_ConnectionString() , "SELECT EmployeeID FROM Employees"); try { IEnumerable ids = sds.Select(DataSourceSelectArguments.get_Empty()); // Iterate through the Enumeration and create a // NorthwindEmployee object for each ID. IEnumerator enumerator = ids.GetEnumerator(); while (enumerator.MoveNext()) { // The IEnumerable contains DataRowView objects. DataRowView row = (DataRowView)enumerator.get_Current(); String idObj = row.get_Item("EmployeeID").ToString(); NorthwindEmployee nwe = new NorthwindEmployee(idObj); // Add the NorthwindEmployee object to the collection. al.Add(nwe); } } finally { // If anything strange happens, clean up. sds.Dispose(); } return al; } //GetAllEmployees public static NorthwindEmployee GetEmployee(Object anID) throws NorthwindDataException { ArrayList al = (ArrayList)GetAllEmployees(); IEnumerator enumerator = al.GetEnumerator(); while (enumerator.MoveNext()) { // The IEnumerable contains initialized NorthwindEmployee objects. NorthwindEmployee ne = (NorthwindEmployee)enumerator.get_Current(); if (ne.get_EmpID().Equals(anID.ToString())) { return ne; } } return null; } //GetEmployee public static void UpdateEmployee(NorthwindEmployee ne) throws NorthwindDataException { boolean retVal = ne.Update(); if (!(retVal)) { throw new NorthwindDataException("Employee update failed."); } } //UpdateEmployee // This method is added as a conveniece wrapper on the original // implementation. public static void UpdateEmployeeWrapper(String anID, String anAddress, String aCity, String aPostalCode) throws NorthwindDataException { NorthwindEmployee ne = new NorthwindEmployee(anID); ne.set_Address(anAddress); ne.set_City(aCity); ne.set_PostalCode(aPostalCode); UpdateEmployee(ne); } //UpdateEmployeeWrapper // And so on... } //EmployeeLogic public class NorthwindEmployee { public NorthwindEmployee(Object anID) throws NorthwindDataException { this.id = anID; ConnectionStringSettings cts = ConfigurationManager.get_ConnectionStrings(). get_Item("NorthwindConnection"); SqlConnection conn = new SqlConnection(cts.get_ConnectionString()); SqlCommand sc = new SqlCommand(" SELECT FirstName ,LastName,Address," + "City,PostalCode " + " FROM Employees " + " WHERE EmployeeID = @empId", conn); // Add the employee ID parameter and set its value. sc.get_Parameters().Add(new SqlParameter("@empId", SqlDbType.Int)). set_Value(anID.ToString()); SqlDataReader sdr = null; try { conn.Open(); sdr = sc.ExecuteReader(); // This is not a while loop. It only loops once. if (sdr != null && sdr.Read()) { // The IEnumerable contains DataRowView objects. this.firstName = sdr.get_Item("FirstName").ToString(); this.lastName = sdr.get_Item("LastName").ToString(); this.address = sdr.get_Item("Address").ToString(); this.city = sdr.get_Item("City").ToString(); this.postalCode = sdr.get_Item("PostalCode").ToString(); } else { throw new NorthwindDataException("Data not loaded for" + " employee id."); } } finally { try { if (sdr != null) { sdr.Close(); } conn.Close(); } catch (SqlException exp) { // Log an event in the Application Event Log. } } } //NorthwindEmployee private Object id; /** @property */ public Object get_EmpID() { return id; } //get_EmpID private String lastName; /** @property */ public void set_LastName(String value) { lastName = value; } //set_LastName private String firstName; /** @property */ public void set_FirstName(String value) { firstName = value; } //set_FirstName /** @property */ public String get_FullName() { return firstName + " " + lastName; } //get_FullName private String address; /** @property */ public String get_Address() { return address; } //get_Address /** @property */ public void set_Address(String value) { address = value; } //set_Address private String city; /** @property */ public String get_City() { return city; } //get_City /** @property */ public void set_City(String value) { city = value; } //set_City private String postalCode; /** @property */ public String get_PostalCode() { return postalCode; } //get_PostalCode /** @property */ public void set_PostalCode(String value) { postalCode = value; } //set_PostalCode public boolean Update() { // Implement Update logic. return true; } //Update } //NorthwindEmployee public class NorthwindDataException extends Exception { public NorthwindDataException(String msg) { super(msg); } //NorthwindDataException } //NorthwindDataException

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- ObjectDataSource.UpdateMethod プロパティのページへのリンク