i'm currently modifying a legacy application that uses the SqlHelper class in c#. how do you access the OUTPUT value using an executedatareader?
You need to pass a SqlParameter object with the correct name, then check the parameter's value after calling ExecuteDataReader.
Related
Say i have Object1 which has a list of Object2 which has Object3, which has Property1 which i want the value of. Is it possible to have a string in my database like "Object1.Object2[0].Object3.Property1" and then somehow use that within code to navigate that object and get the value for a property i want? I am getting my object i want to navigate to from another api and i basically want to look in my database and see property1 must be > 10 and then dynamically be able navigate that object so i can see if the value in my object passes my criteria in my database. I want to be able to just use database values for this so we can add fields to our database without updating our code. Is this possible?
Yes, it is definitely possible. You'll want Object1 to be a dynamic object, so that you can access its properties using variables e.g. Object1["Object2"][0]. Then all you need is to work out a consistent format for your database strings, with your example you could split by . and use a loop to access and store each property in the result successively.
See this question for info on dynamic objects from JSON (assuming that's the format you receive it in from your API) or if you'd prefer to use reflection, this question has info about that.
I found a nuget package to do exactly what i wanted. https://github.com/Domysee/Pather.CSharp
I'm working on a deserialization class in .NET, I have to develop a method that provides to me with a variable name that is stored in a string.
I have a string such as:
string string_name = "this_is_going_to_be_var_name";
Now what can I do so that my code dynamically declares a variable named this_is_going_to_be_var_name?
So to clear things up: There will be a deserialization class that will declare variables of the same names as strings provided as input with their PARENT TYPES as per wish of the Higher Level Programmer/User.
For Example: In javascript/jQuery, when I fetch JSON by making a request, the interpreter declares variable(s)/array(s) of the same name and assigns values to them. If {"var_name":"var_value"} is a JSON string, the interpreter will create a variable named var_name and will assign "var_value" to it such as json_data_object.var_name.
No you can't. C# variables are all statically declared.
The best thing you can do is create a dictionary and use keys instead of variable names.
// Replace object with your own type
Dictionary<string, object> myDictionary = new Dictionary<string, object>();
myDictionary.Add("this_is_going_to_be_var_name", value_of_the_variable);
// ...
// This is equivalent to foo($this_is_going_to_be_var_name) in PHP
foo(myDictionary["this_is_going_to_be_var_name"]);
This isn't possible, variable names are defined at compile time, not run time.
One approach is to create a dictionary or hash table to map string names to objects to sort of achieve what you want.
Not sure what you meant by
my code dynamically declares a variable named
this_is_going_to_be_var_name?
but the .Net version of what explode does in PHP is Split:
string[] zz = "this_is_going_to_be_var_name".Split('_');
The only thing that I can think on the moment (I haven't tested it so I have no clue if it is possible), is to have a object of type dynamic and then try to set the fields at runtime using reflection and InvokeMember(), which I can give a chance that it will work since the there is no validation on objects of type dynamic.
UPDATE:
I have tested it with ExpendoObject and InvokeMember doesn't appear to work (at least not with the default binder, but I haven't tested it with DynamicObject and allthough I don't give it to much chances to work you might still try it, you can check out http://msdn.microsoft.com/en-us/library/ee461504.aspx on how to use DynamicObject.
Take a look on Dynamically adding properties to an ExpandoObject which essentially describes a method in which the dynamic object is being cast as a IDictionary and then you can add properties to it by using the standard dictionary access, while they are actually getting properties of the object.
I tested it in a sample project by having a dynamic object of type ExpendoObject and then I add another variable that referenced it using type IDictionary, and then I tried to set and get properties on both, as in the following example:
dynamic test = new ExpandoObject();
//reference the object as a dictionary
var asDictinary = test as IDictionary<string, Object>;
//Test by setting it as property and get as a dictionary
test.testObject = 123;
Console.Write("Testing it by getting the value as if it was a dictionary");
Console.WriteLine(asDictinary["testObject"]);
//Test by setting as dictionary and get as a property
//NOTE: the command line input should be "input", or it will fail with an error
Console.Write("Enter the varible name, ");
Console.Write("note that for the example to work it should the word 'input':");
string variableName = Console.ReadLine();
Console.Write("Enter the varible value, it should be an integer: ");
int variableValue = int.Parse(Console.ReadLine());
asDictinary.Add(variableName, variableValue);
Console.WriteLine(test.input);//Provided that the command line input was "input"
(Still however in your case since you will anyway not access the properties directly in code I don't see the need for it and you would probably be able to use a Dictionary directly, and I don't understand why you need them to be properties of the object, which is only needed if you want to reference them at compile time.
But maybe I am misunderstanding you and you are looking for a dynamic variable and not for a dynamic property [something that is available in PHP using the $$ syntax], if this is the case then note that in c# there are no variables at all as everything is encapsulated in a object).
You can also take a look in How can I dynamically add a field to a class in C# for more answers.
I'm developing an application which can deal with a MS-ADLDS-Service.
Currently it is possible to create Directory-Entries and assign values to some properties.
Not a realy exciting task until this:
Im my application it's possible (it should be) to configure which properties of a class (for instance: the CN=Person class) should be assigned with values which are evaluated at runtime in my application.
Long story short:
I want to retrieve all (writeable) properties of a class. Without creating and saving a new CN=Person-Object before.
Currently i use my schemaBinding to get the Directory-classSchema-Entry of the Person-Class (CN=Person) from where i read some property-values (like "AllowedAttributesEffective", "mayContain", "AllowedAttributes") - i get the most properties by this way - but some Properties are missing! For instance the "telephoneNumber"-Property (attributeSchema: CN=Telephone-Number)
Does anybody know how to get these properties of a class? ADSI-Edit does this: when i create a new object with adsi-edit i can assign values to all possible properties before committing the new entry.
thanks a lot for any hint!
(.net code is welcome)
I have found the solution for my task!
Some of these properties are "calculated" and not persistent at the directoryentry.
So its meant to call the RefreshCache() Method and pass the needed property names as an string array.
directoryEntry.RefreshCache(new string[] { "allowedAttributesEffective",
"allowedAttributes",
"systemMayContain",
"systemMustContain" });
After that call, the properties have values....
if (directoryEntry.Properties["systemMayContain"]).Value != null)
{
/// Success
}
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).
i was wondering, can a parameter be used more then once in the same query, like this :
MySqlParameter oPar0 = new MySqlParameter("e164", MySqlDbType.String);
oPar0.Value = user.E164;
string sSQL0 = "Delete from callmone.call where (caller=?e164 or called=?e164);";
clsDatabase.ExecuteSQL(sSQL0, oPar0);
Is this possible or should i write 2 parameters?
If the database driver handles named parameters, then you can reuse the parameter.
If the database driver doesn't handle named parameters, the parameter names are ignored and you have to add one parameter values for each use, in the exact order that they are used.
From the code that you presented it looks like the driver supports named parameters. If the code runs without an error, it works. If the driver would not support names parameters, the code would cause an error as there is only one parameter value.
I don't know of any reason why you can't do that.