Fluent nhib1_example, fluentnhib.pdf

Source: Internet
Author: User

Fluent nhib1_example, fluentnhib.pdf

Http://www.codeproject.com/Articles/26466/Dependency-Injection-using-Spring-NET

Http://stackoverflow.com/questions/29767825/spring-netnhibernate-configuration

Http://nhbusinessobj.sourceforge.net/index.html

Http://code.google.com/p/genericrepository/

SQL:

CREATE TABLE [dbo].[Customers]([customer_id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,[name] [nvarchar](75) NULL,[email] [nvarchar](95) NULL,[contact_person] [nvarchar](75) NULL,[postal_address] [nvarchar](150) NULL,[physical_address] [nvarchar](150) NULL,[contact_number] [nvarchar](50) NULL,CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED([customer_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] GO INSERT INTO [dbo].[Customers]           ([name]           ,[email]           ,[contact_person]           ,[postal_address]           ,[physical_address]           ,[contact_number])     VALUES           ('Kode Blog'           ,'a-team@kode-blog.com'           ,'Rodrick Kazembe'           ,'Private Bag WWW'           ,'Tanzania'           ,'911')   INSERT INTO [dbo].[Customers]           ([name]           ,[email]           ,[contact_person]           ,[postal_address]           ,[physical_address]           ,[contact_number])     VALUES          ('Google Inc'           ,'info@google.com'           ,''           ,''           ,'USA'           ,'')GO

  

    /// <summary>    ///     /// </summary>    public class Customers    {        public virtual int customer_id { get; protected set; }        public virtual string name { get; set; }        public virtual string email { get; set; }        public virtual string contact_person { get; set; }        public virtual string postal_address { get; set; }        public virtual string physical_address { get; set; }        public virtual string contact_number { get; set; }    }

  

using System;using System.Collections.Generic;using System.Linq;using System.Text;using FluentNHibernate.Mapping;namespace CodeBlogdeom{    /// <summary>    ///     /// </summary>    class CustomersMap : ClassMap<Customers>    {        /// <summary>        ///         /// </summary>        public CustomersMap()        {            Id(x => x.customer_id);            Map(x => x.name);            Map(x => x.email);            Map(x => x.contact_person);            Map(x => x.postal_address);            Map(x => x.physical_address);            Map(x => x.contact_number);        }    }}

  

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Globalization;using NHibernate.Persister;using FluentNHibernate.Cfg;using FluentNHibernate.Cfg.Db;using NHibernate;using NHibernate.Cfg;//http://www.kode-blog.com/2014/04/fluent-nhibernate-tutorial-c-windows-crud-example/namespace CodeBlogdeom{    /// <summary>    ///     /// </summary>    public partial class frmCustomers : Form    {        #region declarations        ISessionFactory sessionFactory;        #endregion        #region methods        private void load_records(string sFilter = "")        {            try            {                sessionFactory = CreateSessionFactory();                using (var session = sessionFactory.OpenSession())                {                    string h_stmt = "FROM Customers";                    if (sFilter != "")                    {                        h_stmt += " WHERE " + sFilter;                    }                    IQuery query = session.CreateQuery(h_stmt);                    IList<Customers> customersList = query.List<Customers>();                    dgvListCustomers.DataSource = customersList;                    lblStatistics.Text = "Total records returned: " + customersList.Count;                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }        private static ISessionFactory CreateSessionFactory()        {            ISessionFactory isessionFactory = Fluently.Configure()                .Database(MsSqlConfiguration.MsSql2005                .ConnectionString(@"Server=GEOVINDU-PC\GEOVIN; Database=NHibernateSimpleDemo; Integrated Security=SSPI;"))                .Mappings(m => m                .FluentMappings.AddFromAssemblyOf<frmCustomers>())                .BuildSessionFactory();            return isessionFactory;        }        /// <summary>        ///         /// </summary>        /// <param name="customer_id"></param>        private void load_customer_details(int customer_id)        {            using (ISession session = sessionFactory.OpenSession())            {                using (ITransaction transaction = session.BeginTransaction())                {                    try                    {                        IQuery query = session.CreateQuery("FROM Customers WHERE customer_id = " + customer_id);                        Customers customer = query.List<Customers>()[0];                        txtCustomerId.Text = customer.customer_id.ToString();                        txtName.Text = customer.name;                        txtEmail.Text = customer.email;                        txtContactPerson.Text = customer.contact_person;                        txtContactNumber.Text = customer.contact_number;                        txtPostalAddress.Text = customer.postal_address;                        txtPhysicalAddress.Text = customer.physical_address;                    }                    catch (Exception ex)                    {                        MessageBox.Show(ex.Message, "Exception Msg");                    }                }            }        }        #endregion        /// <summary>        ///         /// </summary>        public frmCustomers()        {            InitializeComponent();        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Form1_Load(object sender, EventArgs e)        {            load_records();        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnClose_Click(object sender, EventArgs e)        {            Close();        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnFilter_Click(object sender, EventArgs e)        {            string sFilterValue = string.Empty;            string sField = cboFilter.Text;            string sCriteria = cboCriteria.Text;            string sValue = txtValue.Text;            switch (sCriteria)            {                case "Equals":                    sFilterValue = sField + " = '" + sValue + "'";                    break;                case "Begins with":                    sFilterValue = sField + " LIKE '" + sValue + "%'";                    break;                case "Contains":                    sFilterValue = sField + " LIKE '%" + sValue + "%'";                    break;                case "Ends with":                    sFilterValue = sField + " LIKE '%" + sValue + "'";                    break;            }            //data.Add(sFilterValue, sValue);            load_records(sFilterValue);        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void dgvListCustomers_Click(object sender, EventArgs e)        {            int customer_id = 0;            customer_id = int.Parse(dgvListCustomers.CurrentRow.Cells[0].Value.ToString());            load_customer_details(customer_id);        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnAddNew_Click(object sender, EventArgs e)        {            //data validation            if (txtName.Text == "")            {                MessageBox.Show("The name field is required", "Null name", MessageBoxButtons.OK, MessageBoxIcon.Warning);                return;            }            if (txtEmail.Text == "")            {                MessageBox.Show("The email field is required", "Null email", MessageBoxButtons.OK, MessageBoxIcon.Warning);                return;            }            if (txtPhysicalAddress.Text == "")            {                MessageBox.Show("The physical address field is required", "Null physical address", MessageBoxButtons.OK, MessageBoxIcon.Warning);                return;            }            Customers customer = new Customers();            customer.name = txtName.Text;            customer.email = txtEmail.Text;            customer.contact_person = txtContactPerson.Text;            customer.contact_number = txtContactNumber.Text;            customer.physical_address = txtPhysicalAddress.Text;            customer.postal_address = txtPostalAddress.Text;            using (var session = sessionFactory.OpenSession())            {                using (ITransaction transaction = session.BeginTransaction())                {                    try                    {                        session.Save(customer);                        transaction.Commit();                        load_records();                    }                    catch (Exception ex)                    {                        transaction.Rollback();                        MessageBox.Show(ex.Message, "Exception Msg");                    }                }            }        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnRefresh_Click(object sender, EventArgs e)        {            load_records();        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnUpdate_Click(object sender, EventArgs e)        {            //data validation            if (txtName.Text == "")            {                MessageBox.Show("The name field is required", "Null name", MessageBoxButtons.OK, MessageBoxIcon.Warning);                return;            }            if (txtEmail.Text == "")            {                MessageBox.Show("The email field is required", "Null email", MessageBoxButtons.OK, MessageBoxIcon.Warning);                return;            }            if (txtPhysicalAddress.Text == "")            {                MessageBox.Show("The physical address field is required", "Null physical address", MessageBoxButtons.OK, MessageBoxIcon.Warning);                return;            }            using (var session = sessionFactory.OpenSession())            {                using (ITransaction transaction = session.BeginTransaction())                {                    try                    {                        IQuery query = session.CreateQuery("FROM Customers WHERE customer_id = '" + txtCustomerId.Text + "'");                        Customers customer = query.List<Customers>()[0];                        customer.name = txtName.Text;                        customer.email = txtEmail.Text;                        customer.contact_person = txtContactPerson.Text;                        customer.contact_number = txtContactNumber.Text;                        customer.physical_address = txtPhysicalAddress.Text;                        customer.postal_address = txtPostalAddress.Text;                        session.Update(customer);                        transaction.Commit();                        load_records();                    }                    catch (Exception ex)                    {                        transaction.Rollback();                        MessageBox.Show(ex.Message, "Exception Msg");                    }                }            }        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnDelete_Click(object sender, EventArgs e)        {            using (ISession session = sessionFactory.OpenSession())            {                using (ITransaction transaction = session.BeginTransaction())                {                    try                    {                        IQuery query = session.CreateQuery("FROM Customers WHERE customer_id = '" + txtCustomerId.Text + "'");                         Customers customer = query.List<Customers>()[0];                         session.Delete(customer); //delete the record                         transaction.Commit(); //commit it                         btnRefresh_Click(sender, e);                     }                     catch (Exception ex)                    {                         transaction.Rollback();                         MessageBox.Show(ex.Message, "Exception Msg");                     }                 }             }               }    }}

  

Related Keywords:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.