using System;

using System.Collections.Generic;

using System.Collections;

using System.Text;

using System.Data;

using System.Linq;

using System.Data.Linq;

using HZLinq;

using HZSoft.Data;

using System.Windows.Forms;

using LinqSql;

 

 

 

namespace {2}

{{

public partial class {1}

{{

       DataClassesDataContext db = new DataClassesDataContext (SqlConnectionString.ConnectionString);

       #region ==================== Private Field ==================================

       const string tableName = "{0}";

       bool isDisposed = false;

      string _Msg = "";

       #endregion       

       

       #region ==================== Constructed Method ============================

 

       #endregion

 

 

/// <summary>

///

/// </summary>

/// <returns></returns>

public List<{0}> GetModelList()

{{

         return db.{0}s.ToList();

}}

 

public List<{0}> GetModelList(string sbWhere, object[] paramsObj)

{{

           return db.{0}s.Where(sbWhere, paramsObj).ToList();

}}

 

 /// <summary>

/// 按字段查结果集

/// </summary>

/// <param name="colName">字段名</param>

/// <param name="colValue">字段值</param>

/// <returns></returns>

public List<{0}> GetModelListByCol(string colName, object colValue)

{{

return db.{0}s.Where(colName + ".Equals(@0)", colValue).ToList();

}}

 

 

/// <summary>

///

/// </summary>

/// <param name="id"></param>

/// <returns></returns>

public {0} GetModelByPK(int id)

{{          

    try

     {{

             return db.{0}s.Where(w => w.ID.Equals(id)).FirstOrDefault();

}}

               catch (Exception ex)

               {{

                   throw ex;

               }}

}}

 

 

 /// <summary>

/// 按字段查结果集

/// </summary>

/// <param name="colName">字段名</param>

/// <param name="colValue">字段值</param>

/// <returns></returns>

public {0} GetModelByCol(string colName, object colValue)

{{

return db.{0}s.Where(colName + ".Equals(@0)", colValue).FirstOrDefault();

}}

  

 

/// <summary>

/// 判断值是否存在

/// </summary>

/// <param name="id"></param>

/// <returns></returns>

public bool IsExists(int id)

{{

return db.{0}s.Any(a => a.ID.Equals(id));

}} 

 

 

/// <summary>

/// 查询是否有重复值,在修改时判断是否重复用

/// </summary>

/// <param name="colName">数据库字段名</param>

/// <param name="paramsObj">输入的参数</param>

/// <param name="code">该条记录的id</param>

/// <returns></returns>

public bool IsExistsByColName(string colName, object colValue, int id)

{{

    object[] obj = {{ colValue, id }};

    if (db.{0}s.Where("( " + colName + ".Equals(@0) ) && ( !ID.Equals(@1) )", obj).Count() > 0)

        return true;

    else

        return false;

}}

 

/// <summary>

/// 查询是否有重复值

/// </summary>

/// <param name="colName">数据库字段名</param>

/// <param name="paramsObj">输入的参数</param>

/// <param name="code">该条记录的id</param>

/// <returns></returns>

public bool IsExistsByColName(string colName, object colValue)

{{

    object[] obj = {{ colValue}};

    if (db.{0}s.Where("( " + colName + ".Equals(@0) ) ", obj).Count() > 0)

        return true;

    else

        return false;

}}

 

       

 /// <summary>

/// 增加记录

/// </summary>

/// <param name="localTable"></param>

/// <returns></returns>

public bool InsertModel({0}  model)

{{

   try

       {{

              model.Detach();

               db.{0}s.InsertOnSubmit(model);

               db.SubmitChanges();

 

               return true;

       }}

       catch (System.Data.SqlClient.SqlException ex)

       {{

              if (ex.Number == 2627)

               {{

                    _Msg = "插入数据重复";

MessageBox.Show(_Msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

 

               }}

               else

               {{

                   #region 日志          

                   HZLinq.HZ_Log modelLog = new HZLinq.HZ_Log();

                   modelLog.Type = DataDefine.LOGTYPE.异常.GetHashCode();

                   modelLog.UserCode = HZLinq.HZDataValue.ModelUser.LoginCode;

                   modelLog.REMARK = ex.Message;

                   modelLog.TableName = tableName;

                   modelLog.Date = DateTime.Now;

                   HZBll.BllLog.InsertModel(modelLog);

                   _Msg = "操作失败,失败原-因请查看日志ID:" + modelLog.ID.ToString();     

  MessageBox.Show(_Msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

 

                   #endregion                                   

               }}

               return false;

           }}

           catch (Exception ex)

           {{

               #region 日志

               HZLinq.HZ_Log modelLog = new HZLinq.HZ_Log();

               modelLog.Type = DataDefine.LOGTYPE.异常.GetHashCode();

               modelLog.UserCode = HZLinq.HZDataValue.ModelUser.LoginCode;

               modelLog.REMARK = ex.Message;

               modelLog.TableName = tableName;

               modelLog.Date = DateTime.Now;

               HZBll.BllLog.InsertModel(modelLog);

               _Msg = "操作失败,失败原因请查看日志ID:" + modelLog.ID.ToString();

MessageBox.Show(_Msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

 

               #endregion  

               return false;

           }}

       }} 

 

 

       /// <summary>

       /// 修改记录

       /// </summary>

       /// <param name="model"></param>

       /// <returns></returns>

       public bool UpdateModel( {0} model)

       {{

           

               try

               {{

                   model.Detach();

                   db.{0}s.Attach(model, true);

                   db.SubmitChanges();

 

                   return true;

               }}

               catch (System.Data.Linq.ChangeConflictException ex)

               {{

                   foreach (System.Data.Linq.ObjectChangeConflict occ in db.ChangeConflicts)

                   {{

                       //

                       occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);

                   }}

                   db.SubmitChanges();

                   return true;

               }}

               catch (Exception ex)

               {{

                     #region 日志

                HZLinq.HZ_Log modelLog = new HZLinq.HZ_Log();

                modelLog.Type = DataDefine.LOGTYPE.异常.GetHashCode();

                modelLog.UserCode = HZLinq.HZDataValue.ModelUser.LoginCode;

               modelLog.REMARK = ex.Message;

                modelLog.TableName = tableName;

                modelLog.Date = DateTime.Now;

                HZBll.BllLog.InsertModel(modelLog);

                _Msg = "操作失败,失败原因请查看日志ID:" + modelLog.ID.ToString();

MessageBox.Show(_Msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

 

                #endregion  

                return false

               }}

           

       }} 

 

 

       /// <summary>

       /// 修改记录

       /// </summary>

       /// <returns></returns>

       public bool UpdateModel()

       {{

          

               try

               {{

 

                   db.SubmitChanges(); 

                   return true;

               }}

               catch (System.Data.Linq.ChangeConflictException ex)

               {{

                   foreach (System.Data.Linq.ObjectChangeConflict occ in db.ChangeConflicts)

                   {{

                       //

                       occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);

                   }}

                   db.SubmitChanges();

                   return true;

               }}

               catch (Exception ex)

               {{

                     #region 日志

                HZLinq.HZ_Log modelLog = new HZLinq.HZ_Log();

                modelLog.Type = DataDefine.LOGTYPE.异常.GetHashCode();

                modelLog.UserCode = HZLinq.HZDataValue.ModelUser.LoginCode;

               modelLog.REMARK = ex.Message;

                modelLog.TableName = tableName;

                modelLog.Date = DateTime.Now;

                HZBll.BllLog.InsertModel(modelLog);

                _Msg = "操作失败,失败原因请查看日志ID:" + modelLog.ID.ToString();

MessageBox.Show(_Msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

 

                #endregion  

                return false;

 

               }}

           

       }} 

 

 

       /// <summary>

       /// 删除记录

       /// </summary>

       /// <param name="model"></param>

       /// <param name="Msg"></param>

       /// <returns></returns>

       public bool DeleteModel( {0} model)

       {{

           

               try

               {{

                   model.Detach();

                   db.{0}s.Attach(model, true);

                   db.{0}s.DeleteOnSubmit(model);

                   db.SubmitChanges();

 

                   return true;

               }}

               catch (System.Data.SqlClient.SqlException ex)

               {{

                   if (ex.Number == 547)

                   {{

                       throw ex;

                   }}

                   else

                   {{

                        #region 日志

                HZLinq.HZ_Log modelLog = new HZLinq.HZ_Log();

                modelLog.Type = DataDefine.LOGTYPE.异常.GetHashCode();

                modelLog.UserCode = HZLinq.HZDataValue.ModelUser.LoginCode;

               modelLog.REMARK = ex.Message;

                modelLog.TableName = tableName;

                modelLog.Date = DateTime.Now;

                HZBll.BllLog.InsertModel(modelLog);

                _Msg = "操作失败,失败原因请查看日志ID:" + modelLog.ID.ToString();

MessageBox.Show(_Msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

 

                #endregion  

                return false;

                   }}

               }}

               catch (Exception ex)

               {{

                    #region 日志

                HZLinq.HZ_Log modelLog = new HZLinq.HZ_Log();

                modelLog.Type = DataDefine.LOGTYPE.异常.GetHashCode();

                modelLog.UserCode = HZLinq.HZDataValue.ModelUser.LoginCode;

                modelLog.REMARK = ex.Message;

                modelLog.TableName = tableName;

                 modelLog.Date = DateTime.Now;

                HZBll.BllLog.InsertModel(modelLog);

                _Msg = "操作失败,失败原因请查看日志ID:" + modelLog.ID.ToString();

MessageBox.Show(_Msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

 

                #endregion  

                return false;

               }}

           

       }}      

 

 

      

   }}

}}