I am trying to create a method "Insert" to my WebService connected to external DataBase, but instead of create InsertPerson, InsertAge, Insert anything I am trying to do an unique Insert that the parameter type is defined automatically according with request, for example: if I want to insert a person in db, I call Insert("mytype" name), if after I want to insert age calling Insert("mytype" age) and this type is included when you click at specific button.
If it was confusing I will try explain with one more example: I click on "Insert Age" button who will send to "mytype" that is an integer and value of textbox by parameter, the code will connect to DB and insert that. Something like this:
Click event -> mytype = int
Insert(int age) -> "INSERT INTO db VALUE age"
So, after all this, is possible create something like this? Or exist another better alternative?
Instead of boxing in object
, you can do it generically:
public void Insert<T>(T input)
{
// DB setup code goes here
var parameter = new SqlParameter("@input", input);
// Finish DB code here
}
// Call it like this
myClass.Insert<int>(integerInputData);
myClass.Insert<string>(stringInputData);
It's cleaner than boxing and unboxing in object.
https://msdn.microsoft.com/en-us/library/twcad0zb.aspx
However, if you are just passing it to something that expects object
(like SqlParameter
), then you can just box it in object
.
public void Insert(object input)
{
// DB setup code goes here
var parameter = new SqlParameter("@input", input);
// Finish DB code here
}
// Call it like this
myClass.Insert(integerInputData);
myClass.Insert(stringInputData);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments