iBATIS.NET 学习笔记(七)

用iBATIS.NET读取单条记录,并对这条记录进行修改。
修改Maps/Customers.xml文件,在statements标记中添加下面代码:
<update id="UpdateCustomer" parameterClass="Customers">
        update Customers set 
        CompanyName=#CompanyName#,
        ContactName=#ContactName#,
        ContactTitle=#ContactTitle#,
        Address=#Address#,
        City=#City#,
        Region=#Region#,
        PostalCode=#PostalCode#,
        Country=#Country#,
        Phone=#Phone#,
        Fax=#Fax#
        where CustomerID=#CustomerID#
        
</update>
        
        
<statement id="SelectCustomer" parameterClass="string" resultClass="Customers">
        select 
        CustomerID as CustomerID,
        CompanyName as CompanyName,
        ContactName as ContactName,
        ContactTitle as ContactTitle,
        Address as Address,
        City as City,
        Region as Region,
        PostalCode as PostalCode,
        Country as Country,
        Phone as Phone,
        Fax as Fax
        from Customers where CustomerID=#value#
        
</statement>

新建Web页面,Test3.aspx
<%@ Page language="c#" Codebehind="Test3.aspx.cs" AutoEventWireup="false" Inherits="IbatisNet.Example.Test3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>Test3</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    
</HEAD>
    
<body>
        
<form id="Form1" method="post" runat="server">
            
<h1><P><FONT face="宋体">修改数据库中的记录</FONT></P>
                
<P><FONT face="宋体">
                        
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="0">
                            
<TR>
                                
<TD>CustomerID</TD>
                                
<TD>
                                    
<asp:TextBox id="txtID" runat="server" MaxLength="5" ReadOnly="True"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>CompanyName</TD>
                                
<TD>
                                    
<asp:TextBox id="txtComName" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>ContactName</TD>
                                
<TD>
                                    
<asp:TextBox id="txtContName" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>ContactTitle</TD>
                                
<TD>
                                    
<asp:TextBox id="txtContTitle" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>Address</TD>
                                
<TD>
                                    
<asp:TextBox id="txtAddress" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>City</TD>
                                
<TD>
                                    
<asp:TextBox id="txtCity" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>Region</TD>
                                
<TD>
                                    
<asp:TextBox id="txtRegion" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>PostalCode</TD>
                                
<TD>
                                    
<asp:TextBox id="txtPostal" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>Country</TD>
                                
<TD>
                                    
<asp:TextBox id="txtCountry" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>Phone</TD>
                                
<TD>
                                    
<asp:TextBox id="txtPhone" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD>Fax</TD>
                                
<TD>
                                    
<asp:TextBox id="txtFax" runat="server"></asp:TextBox></TD>
                            
</TR>
                            
<TR>
                                
<TD align="center" colSpan="2">
                                    
<asp:Button id="btnSave" runat="server" Text="保存"></asp:Button></TD>
                            
</TR>
                        
</TABLE>
                    
</FONT>
                
</P>
            
</h1>
            
<asp:Label id="lbMessage" runat="server" ForeColor="Red"></asp:Label>
        
</form>
    
</body>
</HTML>

Test3.aspx.cs
//*****************************************************************
//*组织机构:
//*模块名称:
//*模块功能:
//*创建者  : YK
//*创建时间:
//*修改者  :
//*修改时间:
//*****************************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace IbatisNet.Example
{
    
/// <summary>
    
/// Test3 的摘要说明。
    
/// </summary>

    public class Test3 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.TextBox txtID;
        
protected System.Web.UI.WebControls.TextBox txtComName;
        
protected System.Web.UI.WebControls.TextBox txtContName;
        
protected System.Web.UI.WebControls.TextBox txtContTitle;
        
protected System.Web.UI.WebControls.TextBox txtAddress;
        
protected System.Web.UI.WebControls.TextBox txtCity;
        
protected System.Web.UI.WebControls.TextBox txtRegion;
        
protected System.Web.UI.WebControls.TextBox txtPostal;
        
protected System.Web.UI.WebControls.TextBox txtCountry;
        
protected System.Web.UI.WebControls.TextBox txtPhone;
        
protected System.Web.UI.WebControls.TextBox txtFax;
        
protected System.Web.UI.WebControls.Button btnSave;
        
protected System.Web.UI.WebControls.Label lbMessage;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!Page.IsPostBack) {
                IbatisNet.Example.Model.Customers customer 
= new IbatisNet.Example.Model.Customers();
                
//HZZKE是Customer的CustomerID
                customer = IbatisNet.Example.Mapper.Instance().QueryForObject("SelectCustomer","HZZKE"as IbatisNet.Example.Model.Customers ;
                
this.txtAddress.Text = customer.Address;
                
this.txtCity.Text = customer.City;
                
this.txtComName.Text = customer.CompanyName;
                
this.txtContName.Text = customer.ContactName;
                
this.txtContTitle.Text = customer.ContactTitle;
                
this.txtCountry.Text = customer.Country;
                
this.txtFax.Text = customer.Fax;
                
this.txtID.Text = customer.CustomerID;
                
this.txtPhone.Text = customer.Phone;
                
this.txtPostal.Text = customer.PostalCode;
                
this.txtRegion.Text = customer.Region;
                 
            }

        }


        
Web 窗体设计器生成的代码

        
private void btnSave_Click(object sender, System.EventArgs e) {
            IbatisNet.Example.Model.Customers customer 
= new IbatisNet.Example.Model.Customers();
            customer.CustomerID 
= this.txtID.Text;
            customer.CompanyName 
= this.txtComName.Text;
            customer.ContactName 
= this.txtContName.Text;
            customer.ContactTitle 
= this.txtContTitle.Text;
            customer.Country 
= this.txtCountry.Text;
            customer.Fax 
= this.txtFax.Text;
            customer.Phone 
= this.txtPhone.Text;
            customer.PostalCode 
= this.txtPostal.Text;
            customer.Region 
= this.txtRegion.Text;
            customer.Address 
= this.txtAddress.Text;
            customer.City 
= this.txtCity.Text;
            
try {
                IbatisNet.Example.Mapper.Instance().Update(
"UpdateCustomer",customer);
                
this.lbMessage.Text="记录修改成功!";
            }

            
catch(Exception ex) {
                
this.lbMessage.Text="记录修改出错"+ex.Message;     
            }

        }

    }

}

posted @ 2006-07-19 21:04  Kangaroo  阅读(622)  评论(0编辑  收藏  举报