sql script code:
CREATE TABLE DuCardType ( CardTypeId INT IDENTITY(1,1) PRIMARY KEY, CardTypeName NVARCHAR(100) NOT NULL, --卡類名稱 CardTypeColor NVARCHAR(50) NOT NULL --卡顏色(或樣本) ) GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Insert_DuCardType') DROP PROCEDURE proc_Insert_DuCardType GO CREATE PROCEDURE proc_Insert_DuCardType ( @CardTypeName NVarChar(200), @CardTypeColor NVarChar(100) ) AS IF NOT EXISTS (SELECT * FROM DuCardType WHERE [CardTypeName]=@CardTypeName) BEGIN INSERT INTO DuCardType ( [CardTypeName] , [CardTypeColor] ) VALUES ( @CardTypeName , @CardTypeColor ) END GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Insert_DuCardTypeOutput') DROP PROCEDURE proc_Insert_DuCardTypeOutput GO CREATE PROCEDURE proc_Insert_DuCardTypeOutput ( @CardTypeName NVarChar(200), @CardTypeColor NVarChar(100), @CardTypeId int output ) AS IF NOT EXISTS (SELECT * FROM DuCardType WHERE [CardTypeName]=@CardTypeName) BEGIN INSERT INTO DuCardType ( [CardTypeName] , [CardTypeColor] ) VALUES ( @CardTypeName , @CardTypeColor ) select @CardTypeId=@@IDENTITY END GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Update_DuCardType') DROP PROCEDURE proc_Update_DuCardType GO CREATE PROCEDURE proc_Update_DuCardType ( @CardTypeId Int, @CardTypeName NVarChar(200), @CardTypeColor NVarChar(100) ) AS IF NOT EXISTS (SELECT * FROM DuCardType WHERE [CardTypeName]=@CardTypeName) BEGIN UPDATE DuCardType SET [CardTypeName]=@CardTypeName , [CardTypeColor]=@CardTypeColor where [CardTypeId]=@CardTypeId END GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Update_DuCardTypeField') DROP PROCEDURE proc_Update_DuCardTypeField GO CREATE PROCEDURE proc_Update_DuCardTypeField ( @CardTypeId Int, @FieldValue NVARCHAR(1500) ) AS DECLARE @sql NVARCHAR(4000) SET @sql='update DuCardType set '+@FieldValue+' WHERE CardTypeId ='+ @CardTypeId EXEC(@sql) GO IF EXISTS (select * from sysobjects where [name] = 'proc_Delete_DuCardType') DROP PROCEDURE proc_Delete_DuCardType GO CREATE PROCEDURE proc_Delete_DuCardType ( @CardTypeId Int ) as DELETE DuCardType WHERE CardTypeId = @CardTypeId GO IF EXISTS (select * from sysobjects where [name] = 'proc_Delete_DuCardTypeId') DROP PROCEDURE proc_Delete_DuCardTypeId GO CREATE PROCEDURE proc_Delete_DuCardTypeId ( @CardTypeId varchar(2000) ) as DECLARE @strsql varchar(3000) SET @strsql='DELETE DuCardType WHERE CardTypeId in('+@CardTypeId+')' EXEC(@strsql) GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_DuCardType') DROP PROCEDURE proc_Select_DuCardType GO CREATE PROCEDURE proc_Select_DuCardType ( @CardTypeId Int ) AS SELECT * FROM DuCardType WHERE CardTypeId = @CardTypeId GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_DuCardTypeExists') DROP PROCEDURE proc_Select_DuCardTypeExists GO CREATE PROCEDURE proc_Select_DuCardTypeExists ( @CardTypeId Int ) AS SELECT count(1) as H FROM DuCardType WHERE CardTypeId = @CardTypeId GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_DuCardTypeCount') DROP PROCEDURE proc_Select_DuCardTypeCount GO CREATE PROCEDURE proc_Select_DuCardTypeCount ( @where NVARCHAR(1000) ) AS DECLARE @sql NVARCHAR(4000) SET @sql='select count(*) as H from DuCardType ' IF @where<>'' SET @sql=@sql+@where EXEC(@sql) GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_DuCardTypeTitle') DROP PROCEDURE proc_Select_DuCardTypeTitle GO CREATE PROCEDURE proc_Select_DuCardTypeTitle ( @FieldName NVARCHAR(100), @CardTypeId Int ) AS DECLARE @sql NVARCHAR(4000) SET @sql='select top 1 '+@FieldName+' from DuCardType WHERE CardTypeId ='+ CAST(@CardTypeId AS VARCHAR(50)) EXEC(@sql) GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_DuCardTypeFuzzySearch') DROP PROCEDURE proc_Select_DuCardTypeFuzzySearch GO CREATE PROCEDURE proc_Select_DuCardTypeFuzzySearch ( @FieldList NVARCHAR(1000), @where NVARCHAR(2000) ) AS DECLARE @sql NVARCHAR(4000) IF(@where<>'') SET @where=' WHERE '+@where IF(@FieldList='') SET @FieldList=' * ' SET @sql='select '+@FieldList+' from DuCardType '+@where EXEC(@sql) GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_DuCardTypeAll') DROP PROCEDURE proc_Select_DuCardTypeAll GO CREATE PROCEDURE proc_Select_DuCardTypeAll AS SELECT * FROM DuCardType GO
DAL:
/// <summary> /// DuCardType表數據訪問層 ///生成時間2016-6-21 10:28:44 ///塗聚文(Geovin Du) ///</summary> public class DuCardTypeDAL { ///<summary> /// 追加記錄 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<returns>返回添加的記錄條數</returns> public int InsertDuCardType(DuCardTypeInfo duCardType) { int ret = 0; try { SqlParameter[] par = new SqlParameter[]{ new SqlParameter("@CardTypeName",SqlDbType.NVarChar,200), new SqlParameter("@CardTypeColor",SqlDbType.NVarChar,100), }; par[0].Value = duCardType.CardTypeName; par[1].Value = duCardType.CardTypeColor; ret = DBHelper.ExecuteSql("proc_Insert_DuCardType", CommandType.StoredProcedure, par); } catch (SqlException ex) { throw ex; } return ret; } ///<summary> /// 追加記錄返回值 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<param name="CardTypeId">返回參數:CardTypeId</param> ///<returns>返回是否添加的個數</returns> public int InsertDuCardTypeOutput (DuCardTypeInfo duCardType,out int cardTypeId) { int ret = 0; cardTypeId= 0; try { SqlParameter[] par = new SqlParameter[]{ new SqlParameter("@CardTypeName",SqlDbType.NVarChar,200), new SqlParameter("@CardTypeColor",SqlDbType.NVarChar,100), new SqlParameter("@CardTypeId",SqlDbType.Int,8), }; par[0].Value = duCardType.CardTypeName; par[1].Value = duCardType.CardTypeColor; par[2].Direction = ParameterDirection.Output; ret = DBHelper.ExecuteSql("proc_Insert_DuCardTypeOutput", CommandType.StoredProcedure, par); if(ret>0) { cardTypeId=(int)par[2].Value; } } catch (SqlException ex) { throw ex; } return ret; } ///<summary> ///修改記錄 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<returns>返回修改的多少記錄數</returns> public int UpdateDuCardType(DuCardTypeInfo duCardType) { int ret = 0; try { SqlParameter[] par = new SqlParameter[]{ new SqlParameter("@CardTypeId",SqlDbType.Int,4), new SqlParameter("@CardTypeName",SqlDbType.NVarChar,200), new SqlParameter("@CardTypeColor",SqlDbType.NVarChar,100), }; par[0].Value = duCardType.CardTypeId; par[1].Value = duCardType.CardTypeName; par[2].Value = duCardType.CardTypeColor; ret = DBHelper.ExecuteSql("proc_Update_DuCardType", CommandType.StoredProcedure, par); } catch (SqlException ex) { throw ex; } return ret; } ///<summary> /// 刪除記錄 ///</summary> ///<param name="cardTypeId">輸入參數:CardTypeId</param> ///<returns>返回刪除記錄條數</returns> public bool DeleteDuCardType(int cardTypeId) { bool ret = false; try { SqlParameter par =new SqlParameter("@CardTypeId",cardTypeId); int temp = 0 ; temp = DBHelper.ExecuteSql("proc_Delete_DuCardType", CommandType.StoredProcedure, par); if(temp!=0) { ret = true; } } catch (SqlException ex) { throw ex; } return ret; } ///<summary> /// 刪除多條記錄 ///</summary> ///<param name="cardTypeId">輸入參數:CardTypeId</param> ///<returns>返回刪除多少記錄</returns> public bool DeleteDuCardTypeId(string cardTypeId) { bool ret = false; try { SqlParameter par =new SqlParameter("@CardTypeId",cardTypeId); int temp = 0 ; temp = DBHelper.ExecuteSql("proc_Delete_DuCardTypeId", CommandType.StoredProcedure, par); if(temp!=0) { ret = true; } } catch (SqlException ex) { throw ex; } return ret; } ///<summary> /// 查詢記錄 ///</summary> ///<param name="cardTypeId">輸入參數:CardTypeId</param> ///<returns>返回DuCardTypeInfo</returns> public DuCardTypeInfo SelectDuCardType(int cardTypeId) { DuCardTypeInfo duCardType = null; try { SqlParameter par =new SqlParameter("@CardTypeId",cardTypeId); using (SqlDataReader reader = DBHelper.GetReader("proc_Select_DuCardType", CommandType.StoredProcedure, par)) { if (reader.Read()) { duCardType = new DuCardTypeInfo(); duCardType.CardTypeId =(!DBNull.Equals(reader["CardTypeId"],null))? (int) reader["CardTypeId"]:0; duCardType.CardTypeName =(!DBNull.Equals(reader["CardTypeName"],null))? (string) reader["CardTypeName"].ToString():""; duCardType.CardTypeColor =(!DBNull.Equals(reader["CardTypeColor"],null))? (string) reader["CardTypeColor"].ToString():""; } } } catch (SqlException ex) { throw ex; } return duCardType; } ///<summary> /// 查詢所有記錄 ///</summary> ///<param name="cardTypeId">無輸入參數</param> ///<returns>返回表所有記錄(List)DuCardTypeInfo</returns> public List<DuCardTypeInfo> SelectDuCardTypeAll() { List<DuCardTypeInfo> list = new List<DuCardTypeInfo>(); DuCardTypeInfo duCardType = null; try { using (SqlDataReader reader = DBHelper.GetReader("proc_Select_DuCardTypeAll", CommandType.StoredProcedure, null)) { while (reader.Read()) { duCardType = new DuCardTypeInfo(); duCardType.CardTypeId =(!DBNull.Equals(reader["CardTypeId"],null))? (int) reader["CardTypeId"]:0; duCardType.CardTypeName =(!DBNull.Equals(reader["CardTypeName"],null))? (string) reader["CardTypeName"].ToString():""; duCardType.CardTypeColor =(!DBNull.Equals(reader["CardTypeColor"],null))? (string) reader["CardTypeColor"].ToString():""; list.Add(duCardType); } } } catch (SqlException ex) { throw ex; } return list; } ///<summary> /// 查詢所有記錄 ///</summary> ///<param name="cardTypeId">無輸入參數</param> ///<returns>返回(DataTable)DuCardType表所有記錄</returns> public DataTable SelectDuCardTypeDataTableAll() { DataTable dt = new DataTable(); try { using (DataTable reader = DBHelper.GetTable("proc_Select_DuCardTypeAll", CommandType.StoredProcedure, null)) { dt = reader; } } catch (SqlException ex) { throw ex; } return dt; } /// <summary> /// SQL script查詢分頁 /// </summary> /// <param name="pageSize">每頁頁數</param> /// <param name="currentPage">當前頁碼</param> /// <param name="strWhere">查詢的條件</param> /// <param name="filedOrder">排序字段</param> /// <param name="recordCount">每頁的記錄數</param> /// <returns></returns> public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string filedOrder, out int recordCount) { int topNum = pageSize * currentPage; StringBuilder strSql = new StringBuilder(); strSql.Append("select * FROM DuCardType"); if (strWhere.Trim() !="") { strSql.Append(" where " + strWhere); } recordCount = Convert.ToInt32(DBHelper.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString()))); return DBHelper.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, currentPage, strSql.ToString(), filedOrder)); } /// <summary> /// 模糊查詢 /// </summary> /// <param name="filedlist">顯示字段列表</param> /// <param name="strkey">輸入的關鍵字</param> /// <returns></returns> public DataTable GetDataTableDuCardTypeFuzzySearch(string filedlist, string strkey) { DataTable dt = new DataTable(); try { SqlParameter[] par = new SqlParameter[]{ new SqlParameter("@FieldList",SqlDbType.NVarChar,2000),//可以考慮類型 new SqlParameter("@where",SqlDbType.NVarChar,1000), }; par[0].Value = filedlist; par[1].Value = StringConvert.getStrWhere("CardTypeName,CardTypeColor",strkey);//獲取字段為字符串的字段列表(varchar,nvarchar,text,ntext) using (DataTable reader = DBHelper.GetTable("proc_Select_DuCardTypeFuzzySearch", CommandType.StoredProcedure, par)) { dt = reader; } } catch (SqlException ex) { throw ex; } return dt; } /// <summary> /// 是否存在該記錄 /// </summary> /// <param name="Id"></param> /// <returns></returns> public bool Exists(int Id) { bool isok = false; int count = 0; try { SqlParameter par = new SqlParameter("@Id", Id); using (SqlDataReader reader = DBHelper.GetReader("proc_Select_DuCardTypeExists", CommandType.StoredProcedure, par)) { if (reader.Read()) { count = (!DBNull.Equals(reader["H"], null)) ? (int)reader["H"] : 0; if (count > 0) isok = true; } } } catch (SqlException ex) { throw ex; } return isok; } /// <summary> /// 返回數據總數 /// </summary> /// <param name="strWhere">查詢條件</param> /// <returns></returns> public int GetCount(string where) { int count = 0; try { SqlParameter par = new SqlParameter("@where", where); using (SqlDataReader reader = DBHelper.GetReader("proc_Select_DuCardTypeCount", CommandType.StoredProcedure, par)) { if (reader.Read()) { count = (!DBNull.Equals(reader["H"], null)) ? (int)reader["H"] : 0; } } } catch (SqlException ex) { throw ex; } return count; } /// <summary> /// 更新一列數據 /// </summary> /// <param name="Id"></param> /// <param name="strValue">字段名=值</param> /// <returns></returns> public int UpdateField(int Id, string fieldValue) { int ret = 0; try { SqlParameter[] par = new SqlParameter[]{ new SqlParameter("@Id",SqlDbType.Int,4), new SqlParameter("@FieldValue",SqlDbType.NVarChar,2000), }; par[0].Value = Id; par[1].Value = fieldValue; ret = DBHelper.ExecuteSql("proc_Update_DuCardTypeField", CommandType.StoredProcedure, par); } catch (SqlException ex) { throw ex; } return ret; } /// <summary> /// 返回指字字段的字串 /// </summary> /// <param name="Id"></param> /// <param name="fieldName">字段名</param> /// <returns></returns> public string GetTitle(int Id, string fieldName) { string title = string.Empty; try { SqlParameter[] par = new SqlParameter[]{ new SqlParameter("@FieldName",SqlDbType.NVarChar,2000), new SqlParameter("@Id",SqlDbType.Int,4), }; par[0].Value = fieldName; par[1].Value = Id; using (SqlDataReader reader = DBHelper.GetReader("proc_Select_DuConfigTitle", CommandType.StoredProcedure, par)) { title =(!DBNull.Equals(reader[0],null))? (string) reader[0]:""; } } catch (SqlException ex) { throw ex; } return title; } /// <summary> /// 存儲過程分頁 /// </summary> /// <param name="strwhere">Where條件</param> /// <param name="aecdesc">排序字段</param> /// <param name="pageIndex">開始頁碼</param> /// <param name="pageSize">每頁頁數</param> /// <param name="RecordCount">輸出總頁數</param> /// <returns></returns> public DataTable GetDataPage(string strwhere,string aecdesc, int pageIndex, int pageSize, out int RecordCount) { DataTable dt = new DataTable(); try { SqlParameter[] par = new SqlParameter[]{ new SqlParameter("@Table",SqlDbType.NVarChar,400), //表名 new SqlParameter("@TIndex",SqlDbType.NVarChar,1000),//主鍵,可以帶表頭 new SqlParameter("@Column",SqlDbType.NVarChar,200),//讀取字段 new SqlParameter("@Sql",SqlDbType.NVarChar,1600),//Where條件 new SqlParameter("@PageIndex",SqlDbType.Int, 8),//開始頁碼 new SqlParameter("@PageSize",SqlDbType.Int, 8),//頁大小 new SqlParameter("@Sort",SqlDbType.NVarChar,1600),//排序字段 }; if (string.IsNullOrEmpty(strwhere)) { strwhere = " 1=1 "; } par[0].Value = "DuCardType"; //表名 par[1].Value = "CardTypeId"; par[2].Value = " * "; par[3].Value = strwhere;//查詢條件 par[4].Value = pageIndex; par[5].Value = pageSize; par[6].Value = aecdesc; using (DataTable reader = DBHelper.GetTable("GetPagedRecordFor2005_2008", CommandType.StoredProcedure, par)) { dt = reader; RecordCount = dt.Rows.Count; } } catch (SqlException ex) { RecordCount = 0; throw ex; } return dt; } }
DAO:
/// <summary> /// DuCardType表DAO ///生成時間2016-6-21 15:41:47 ///塗聚文(Geovin Du) ///</summary> [Spring.Stereotype.Repository] public class DuCardTypeDao : INHibernateSessionFactory { //從NHibernate SessionFactory數據訪問DAO中獲取DuCardType表的Dao數據訪問接口實例 public NHibernate.ISessionFactory SessionFactory { set; get; } public DuCardTypeDao() { //加載 //var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml"); //sessionFactory = cfg.BuildSessionFactory(); } ///<summary> /// 追加記錄 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<returns>返回添加的記錄條數</returns> public int InsertDuCardType(DuCardType duCardType) { int saveid = 0; //DuCardType info = null; NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); ITransaction tran = session.BeginTransaction(); try { if (!object.Equals(session, null)) { //2. session.Save(duCardType); //1 //string sql = @"exec GetDepartmentId @Id=:Id";// @"exec GetDepartmentId :Id"; //IQuery query = session.CreateSQLQuery(sql) //CreateSQLQuery(sql) //GetNamedQuery("GetDepartmentId") // .SetParameter("Id", 1) // .SetResultTransformer( // Transformers.AliasToBean(typeof(DuCardTypeInfo))); //var clients = query.UniqueResult();// query.List<Department>().ToList(); //不能強制轉化 //info = (DuCardTypeInfo)clients; //無法將類型為“System.Object[]”的對象強制轉換為類型 //關聯的數據也查出來了 //string sql = @"exec proc_Insert_DuCardType @CardTypeName = :CardTypeName,@CardTypeColor = :CardTypeColor ";// @"exec GetDepartmentId :Id"; //IQuery query = session.CreateSQLQuery(sql) //CreateSQLQuery(sql) //GetNamedQuery("GetDepartmentId") // .SetParameter("CardTypeName", duCardType.CardTypeName) // .SetParameter("CardTypeColor", duCardType.CardTypeColor) // .SetResultTransformer( // Transformers.AliasToBean(typeof(DuCardType))); //IQuery query = session.CreateSQLQuery(sql) // .SetParameter("CardTypeName", duCardType.CardTypeName) // .SetParameter("CardTypeColor", duCardType.CardTypeColor); //query.SetParameter("CardTypeName", duCardType.CardTypeName, NHibernateUtil.String); //query.SetParameter("CardTypeColor", duCardType.CardTypeColor, NHibernateUtil.String); //query.ExecuteUpdate(); //IQuery query = session.CreateSQLQuery(sql) // .SetString("CardTypeName", duCardType.CardTypeName) // .SetString("CardTypeColor", duCardType.CardTypeColor); //.ExecuteUpdate(); //IQuery query = session.CreateSQLQuery("UPDATE DuCardType as c set c.CardTypeName =?,c.CardTypeColor=? where c.CardTypeId = ? ") // .SetString(0, duCardType.CardTypeName) // .SetString(1, duCardType.CardTypeColor) // .executeUpdate(); // IQuery query = session.CreateSQLQuery(@"insert into DuCardType (CardTypeName, CardTypeColor) // values (:cardTypeName, :cardTypeColor)") // .SetParameter("cardTypeName", duCardType.CardTypeName, NHibernateUtil.String) // .SetParameter("cardTypeColor", duCardType.CardTypeColor, NHibernateUtil.String); // IQuery query = session.CreateSQLQuery(@"insert into DuCardType (CardTypeName, CardTypeColor) vlaues('" + duCardType.CardTypeName + "','" + duCardType.CardTypeColor + "')"); //int result = query.ExecuteUpdate(); // saveid = query.ExecuteUpdate(); tran.Commit(); saveid = 1; } } catch (MappingException ex) { saveid = 0; tran.Rollback(); ex.Message.ToString(); } return saveid; //return dal.InsertDuCardType(duCardType); } ///<summary> /// 存儲過程追加記錄 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<returns>返回添加的記錄條數</returns> public int InsertDuCardTypeProc(DuCardType duCardType) { int ret = 0; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), new SqlParameter(), }; par[0].ParameterName = "@CardTypeName"; par[0].Value = duCardType.CardTypeName; par[1].ParameterName = "@CardTypeColor"; par[1].Value = duCardType.CardTypeColor; ret = DBHelper.ExecuteNonQuery(SessionFactory, CommandType.StoredProcedure, "proc_Insert_DuCardType", par); } catch (NotImplementedException ex) { ex.Message.ToString(); } return ret; } ///<summary> /// 追加記錄返回值 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<param name="CardTypeId">返回參數:CardTypeId</param> ///<returns>返回是否添加的個數</returns> public int InsertDuCardTypeOutput(DuCardType cardType, out int cardTypeId) { int sid = 1; NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); ITransaction tran = session.BeginTransaction(); try { session.Save(cardType); cardTypeId = cardType.CardTypeId; tran.Commit(); } catch (MappingException ex) { sid = 0; tran.Rollback(); throw ex; } return sid; //return dal.InsertDuCardTypeOutput(duCardType,out cardTypeId); } ///<summary> /// 存儲過程追加記錄返回值 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<param name="CardTypeId">返回參數:CardTypeId</param> ///<returns>返回是否添加的個數</returns> public int InsertDuCardTypeOutputProc(DuCardType duCardType, out int cardTypeId) { int rets = 0; cardTypeId = 0; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), new SqlParameter(), new SqlParameter(), }; par[0].ParameterName = "@CardTypeName"; par[0].Value = duCardType.CardTypeName; par[1].ParameterName = "@CardTypeColor"; par[1].Value = duCardType.CardTypeColor; par[2].ParameterName = "@CardTypeId"; par[2].Size = 10; par[2].DbType = DbType.Int32; par[2].Direction = ParameterDirection.Output; rets = DBHelper.ExecuteNonQuery(SessionFactory, CommandType.StoredProcedure, "proc_Insert_DuCardTypeOutput", par); if (rets > 0) { cardTypeId = int.Parse(par[2].Value.ToString()); } } catch (NotImplementedException ex) { rets = 0; ex.Message.ToString(); } return rets; } ///<summary> ///修改記錄 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<returns>返回修改的多少記錄數</returns> public int UpdateDuCardType(DuCardType duCardType) { int sid = 1; NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); ITransaction tran = session.BeginTransaction(); try { session.Update(duCardType); tran.Commit(); } catch (Exception ex) { sid = 0; tran.Rollback(); throw ex; } return sid; //return dal.UpdateDuCardType(duCardType); } ///<summary> ///存儲過程修改記錄 ///</summary> ///<param name="DuCardType">輸入參數:DuCardTypeInfo</param> ///<returns>返回修改的多少記錄數</returns> public int UpdateDuCardTypeProc(DuCardType duCardType) { int rets = 0; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), new SqlParameter(), new SqlParameter(), }; par[0].ParameterName = "@CardTypeId"; par[0].Value = duCardType.CardTypeId; par[1].ParameterName = "@CardTypeName"; par[1].Value = duCardType.CardTypeName; par[2].ParameterName = "@CardTypeColor"; par[2].Value = duCardType.CardTypeColor; rets = DBHelper.ExecuteNonQuery(SessionFactory, CommandType.StoredProcedure, "proc_Update_DuCardType", par); } catch (NotImplementedException ex) { rets = 0; ex.Message.ToString(); } return rets; } /// <summary> /// 查詢所有 /// </summary> /// <returns></returns> public IQueryable<DuCardType> GetAllCardTypes() { NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); //if (!object.Equals(session, null)) //{ // string s = ""; //} var query = session.Query<DuCardType>(); var result = from cardType in query orderby cardType.CardTypeName //名字排序 select cardType; return result; } /// <summary> /// Sql查詢 /// </summary> /// <param name="id"></param> /// <returns></returns> public IList<DuCardType> CardTypeSql(int id) { NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); IList<DuCardType> card = new List<DuCardType>(); if (!object.Equals(session, null)) { //寫法1 //return card = session.CreateQuery("from DuCardType c where c.CardTypeName='" + firstname + "'") // .List<DuCardType>(); //寫法2:位置型參數 //card = session.CreateQuery("from DuCardType c where c.CardTypeId=:CardTypeId") // .SetInt32("CardTypeId", id) // .List<DuCardType>(); //寫法4. //var sql = "select * from DuCardType"; //var query = session.CreateSQLQuery(sql) //.AddScalar("CardTypeId", NHibernateUtil.Int32) //.AddScalar("CardTypeName", NHibernateUtil.String) //.AddScalar("CardTypeColor", NHibernateUtil.String) //.SetResultTransformer(Transformers.AliasToBean<DuCardType>()); //card = query.List<DuCardType>(); //寫法3:命名型參數(推薦) var query = session.CreateSQLQuery("Select * FROM DuCardType where CardTypeId=:CardTypeId") .SetInt32("CardTypeId", id) .SetResultTransformer(Transformers.AliasToBean(typeof(DuCardType))); card = query.List<DuCardType>(); } return card; } /// <summary> /// 存儲過程查詢 /// </summary> /// <param name="id"></param> /// <returns></returns> public IList<DuCardType> CardTypeProcedures(int id) { //讀到數據 IList<DuCardType> li = null; NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); try { IQuery query = session.GetNamedQuery("proc_Select_DuCardType") .SetParameter("CardTypeId", id) .SetResultTransformer( Transformers.AliasToBean(typeof(DuCardType))); li = query.List<DuCardType>(); //var executor = new HibernateStoredProcedureExecutor(session); //var lie = executor.ExecuteStoredProcedure<DuCardType>( // "proc_Select_DuCardType", //find no // new[] // { // new SqlParameter("CardTypeId", id), // //new SqlParameter("startDate", startDate), // // new SqlParameter("endDate", endDate), // }); //li = lie.ToList(); //return li; //string sql = @"exec proc_Select_DuCardType :CardTypeId"; //IQuery query = session.CreateSQLQuery(sql) // .SetInt32("CardTypeId", id); //var list = query.List(); //foreach (object[] item in list) //{ // li = new List<DuCardType>(); // var cardType = new DuCardType(); // cardType.CardTypeId = (int)item[0]; // cardType.CardTypeName = (string)item[1]; // cardType.CardTypeColor = item[2].ToString(); // li.Add(cardType); //} ////query = query.SetResultTransformer(Transformers.AliasToBean<OrderDto>()); ////var list2 = query.List<OrderDto>(); //var result = from cardType in li // orderby cardType.CardTypeName //名字排序 // select cardType; } catch (MappingException ex) { ex.Message.ToString(); } return li; } /// <summary> /// Linq查詢 /// </summary> /// <param name="id"></param> /// <returns></returns> public IQueryable<DuCardType> getCardTypeID(int id) { NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); var query = session.Query<DuCardType>(); var result = from cardType in query where cardType.CardTypeId == id select cardType; return result; } ///<summary> /// 刪除記錄 ///</summary> ///<param name="cardTypeId">輸入參數:CardTypeId</param> ///<returns>返回刪除記錄條數</returns> public bool DeleteDuCardType(int cardTypeId) { bool isok = false; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), }; par[0].ParameterName = "@CardTypeId"; par[0].Value = cardTypeId; isok = DBHelper.ExecuteNonQuery(SessionFactory, CommandType.StoredProcedure, "proc_Delete_DuCardType"); } catch (NotImplementedException ex) { ex.Message.ToString(); } return isok; //return dal.DeleteDuCardType(cardTypeId); } /// <summary> /// 刪除 /// </summary> /// <param name="customer"></param> /// <returns></returns> public int Dell(DuCardType cardType) { int sid = 1; NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); ITransaction tran = session.BeginTransaction(); try { session.Delete(cardType); tran.Commit(); } catch (Exception ex) { sid = 0; tran.Rollback(); throw ex; } return sid; } ///<summary> /// 刪除多條記錄 ///</summary> ///<param name="cardTypeId">輸入參數:CardTypeId</param> ///<returns>返回刪除多少記錄</returns> public int DeleteDuCardTypeId(string cardTypeId) { int isok = 0; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), }; par[0].ParameterName = "@CardTypeId"; par[0].Value = cardTypeId; isok = DBHelper.ExecuteNonQuery(SessionFactory, CommandType.StoredProcedure, "proc_Delete_DuCardTypeId",par); } catch (NotImplementedException ex) { ex.Message.ToString(); } return isok; //return dal.DeleteDuCardTypeId(cardTypeId); } ///<summary> /// 查詢記錄 ///</summary> ///<param name="cardTypeId">輸入參數:CardTypeId</param> ///<returns>返回DuCardTypeInfo</returns> public DuCardType SelectDuCardType(int cardTypeId) { NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); //var query = session.CreateQuery(""); var query =session.Query<DuCardType>() ;// ;session.Get<DuCardType>(cardTypeId) var result = from cardType in query where cardType.CardTypeId == cardTypeId select cardType; List<DuCardType> ty = query.ToList<DuCardType>(); return ty[0]; //return dal.SelectDuCardType(cardTypeId); } ///<summary> /// 查詢記錄 ///</summary> ///<param name="cardTypeId">輸入參數:CardTypeId</param> ///<returns>返回DuCardTypeInfo</returns> public DuCardType SelectDuCardTypePrce(int cardTypeId) { DuCardType duCardType = null; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), }; par[0].ParameterName = "@CardTypeId"; par[0].Value = cardTypeId; using (IDataReader reader = DBHelper.ExecuteReader(SessionFactory, CommandType.StoredProcedure, "proc_Delete_DuCardTypeId", par)) { while (reader.Read()) { duCardType = new DuCardType(); duCardType.CardTypeId = (!DBNull.Equals(reader["CardTypeId"], null)) ? (int)reader["CardTypeId"] : 0; duCardType.CardTypeName = (!DBNull.Equals(reader["CardTypeName"], null)) ? (string)reader["CardTypeName"].ToString() : ""; duCardType.CardTypeColor = (!DBNull.Equals(reader["CardTypeColor"], null)) ? (string)reader["CardTypeColor"].ToString() : ""; } } } catch (NotImplementedException ex) { ex.Message.ToString(); } return duCardType; } ///<summary> /// 查詢所有記錄 ///</summary> ///<param name="cardTypeId">無輸入參數</param> ///<returns>返回表所有記錄(List)DuCardTypeInfo</returns> public List<DuCardType> SelectDuCardTypeAll() { NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); var query = session.Query<DuCardType>(); var result = from cardType in query select cardType; List<DuCardType> list = result.ToList<DuCardType>(); return list; //return dal.SelectDuCardTypeAll(); } ///<summary> /// 查詢所有記錄 ///</summary> ///<param name="cardTypeId">無輸入參數</param> ///<returns>返回表所有記錄(List)DuCardTypeInfo</returns> public List<DuCardType> SelectDuCardTypeAllPrce() { List<DuCardType> list = new List<DuCardType>(); DuCardType duCardType = null; try { using (IDataReader reader = DBHelper.ExecuteReader(SessionFactory, CommandType.StoredProcedure, "proc_Select_DuCardTypeAll")) { while (reader.Read()) { duCardType = new DuCardType(); duCardType.CardTypeId = (!DBNull.Equals(reader["CardTypeId"], null)) ? (int)reader["CardTypeId"] : 0; duCardType.CardTypeName = (!DBNull.Equals(reader["CardTypeName"], null)) ? (string)reader["CardTypeName"].ToString() : ""; duCardType.CardTypeColor = (!DBNull.Equals(reader["CardTypeColor"], null)) ? (string)reader["CardTypeColor"].ToString() : ""; list.Add(duCardType); } } } catch (SqlException ex) { throw ex; } return list; } ///<summary> /// 查詢所有記錄 ///</summary> ///<param name="cardTypeId">無輸入參數</param> ///<returns>返回(DataTable)DuCardType表所有記錄</returns> public DataTable SelectDuCardTypeDataTableAll() { DataTable dt = new DataTable(); try { dt = DBHelper.ExecuteDataTable(SessionFactory, CommandType.StoredProcedure, "proc_Select_DuCardTypeAll"); } catch (NotImplementedException ex) { ex.Message.ToString(); } return dt; //return dal.SelectDuCardTypeDataTableAll(); } /// <summary> /// SQL script查詢分頁 /// </summary> /// <param name="pageSize">每頁頁數</param> /// <param name="currentPage">當前頁碼</param> /// <param name="strWhere">查詢的條件</param> /// <param name="filedOrder">排序字段</param> /// <param name="recordCount">每頁的記錄數</param> /// <returns></returns> public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string filedOrder, out int recordCount) { DataSet ds = new DataSet(); recordCount = 0; try { int topNum = pageSize * currentPage; StringBuilder strSql = new StringBuilder(); strSql.Append("select * FROM DuCardType"); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } recordCount = Convert.ToInt32(DBHelper.ExecuteScalar(SessionFactory, CommandType.Text,PagingHelper.CreateCountingSql(strSql.ToString()))); ds=DBHelper.ExecuteDataSet(SessionFactory, CommandType.Text, PagingHelper.CreatePagingSql(recordCount, pageSize, currentPage, strSql.ToString(), filedOrder)); } catch (NotImplementedException ex) { ex.Message.ToString(); } return ds; //return dal.GetPageList(pageSize, currentPage, strWhere, filedOrder, out recordCount); } /// <summary> /// 模糊查詢 /// </summary> /// <param name="filedlist">顯示字段列表</param> /// <param name="strkey">輸入的關鍵字</param> /// <returns></returns> public DataTable GetDataTableDuCardTypeFuzzySearch(string filedlist, string strkey) { DataTable dt = new DataTable(); try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), new SqlParameter(), }; par[0].ParameterName = "@FieldList"; //表名 par[0].Value = filedlist; par[1].ParameterName = "where"; par[1].Value = StringConvert.getStrWhere("CardTypeName,CardTypeColor", strkey); ; dt = DBHelper.ExecuteDataTable(SessionFactory, CommandType.StoredProcedure, "proc_Select_DuCardTypeFuzzySearch", par); } catch (NotImplementedException ex) { ex.Message.ToString(); } return dt; //return dal.GetDataTableDuCardTypeFuzzySearch(filedlist, strkey); } /// <summary> /// 是否存在該記錄 /// </summary> /// <param name="Id"></param> /// <returns></returns> public bool Exists(int Id) { bool isok=false; int i = 0; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), }; par[0].ParameterName = "@Id"; par[0].Value = Id; using (IDataReader reader = DBHelper.ExecuteReader(SessionFactory, CommandType.StoredProcedure, "proc_Select_DuCardTypeExists", par)) { i = (!DBNull.Equals(reader["H"], null)) ? (int)reader["H"] : 0; if (i > 0) isok = true; } } catch (NotImplementedException ex) { ex.Message.ToString(); } return isok; //return dal.Exists(Id); } /// <summary> /// 返回數據總數 /// </summary> /// <param name="strWhere">查詢條件</param> /// <returns></returns> public int GetCount(string where) { int i = 0; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), }; par[0].ParameterName = "@where"; par[0].Value = where; object obj = DBHelper.ExecuteScalar(SessionFactory, CommandType.StoredProcedure, "proc_Select_DuCardTypeCount", par); i = (int)obj; } catch (NotImplementedException ex) { ex.Message.ToString(); } return i; //return dal.GetCount(where); } /// <summary> /// 更新一列數據 /// </summary> /// <param name="Id"></param> /// <param name="strValue">字段名=值</param> /// <returns></returns> public int UpdateField(int Id, string fieldValue) { int ret = 0; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), new SqlParameter(), }; par[0].ParameterName = "@Id"; //表名 par[0].Value =Id ; par[1].ParameterName = "@FieldValue"; par[1].Value = fieldValue; ret = DBHelper.ExecuteNonQuery(SessionFactory, CommandType.StoredProcedure, "proc_Update_DuCardTypeField", par); } catch (NotImplementedException ex) { ex.Message.ToString(); } return ret; //return dal.UpdateField(Id, fieldValue); } /// <summary> /// 返回指字字段的字串 /// </summary> /// <param name="Id"></param> /// <param name="fieldName">字段名</param> /// <returns></returns> public string GetTitle(int Id, string fieldName) { string title = string.Empty; try { IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), new SqlParameter(), }; par[0].ParameterName = "@FieldName"; par[0].Value = fieldName; par[1].ParameterName = "@Id"; par[1].Value = Id; object obj = DBHelper.ExecuteScalar(SessionFactory, CommandType.StoredProcedure, "proc_Select_DuConfigTitle", par); title = obj.ToString(); } catch (NotImplementedException ex) { ex.Message.ToString(); } return title; //return dal.GetTitle(Id, fieldName); } /// <summary> /// 存儲過程分頁 /// 塗聚文 2016.07.03 /// </summary> /// <param name="strwhere">Where條件</param> /// <param name="aecdesc">排序字段</param> /// <param name="pageIndex">開始頁碼</param> /// <param name="pageSize">頁大小</param> /// <param name="RecordCount">輸出總頁數</param> /// <returns></returns> public DataTable GetDataPage(string strwhere, string aecdesc, int pageIndex, int pageSize, out int RecordCount) { NHibernate.ISession session = this.SessionFactory.GetCurrentSession(); DataSet data = new DataSet(); try { //SessionFactory.ConnectionProvider.Driver IDbCommand cmd = SessionFactory.OpenSession().Connection.CreateCommand(); //cmd.CommandText = "testaa"; //IDbConnection con = session.Connection; //Type conType = con.GetType(); //string conTypeName = conType.FullName; // if (!conTypeName.EndsWith("Connection")) // throw new Exception("Unable to interpret connection type name: " + conTypeName); //string adapterTypeName = conTypeName.Substring(0, conTypeName.Length - 10) + "DataAdapter"; IDbDataAdapter adapter = new SqlDataAdapter(); //IDbDataAdapter adapter = conType.Assembly.CreateInstance(adapterTypeName) as IDbDataAdapter; //if (adapter == null) // throw new Exception("Unable to load IDbDataAdapter: " + adapterTypeName); // IDbCommand cmd = con.CreateCommand(); //1. // cmd.CommandText = "GetPagedRecordFor2005_2008"; // cmd.CommandType = CommandType.StoredProcedure; // IDbDataParameter p = new SqlParameter();// cmd.CreateParameter(); // IDbDataParameter p1 = cmd.CreateParameter(); // IDbDataParameter p2 = cmd.CreateParameter(); // IDbDataParameter p3 = cmd.CreateParameter(); // IDbDataParameter p4 = cmd.CreateParameter(); // IDbDataParameter p5 = cmd.CreateParameter(); // IDbDataParameter p6 = cmd.CreateParameter(); //// IDbDataParameter p7 = cmd.CreateParameter(); // if (string.IsNullOrEmpty(strwhere)) // { // strwhere = " 1=1 "; // } // p.ParameterName = "Table"; // p.Value = "DuCardType"; // p1.ParameterName = "TIndex"; // p1.Value = "CardTypeId"; // p2.ParameterName = "Column"; // p2.Value = " * "; // p3.ParameterName = "Sql"; // p3.Value = strwhere; // p4.ParameterName = "PageIndex"; // p4.Value = pageIndex; // p5.ParameterName = "PageSize"; // p5.Value = pageSize; // p6.ParameterName = "Sort"; // p6.Value = aecdesc; // //p1.ParameterName = "geovindu"; // //p1.Size = 10; // //p1.Direction = ParameterDirection.Output; //輸出值 // //p7.ParameterName = "TotalRecords"; // //p7.Size = 10; // //p7.Direction = ParameterDirection.Output; // cmd.Parameters.Add(p); // cmd.Parameters.Add(p1); // cmd.Parameters.Add(p2); // cmd.Parameters.Add(p3); // cmd.Parameters.Add(p4); // cmd.Parameters.Add(p5); // cmd.Parameters.Add(p6); //// cmd.Parameters.Add(p7); // adapter.SelectCommand = cmd; // adapter.Fill(data); // //RecordCount =(int)p7.Value; // cmd.Cancel(); // cmd.Dispose(); //2. IDbDataParameter[] par = new SqlParameter[] { new SqlParameter(), //表名 new SqlParameter(),//主鍵,可以帶表頭 new SqlParameter(),//讀取字段 new SqlParameter(),//Where條件 new SqlParameter(),//開始頁碼 new SqlParameter(),//頁大小 new SqlParameter(),//排序字段 }; if (string.IsNullOrEmpty(strwhere)) { strwhere = " 1=1 "; } par[0].ParameterName = "@Table"; //表名 par[0].Value = "DuCardType"; par[1].ParameterName = "@TIndex"; par[1].Value = "CardTypeId"; par[2].ParameterName = "@Column"; par[2].Value = " * "; par[3].ParameterName = "@Sql";//查詢條件 par[3].Value = strwhere; par[4].ParameterName = "@PageIndex"; par[4].Value = pageIndex; par[5].ParameterName = "@PageSize"; par[5].Value = pageSize; par[6].ParameterName = "@Sort"; par[6].Value = aecdesc; data = DBHelper.ExecuteDataSet(SessionFactory, CommandType.StoredProcedure, "GetPagedRecordFor2005_2008", par); RecordCount = data.Tables[0].Rows.Count; } catch (NotImplementedException ex) { RecordCount = 0; ex.Message.ToString(); } return data.Tables[0]; // return dal.GetDataPage(strwhere, aecdesc, pageIndex, pageSize, out RecordCount); }