Get sql type name from DbType - c#

I need to print real datbase type name from DbParameter.DbType, but when I do something like this DbParameter.DbType.ToString() I get for example Int32 instead of int, is there a command or a property to do that or am I going to need to write switch statement?
Here is code if someone will need it: DbType to real SQL type string

You will have to write your own converter
The data type mappings can be found here MSDN

Related

Using enum in data annotation

I am using a data annotation like so:
[RequiresAnyRole("Admin", "Member")]
I don't like passing strings as it can be a pain refactoring later but If I try to make an enum like so:
public enum Roles
{
Admin,
Member
}
And then try to cast the enum to string like so:
[RequiresAnyRole(Roles.Admin.ToString(), Roles.Member.ToString())]
Then I get the error:
Error CS0182 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
My understanding of this error is that it has to be a string at compile time. I have tried to make a static dictionary with the enum as the key but that didn't work either.
What is the best way to pass these values in a way that can be refactored later i.e. not passing in a string but passing in a reference of some sort that can be updated?
This is a great opportunity to use nameof. You can keep your enum, and do:
[RequiresAnyRole(nameof(Roles.Admin), nameof(Roles.Member))]
nameof expressions are constant expressions.
Of course, if you can change the attribute's declaration, you should change it to accept your Roles enum.

Auto convert specific data type from c# to client

Is there any way to set globally rule for example in global.asax in C# to automatically convert and change specific data type to another data type?
For example automatically every property with Int64 or Long data type convert to the string when it is passing from Controller to the Client??
I know I can do that with defining Convert method but I think it is not good idea.
Thank you

Oracle Custom type parameter

i am new to oracle .I have procedure which has a param like this
#param1 title_type.title_type_code%type
title_type is custom type
ok.. now
How do I pass the parameter from c# using ODAC( OracleParameter object)
%type is confusing do you need to pass as object or pass as string
Anyone know please help??
This is just a reference to the type that title_type.title_type_code is using, as an alternative to hardcoding the type in two places.
You need to look up the definition of that table, and pass the parameter as whatever that is (could be VARCHAR, could be NUMBER, I doubt that it is an object).

NHibernate CreateSQLQuery data conversion from bit to boolean error

Im being a bit lazy in NHibernate and using Session.CreateSqlQuery(...) instead of doing the whole thing with Lambda's. Anyway what struct me is that there seems to be a problem converting some of the types returned from (in this case the MySQL) DB into native .Net tyes.
The query in question looks like this....
IList<Client> allocatableClients =
Session.CreateSQLQuery(
"select clients.id as Id, clients.name as Name, clients.customercode as CustomerCode, clients.superclient as SuperClient, clients.clienttypeid as ClientType " +
...
...
.SetResultTransformer(new NHibernate.Transform.AliasToBeanResultTransformer(typeof(Client))).List<Client>();
The type in the database of SuperClient is a bit(1) and in the Client object the type is a bool.
The error received is:
System.ArgumentException: Object of type 'System.UInt64' cannot be converted to type 'System.Boolean'.
It seems strange that this conversion cannot be completed.
Would be greatful for any ideas.
Thanks.
No need to do any lambdas (even though they are fun!). If Client is a mapped class then you can use the core NHibernate method CreateCriteria<>. It is very simple:
session
.CreateCriteria<Client>()
.List<Client>();
If Client isn't mapped then I would create a property SuperUser_Long and wrap it with SuperUser

Using reflection to set the value of an Int32

I want to use reflection to set some fields according to data from a file. The information that I can have is TypeName, TypeValue and FieldName.
While this is trivial on classes (Activator.CreateInstance and PropertyInfo.SetValue), I am a bit dumbstruck when it comes to value types like Int32 which does not have any properties. I see that IsValueType is true on those Types, but as my TypeValue is a string (i.e. string TypeValue = "400"), I don't know how to assign it.
Do I have to use GetMethods() to check if there is a .Parse Method? Is this something for a TypeConverter?
I know I could just hardcode some common value types (there aren't that many anyway) and have a big switch() statement, but I wonder if there is something that automagically does a "Convert String to T" conversion?
// get the type converters we need
TypeConverter myConverter = TypeDescriptor.GetConverter(typeof(int));
// get the degrees, minutes and seconds value
int Degrees = (int)myConverter.ConvertFromString(strVal);
this should help
ArsenMkrt is right; TypeConverter is the way to go here; some extra thoughts, though:
You might consider using "component model" rather than reflection; i.e. instead of typeof(T).GetProperties(), consider TypeDescriptor.GetProperties(typeof(T)). This gives you a set of PropertyDescriptor records instead of the reflection PropertyInfo. Why is this handy?
because people can actually specify a converter per property (below) - this is exposed as prop.Converter on PropertyDescriptor
if the file is big, you can get a performance boost by using HyperDescriptor
As an example of a property with custom converter:
[TypeConverter(typeof(CurrencyConverter))] // bespoke
public decimal TotalPrice {get;set;}

Categories