Hi,
var obj=new String();
here obj is an object of String Type. But In a case, if we need to create an object dynamically, and even sure about Type, so how we can make it.
For example :-
var obj=new DynamicType(); //it's an example only, there is no any Type available in .Net like DynamicType
Here DynamicType is a Type that can be of any .net Type, it may be Int, String, float or any one.
Actually why I need this:-
I am creating a dynamic sql Update query, in which, I need to pass parameter arguments, and number of parameters are not fixed, so I have to create it dynamically, so for String type variable (like varchar, text, char etc), it need Single Quote and for Numeric Type (like int, float, money), no need to single quote.
So all of you, please share your ideas regarding above scenario, if i need to choose any other way or how I can make it possible.
Thanks,
Vishwakant
Loading

Amit ChoudharyPosted Oct 1, 2013, 10:36 AM
here's the Gist to get the source code:
https://gist.github.com/vendettamit/6779430
So now you just have to pass the column and value ordered array like you're doing in String.Format() expression.
e.g.
// Sample array of columns and correpsonding values
var columnValueArray = new object[] {"Column1", "Value1", "Column2", 8, "Column3", true, "Column4", DateTime.Now };
The call the utility function:
string mappedColumnsWithValuesForSql = ParametersAsSqlColumnValueMap(columnValueArray);
This will give you the output : -
Cheers!!!
Vishwakant TripathiPosted Oct 3, 2013, 6:39 AM
Whatever I was looking, is exactly TypeMapperToSqlValues(Type type, object value) method in your post.
Thanks again.
VulpesPosted Oct 1, 2013, 10:38 AM
Vishwakant TripathiPosted Oct 1, 2013, 9:11 AM
I read your article, and you have explained very nicely about anonymous type. But I am not getting how to use it in my scenario.
Ok, I am trying to explain my points again, so it may be you can help me more.
string sql= String.Format("Update dbo.{0} Set {1}={2}, {3}={4}", "tableName", "ColName1", "abc", "ColName2", 8);
If you see above example, I am trying to format a sql query, now suppose, ColName1 is varchar type, so with it's value we need Single quote also (like 'abc') and ColName2 is int type, so obviously it does not need quotation.
So final query should be like
Update dbo.tableName Set ColName1='abc', ColName2=8
My problem is, i am not able to find whether to put quotation or not, I don't want to do it hard coded like checking each datatype.
So please tell me if you have some ideas.
Thanks
Sunny SharmaPosted Oct 1, 2013, 4:46 AM
Amit ChoudharyPosted Oct 1, 2013, 4:03 AM
see: http://www.cshandler.com/2013/09/creating-dynamic-object-with-dynamic.html
http://www.c-sharpcorner.com/UploadFile/vendettamit/creating-dynamic-object-with-dynamic-feature-in-C-Sharp/
hope this is exactly what you need.
@Sunny The var is a compile time type variable it's not dynamic. It'll throw exception if there's problem during compile time itself. While dynamic allows you to do whatever you want and will be evaluated by run time.
Sunny SharmaPosted Oct 1, 2013, 3:25 AM
if you refer to the var type, itself is an implicit type. It infers the data type from the type of object assigned to it in right side. This is why if you try to declare any var type variable like :
var abc; // it will raise error.
but
var abc=15; // it is fine, because here it will automatically infers integer type from right side.
So you can apparently do operations like that.
Please reply if need any clarification or if it doesn't answer/satisfy your question.
HTH :)