Example #1
0
        public static List <T> SelectSingleValues <T>(SqlCommand sqlCommand)
        {
            DataTable dt   = SelectMultipleRows(sqlCommand);
            List <T>  list = new List <T>();

            foreach (DataRow dr in dt.Rows)
            {
                list.Add(DataRowExtensionsCustom.Get <T>(dr[0]));
            }

            return(list);
        }
Example #2
0
        public T SelectSingleValue <T>()
        {
            if (SqlCommand.Connection == null)
            {
                throw new InvalidOperationException("SqlCommand.Connection is not set.");
            }

            using (QueryTime.Measure(this))
            {
                var value = sqlCommand.ExecuteScalar();
                if (value == null)
                {
                    return(default(T));
                }

                // Special case for Id<> and LongId<>
                var targetConvertType = typeof(T);
                if (IdTypeExtension.IsIdType(targetConvertType))
                {
                    var constructor = targetConvertType.GetConstructor(new[] { typeof(int) });
                    if (constructor == null)
                    {
                        throw new InvalidCastException("The Id<> type has no constructor taking int as a parameter.");
                    }

                    return((T)constructor.Invoke(new object[] { Convert.ToInt32(value) }));
                }
                else if (LongIdTypeExtension.IsLongIdType(targetConvertType))
                {
                    var constructor = targetConvertType.GetConstructor(new[] { typeof(long) });
                    if (constructor == null)
                    {
                        throw new InvalidCastException("The LongId<> type has no constructor taking long as a parameter.");
                    }

                    return((T)constructor.Invoke(new object[] { Convert.ToInt64(value) }));
                }

                // Just execute the query, don't close the connection
                return(DataRowExtensionsCustom.Get <T>(value));
            }
        }
Example #3
0
 public static T SelectSingleValue <T>(SqlCommand sqlCommand)
 {
     return(DataRowExtensionsCustom.Get <T>(SelectSingleValue(sqlCommand)));
 }
Example #4
0
 public static T SelectSingleValue <T>(string sqlCommand, params object[] parameters)
 {
     return(DataRowExtensionsCustom.Get <T>(SelectSingleValue(sqlCommand, parameters.Select((p, i) => new SqlParameter("@" + i, p)).ToArray())));
 }
Example #5
0
 public static T SelectSingleValue <T>(string sqlCommand, params SqlParameter[] parameters)
 {
     return(DataRowExtensionsCustom.Get <T>(SelectSingleValue(sqlCommand, parameters)));
 }