model,dal,DataHelp

model:  (Bin.cs)

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataHelp;


namespace Model
{
   public class Bin : ModelBase
    {
       /// <summary>
       /// 庫位設置
       /// </summary>
       public Bin() { }
       /// <summary>
       /// 庫位編碼
       /// </summary>
       private string _binno;


       public string Binno
       {
           get { return _binno; }
           set { _binno = value; SetValue("Binno"); }
       }
       /// <summary>
       /// 倉庫代碼
       /// </summary>
       private string _storahouse;


       public string Storahouse
       {
           get { return _storahouse; }
           set { _storahouse = value; SetValue("Storahouse"); }
       }
       /// <summary>
       /// 庫位長度
       /// </summary>
       private double _length;


       public double Length
       {
           get { return _length; }
           set { _length = value; SetValue("Length"); }
       }
       /// <summary>
       /// 庫位寬度
       /// </summary>
       private double _width;


       public double Width
       {
           get { return _width; }
           set { _width = value; SetValue("Width"); }
       }
       /// <summary>
       /// 可用高度
       /// </summary>
       private double _height;


       public double Height
       {
           get { return _height; }
           set { _height = value; SetValue("Height"); }
       }


       private string _unit;
       /// <summary>
       /// 單位
       /// </summary>
       public string Unit
       {
           get { return _unit; }
           set { _unit = value; SetValue("Unit"); }
       }
       /// <summary>
       /// 所屬通道
       /// </summary>
       private string _channel;


       public string Channel
       {
           get { return _channel; }
           set { _channel = value; SetValue("Channel"); }
       }
       /// <summary>
       /// 備註
       /// </summary>
       private string _remark;


       public string Remark
       {
           get { return _remark; }
           set { _remark = value; SetValue("Remark"); }
       }
       public override string GetPrimaryKey()
       {
           return "Binno";
       }
    }
}


DataHelp.ModelBase頁面:

using System;
using System.Collections.Generic;
using System.Text;
namespace DataHelp
{
    public abstract class ModelBase
    {
        public abstract string GetPrimaryKey();
        private List<float> list2 = new List<float>();
        private List<string> list = new List<string>();
        public void SetValue(string name)
        {
            if (!list.Contains(name))
            {
                list.Add(name);
            }
        }


        public int Count()
        {
            return list.Count;
        }


        public string GetValue(int index)
        {
            return list[index];
        }
        /// <summary>
        /// 確定model里是否有字段為name
        /// </summary>
        /// <param name="name">要查找字段的名稱</param>
        /// <returns></returns>
        public bool IsValue(string name)
        {
            return list.Contains(name);
        }


    }
}



BinDAL.cs頁面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataHelp;
using System.Data;
using System.Data.SqlClient;
using Model;
namespace DAL
{
    public class BinDAL : DALBase
    {
        /// <summary>
        /// 查詢當前庫位編號是否存在
        /// </summary>
        /// <param name="binNo"></param>
        /// <returns></returns>
        public static bool ExistsBinNo(string binNo)
        {
            string sql = "select count(*) from bin where binNo=@binNo";
            SQLParameterManager spm = new SQLParameterManager(sql, CommandType.Text);
            spm.Add("@binNo", binNo);
            object obj = SqlDataAccess.ExecuteScalar(spm);
            bool isOk = false;
            if (obj != null && Convert.ToInt32(obj.ToString()) >= 1) isOk = true;
            return isOk;


        }
        public static DataTable BinBindWH(string storahouse)
        {
            string sql = string.Format("select * from Bin where storahouse=@storahouse");
            SQLParameterManager spm = new SQLParameterManager(sql, CommandType.Text);
            spm.Add("@storahouse", storahouse);
            return SqlDataAccess.FillTable(spm);
        }


        public static DataTable BindInfo(string binNo)
        {
            string sql = "select * from bin where binNo=@binNo";
            SQLParameterManager spm = new SQLParameterManager(sql, CommandType.Text);
            spm.Add("@binNo", binNo);
            return SqlDataAccess.FillTable(spm);
        }
        /// <summary>
        /// 根據條件查找庫位的信息
        /// 參數searhNUM條件
        /// 參數bin編號
        /// </summary>
        /// <param name="searhNUM"></param>
        /// <param name="bin"></param>
        /// <returns></returns>
       
        public static DataTable SearchBinByNo(Bin bin,string l,string w,string h, int Sbin,string SbinStart,string SbinEnd,int Vbin,string VbinStart,string VbinEnd)
        {
            string sql = "SearchBin";
         
            SQLParameterManager spm = new SQLParameterManager(sql, CommandType.StoredProcedure);
            spm.Add("@binno", bin.Binno);
            spm.Add("@storahouse",bin.Storahouse);
            spm.Add("@length",l);
            spm.Add("@width",w);
            spm.Add("@height",h);
            spm.Add("@unit",bin.Unit);
            spm.Add("@Sbin",Sbin);
            spm.Add("@SbinStart",SbinStart);
            spm.Add("@SbinEnd",SbinEnd);
            spm.Add("@Vbin",Vbin);
            spm.Add("@VbinStart",VbinStart);
            spm.Add("@VbinEnd",VbinEnd);
            return SqlDataAccess.FillTable(spm);
        }
        public static DataTable GETBIN()
        {
            string sql = "select * from bin where 1=1";
            SQLParameterManager spm = new SQLParameterManager(sql, CommandType.Text);
            return SqlDataAccess.FillTable(spm);
        }
        /// <summary>
        /// 批量導入數據
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="msg"></param>
        /// <param name="useTime"></param>
        public static void InsertBinAll(DataTable dt,out string msg,out string useTime)
        {
            string constring = SqlDataAccess.ConString;
            using (SqlConnection con = new SqlConnection(constring))
            {
                con.Open();
                using (SqlBulkCopy sb = new SqlBulkCopy(con))
                {
                    DateTime startTime = DateTime.Now;


                    sb.DestinationTableName = "dbo.Bin";
                    sb.ColumnMappings.Add("Binno_", "binno");
                    sb.ColumnMappings.Add("Storahouse_", "storahouse");
                    sb.ColumnMappings.Add("Length_", "length");
                    sb.ColumnMappings.Add("Width_", "width");
                    sb.ColumnMappings.Add("Height_", "height");
                    sb.ColumnMappings.Add("Unit_", "unit");
                    sb.ColumnMappings.Add("Channel_", "channel");
                    sb.ColumnMappings.Add("Remark_", "remark");
                    try
                    {
                        sb.WriteToServer(dt);
                        DateTime endTime = DateTime.Now;
                        TimeSpan t = endTime -startTime;
                        useTime = t.ToString();
                        msg = "Success";
                    }
                    catch (Exception ex)
                    {
                        msg ="出現異常:"+ ex.Message.ToString();
                        useTime = "";
                    }
                }
            }


        }




        public static DataTable InfoChannel(string storahouse)
        {
            string sql = "select distinct channel from bin where storahouse=@storahouse";
            SQLParameterManager spm = new SQLParameterManager(sql, CommandType.Text);
            spm.Add("storahouse",storahouse);
            return SqlDataAccess.FillTable(spm);
        }




    }
}



DataHelp.DALBase頁面:

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.SqlClient;


namespace DataHelp
{
    public class DALBase
    {
         
        public static int Insert(ModelBase info)
        {
            SQLParameterManager manager = InsertBase(info);
            return SqlDataAccess.ExecuteNonQuery(manager);
        }


        public static int InserAndIdentity(ModelBase model)
        {
            SQLParameterManager manager = InsertBase(model);
            manager.SqlText += ";select @@identity";
            return int.Parse(SqlDataAccess.ExecuteScalar(manager).ToString());
        }


        private static SQLParameterManager InsertBase(ModelBase info)
        {
            if (info.Count() == 0)
            {
                throw new Exception("无法执行没有列的Insert命令");
            }
            Type type = info.GetType();
            //获得实体类的类名(表名)..
            string tableName = type.Name;
            SQLParameterManager manager = new SQLParameterManager();
            StringBuilder txt1 = new StringBuilder();
            StringBuilder txt2 = new StringBuilder();
            for (int i = 0; i < info.Count(); i++)
            {
                string name = info.GetValue(i);
                txt1.Append(name + ",");
                txt2.Append("@" + name + ",");
                PropertyInfo pinfo = type.GetProperty(name);
                manager.Add("@" + name, pinfo.GetValue(info, null));
            }
            txt1.Remove(txt1.Length - 1, 1);
            txt2.Remove(txt2.Length - 1, 1);
            string sql = string.Format("insert into {0}({1}) values({2})", tableName, txt1.ToString(), txt2.ToString());
            manager.SqlText = sql;
            return manager;
        }


        public static int Update(ModelBase model)
        {
            string key = model.GetPrimaryKey();
            if (key == null || key.Length == 0)
            {
                throw new Exception("无法执行没有主键的Update命令");
            }
            if (!model.IsValue(key))
            {
                throw new Exception("无法执行没有主键的Update命令");
            }
            if (model.Count() < 2)
            {
                throw new Exception("无法执行Update命令");
            }


            Type type = model.GetType();
            string tableName = type.Name;
            SQLParameterManager manager = new SQLParameterManager();
            StringBuilder txt = new StringBuilder();
            for (int i = 0; i < model.Count(); i++)
            {
                string name = model.GetValue(i);
                if (name != key)
                {
                    txt.Append(name + "=@" + name + ",");
                    PropertyInfo pinfo = type.GetProperty(name);
                    manager.Add("@" + name, pinfo.GetValue(model, null));
                }
            }
            txt.Remove(txt.Length - 1, 1);
            string sql = string.Format("update {0} set {1} where {2}=@{2}", tableName, txt.ToString(), key);
            PropertyInfo keyinfo = type.GetProperty(key);
            manager.Add("@" + key, keyinfo.GetValue(model, null));
            manager.SqlText = sql;
            return SqlDataAccess.ExecuteNonQuery(manager);
        }


        public static int Delete(ModelBase info)
        {
            string key = info.GetPrimaryKey();
            if (key == null || key.Length == 0)
            {
                throw new Exception("无法执行没有主键的Delete命令");
            }
            if (!info.IsValue(key))
            {
                throw new Exception("无法执行没有主键的Delete命令");
            }
            Type type = info.GetType();
            string sql = string.Format("delete {0} where {1}=@{1}", type.Name, key);
            SQLParameterManager manager = new SQLParameterManager(sql);
            PropertyInfo pinfo = type.GetProperty(key);
            manager.Add("@" + key, pinfo.GetValue(info, null));
            return SqlDataAccess.ExecuteNonQuery(manager);
        }


        public static void SelectForID(ModelBase info)
        {
            string key = info.GetPrimaryKey();
            if (string.IsNullOrEmpty(key))
            {
                throw new Exception("不存在主键,无法查询");
            }
            if (!info.IsValue(key))
            {
                throw new Exception("主键没有赋值,无法查询");
            }
            Type type = info.GetType();
            string tableName = type.Name;
            string sql = string.Format("select * from {0} where {1}=@{1}", tableName, key);
            SQLParameterManager manager = new SQLParameterManager(sql);
            PropertyInfo pinfo = type.GetProperty(key);
            manager.Add("@" + key, pinfo.GetValue(info, null));
            Select(info, manager);
        }


        public static void Select(ModelBase info, SQLParameterManager manager)
        {
            Type type = info.GetType();
            using (SqlDataReader read = SqlDataAccess.ExecuteReader(manager))
            {
                if (read.Read())
                {
                    for (int i = 0; i < read.FieldCount; i++)
                    {
                        object value = read[i];
                        if (value == DBNull.Value)
                        {
                            continue;
                        }
                        string cname = read.GetName(i);
                        foreach (PropertyInfo pinfo in type.GetProperties())
                        {
                            if (pinfo.Name.ToLower() == cname.ToLower())
                            {
                                pinfo.SetValue(info, value,null);
                            }
                        }
                    }
                }
            }
        }


        public static List<T> SelectForAll<T>() where T : ModelBase
        {
            string tableName = typeof(T).Name;
            string sql = "select * from " + tableName;
            SQLParameterManager manager = new SQLParameterManager(sql);
            return Selects<T>(manager);
        }


        public static List<T> Selects<T>(SQLParameterManager manager) where T : ModelBase
        {
            Type type = typeof(T);
            List<T> list = new List<T>();
            using (SqlDataReader read = SqlDataAccess.ExecuteReader(manager))
            {
                while (read.Read())
                {
                    T t = (T)Activator.CreateInstance(type);
                    for (int i = 0; i < read.FieldCount; i++)
                    {
                        object value = read[i];
                        if (value == DBNull.Value)
                        {
                            continue;
                        }
                        string cname = read.GetName(i);
                        foreach (PropertyInfo pinfo in type.GetProperties())
                        {
                            if (pinfo.Name.ToLower() == cname.ToLower())
                            {
                                pinfo.SetValue(t, value, null);
                            }
                        }
                    }
                    list.Add(t);
                }
            }
            return list;
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值