E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

C# Ibatis Sample
by Mohamed Mudassair & Charles Carroll

You code your app and place your ad-hoc SQL statements and stored Procs outside of the code in an XML file. Your code works against the database and SQL specified in the XML.

Benefits of IBatis
Lets say for example your XML file supports Access. Then later you want to switch the app to SQLserver.
  • You add a XML file for SQLserver and adjust an app config file.
  • And later you want to migrate the app to Oracle. Make a new XML file with all the Oracle info and you change config and you are running against Oracle now.
  • Even if the XML file is ad-hoc SQL later you can change to Stored Procedures or Packages with merely adding an XML file and changing the config to point to it.
  • The DBAs can write efficient Sprocs or packages and change your code to sue them without touching the code just adding some new XML and a config file change.
  • It is possible to change databases by merely adding XML files and changing confuigs without recompiling the code. In fact the person making up the XML files need know little about the code, just needs to know the DB well. For example, someone who knows mySQL or DB2 can change an app written by another coder, that was running in another DB quickly transformed to be running DB2 or mySQL in no time.

This page demonstrates IBatis displaying a database query in a datagrid. Here is the code sample:

   filename=/experiments/ibatis/webface2/cs_dbtable.aspx

<Test Script Below>


<%@ trace="true" %>
<%@ Import Namespace="IBatisNet.Common"%>
<%@ Import Namespace="IBatisNet.DataAccess"%>
<%@ Import Namespace="IBatisNet.DataMapper"%>
<%@ assembly src="Publishers.cs" %>
<%@ Import Namespace ="IbatisBuraq.WebFace2"%>
<script language="C#" runat="server">
protected void Page_Load(Object Src, EventArgs E)
    {
    try
        {
        if (!IsPostBack)
                {
        D1.DataSource=Mapper.Instance().QueryForList("Select",null);
        D1.DataBind();
        // Download data from http://www.learnasp.com/biblio        
                }
        } // end try
        catch (Exception exc1)
        {
            litExc.Text=exc1.ToString();
        } // end catch
    } // end page_load
</script>
<html><head><title>First Ibatis Sample</title></head>
<body bgcolor="#FFFFFF">
<font face="Verdana"><h3>First Ibatis Sample</h3></font>
<asp:literal id="litExc" runat="server" />
<ASP:DataGrid id="D1" runat="server" />
</body></html>


config file:

   filename=/experiments/ibatis/webface2/maps/properties.xml

<Test Script Below>


<?xml version="1.0" encoding="utf-8" ?> 
<settings>
    <add key="provider" value="OleDb1.1" />
    <!--add key="connectionString" value="Server=(local);user id=sa;pwd=mom; database=Northwind" /-->
    <add key="connectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Domains\learnasp.com\wwwroot\experiments\data\biblio.mdb; "/>
</settings>

config file:

   filename=/experiments/ibatis/webface2/maps/sqlmap.xml

<Test Script Below>


<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Person" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SqlMap.xsd">
    <!-- XML "behind" document for the People service class. -->
    <alias>
        <typeAlias alias="Publishers" type="IbatisBuraq.WebFace2.Publishers" />
    </alias>
    <resultMaps>
        <resultMap id="SelectAllResult" class="Publishers">
            <result property="Name" column="Name" />
            <result property="CompanyName" column="CompanyName" />
            <result property="Address" column="Address" />
            <result property="City" column="City" />
            <result property="State" column="State" />
        </resultMap>
    </resultMaps>
    <statements>
        <select id="Select" resultMap="SelectAllResult">
            select Name,[Company Name] as CompanyName,Address,City, State 
            from publishers 
            Where State='NY'
        </select>
    </statements>
</sqlMap>

IbatisBuraq.WebFace2.Publishers Class:

filename=/experiments/ibatis/webface2/publishers.cs

using System;

namespace IbatisBuraq.WebFace2
{

    /// <summary>
    /// Business object representing a Person 
    /// entity in our problem domain. 
    /// </summary>    
    public class Publishers
    {
        /*
        private string _Property;
        public string Property
        {
            get { return _Property; }
            set { _Property = value; }
        }
        */

        
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _CompanyName;
        public string CompanyName
        {
            get { return _CompanyName; }
            set { _CompanyName= value; }
        }

        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 _State;
        public string State
        {
            get { return _State; }
            set { _State = value; }
        }

    }
}
Did this site teach you to code?   THANK US here!.   Your gift is NOT tax deductible, but I am very grateful. 2004 gift gallery & 2005 gift gallery