コード例 #1
0
 /// <summary>
 /// 执行语句,返回具体类型对象
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sql"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public T ExecuteScalar <T>(string sql, params object[] args)
 {
     try
     {
         if (_sharedConn.State == ConnectionState.Closed)
         {
             _sharedConn.Open();
         }
         using (var cmd = CreateCommand(CommandType.Text, sql, args))
         {
             object val = cmd.ExecuteScalar();
             OnExecutedCommand(cmd);
             return(ConvertFun.ConvertType <T>(val));
         }
     }
     catch (Exception x)
     {
         OnException(x);
         throw;
     }
     finally
     {
         if (_tran == null)
         {
             _sharedConn.Close();
         }
     }
 }
コード例 #2
0
ファイル: DALHelper.cs プロジェクト: lonlywaiting/Netcollect
 /// <summary>
 /// 根据语句,返回list
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sql"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public IEnumerable <T> Query <T>(string sql, params object[] args) where T : class
 {
     using (var db = new LcLib.DBLib.DBHelper())
     {
         return(ConvertFun.ConvertToIEnumerable <T>(db.ExecuteDataTable(sql, args)));
     }
 }
コード例 #3
0
ファイル: DALHelper.cs プロジェクト: lonlywaiting/Netcollect
 /// <summary>
 /// 根据语句,返回对象
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sql"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public T QueryModel <T>(string sql, params object[] args) where T : class
 {
     using (var db = new LcLib.DBLib.DBHelper())
     {
         DataTable dt = db.ExecuteDataTable(sql, args);
         if (dt.Rows.Count > 0)
         {
             return(ConvertFun.ConvertToModel <T>(dt.Rows[0]));
         }
         else
         {
             return(default(T));
         }
     }
 }