Solved

Reflection to build SqlParameters

Posted on 2013-06-24
7
413 Views
Last Modified: 2013-07-17
I am using Reflection to create lots of SqlParameters in an ASP.NET C# web application; is this a good idea, or are there some serious problems I need to be aware of?

Thanks.
0
Comment
Question by:userTester
  • 4
  • 3
7 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 39272795
It's hard to say without knowing details. However, taking into account that reflection is pretty time consuming process your application performance may become quite poor. If you have many requests it can be a big problem, I think.

Read the article, for example:

http://msdn.microsoft.com/en-us/magazine/cc163759.aspx
0
 

Author Comment

by:userTester
ID: 39273217
Thanks for that; the articles go through a bit more than what I am doing, and it seems that my situation is okay. Here's the piece that I use to get the object properties:

List<KeyValuePair<string, Type>> pType = new List<KeyValuePair<string, Type>>();

// Add object Properties to KeyValue List
foreach (PropertyInfo pInfo in type.GetProperties())
{
  pType.Add(new KeyValuePair<string, Type>(pInfo.Name, pInfo.PropertyType));
}

and

List<KeyValuePair<string, object>> pVal = new List<KeyValuePair<string, object>>();

// Get object type
Type objType = obj.GetType();

// Add Property Name and Value to return list
foreach (PropertyInfo pInfo2 in objType.GetProperties())
{
  pVal.Add(new KeyValuePair<string, object>(pInfo2.Name,propInfo2.GetValue(obj, null)));
}
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 39273705
Looks not too bad :).
If you'll have any doubts about performance in future I'd recommend to do performance measurements using some profiler.
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 

Author Comment

by:userTester
ID: 39273741
Yes, the whole CreateParameters method I have put together works very well, but it's good to know that there is the performance hit that goes with it, so I will have to test that, and might have to simply hard-code the parameters for each situation.

Was hoping to avoid all the hard-coding? I wonder whether there's another more acceptable way of automating the creation of SqlParameters??
0
 

Author Comment

by:userTester
ID: 39325206
Perhaps I should phrase the question a bit differently:

Aside from using Reflection, is there another commonly used way to automate Parameter creation?
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 500 total points
ID: 39325305
Not sure. May be some config file... But I'd rather use reflection.
0
 

Author Closing Comment

by:userTester
ID: 39334891
Thanks for answering my questions.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 55
Change Format on Data when exporting to Excel 4 23
Code works but it's slow 24 40
C# guarantee sql connection close 6 25
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now