Question

This is an example of my custom class.

public class Application
{
    public string App_Name { get; set; }
    public string App_Number { get; set; }
    public string Sever_Name { get; set; }

}

I am using ExecuteNonquery from the Enterprise Library Data Block to call my stored procedure.

I am looking for a way, possibly with linq, to turn an object of type Application into an Object[] that can be passed into ExecuteNoneQuery which looks like this.

Database.ExecuteNonQuery Method (String, Object[])

This is an example of what I'd like to be able to do, but a conversion of application will need to occur before this will work.

internal void Update(Application application)
{
    int returnValue = 0;
    string sql = "somsesproc";
    DatabaseFactory.CreateDatabase("AppsConnectionString").ExecuteNonQuery(somsesproc,    application);
}
Was it helpful?

Solution

It's unclear whether you want the Application object itself within the object array:

object[] oa = new object[] {application};

Or if you want the properties split into an array:

object[] oa = new object[] {application.App_Name, 
                            application.App_Number, 
                            application.Sever_Name};

OTHER TIPS

You can use reflection to strip out all of the property values into an array of any arbitrary object:

public static object[] StripPropertyValues<T>(T obj)
{
    return typeof(T).GetProperties(BindingFlags.Public |
        BindingFlags.Instance)
            .Select(prop => prop.GetValue(obj))
            .ToArray();
}

Thus allowing you to write:

DatabaseFactory.CreateDatabase("AppsConnectionString")
    .ExecuteNonQuery(somsesproc, StripPropertyValues(application));
.ExecuteNonQuery(somsesproc, new Object[] {application});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top