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:
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>

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:


<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)
        if (!IsPostBack)
        // Download data from http://www.learnasp.com/biblio        
        } // end try
        catch (Exception exc1)
        } // end catch
    } // end page_load
<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" />

config file:


<Test Script Below>

<?xml version="1.0" encoding="utf-8" ?> 
    <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; "/>

config file:


<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. -->
        <typeAlias alias="Publishers" type="IbatisBuraq.WebFace2.Publishers" />
        <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" />
        <select id="Select" resultMap="SelectAllResult">
            select Name,[Company Name] as CompanyName,Address,City, State 
            from publishers 
            Where State='NY'

IbatisBuraq.WebFace2.Publishers Class:


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; }