iBATIS.NET 学习笔记(六)
用iBATIS.NET实现简单的添加记录到数据库(向数据库Northwind中的Cutsomers表添加记录)
新建Web页面,Test2.aspx
Test2.aspx.cs
新建Web页面,Test2.aspx
<%@ Page language="c#" Codebehind="Test2.aspx.cs" AutoEventWireup="false" Inherits="IbatisNet.Example.Test2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test2</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"></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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test2</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"></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>
Test2.aspx.cs
//***********************************************************
//*公司:浙江航大科技开发有限公司
//*作者:YK
//*模块:Test2
//*功能:
//*创建日期:
//*修改日期:
//***********************************************************
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;
using IBatisNet.Common;
using IBatisNet.DataMapper;
using IBatisNet.DataAccess;
namespace IbatisNet.Example
{
/// <summary>
/// Test2 的摘要说明。
/// </summary>
public class Test2 : 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.Label lbMessage;
protected System.Web.UI.WebControls.Button btnSave;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
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
{
//添加记录到数据库
//"InsertCustomer"添加语句在Maps/Customers.xml中设置
IbatisNet.Example.Mapper.Instance().Insert("InsertCustomer",customer);
this.lbMessage.Text="新增记录成功!";
}
catch(Exception ex)
{
this.lbMessage.Text="新增记录出错!:"+ex.Message;
}
}
}
}
//*公司:浙江航大科技开发有限公司
//*作者:YK
//*模块:Test2
//*功能:
//*创建日期:
//*修改日期:
//***********************************************************
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;
using IBatisNet.Common;
using IBatisNet.DataMapper;
using IBatisNet.DataAccess;
namespace IbatisNet.Example
{
/// <summary>
/// Test2 的摘要说明。
/// </summary>
public class Test2 : 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.Label lbMessage;
protected System.Web.UI.WebControls.Button btnSave;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
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
{
//添加记录到数据库
//"InsertCustomer"添加语句在Maps/Customers.xml中设置
IbatisNet.Example.Mapper.Instance().Insert("InsertCustomer",customer);
this.lbMessage.Text="新增记录成功!";
}
catch(Exception ex)
{
this.lbMessage.Text="新增记录出错!:"+ex.Message;
}
}
}
}