We help IT Professionals succeed at work.
Get Started

Where to put SQL statements in C# project?

tmreiter asked
Last Modified: 2016-02-11
I've googled on this topic, and have found quite a few answers, but either I didn't understand them or they didn't cover my situation.  I'm a hobbyist develop and am just moving from Access/VBA to C#/SQL Server; while I"m slowly working through a (digital) pile of books on C#, SQL Server, and T-SQL on my kindle, I was hoping that the experts here could help me in the meantime.

My application uses quite a few SQL statements to populate list boxes, tree views, etc.  In Access, I just included the SQL code in the form modules; it probably wasn't a good idea with Access, and probably even more so with C#, so I want to do things more properly in C#.

I have a few questions below, but first some context:
    A)  I'm not really concerned about enterprise-level best practice or performance--any application I develop will probably not be commercial at all, and if so, the dataset will be small enough that performance should not be an issue.
    B)  That said, I don't want to turn out crap or spaghetti code--whatever I do, I want to be high-quality, if not industry-standard or bleeding edge.
    C)  My biggest concern is actually simplicity; I'm teaching myself (OK, with your guidance) and I'm shooting with the minimum amount of brain damage consistent with the first two points.  I'd also like to avoid dead-ends, where I spend weeks learning about some approach before deciding it is too complicated, etc.  I'd like to learn simple, functional approaches and graduate to more complicated approaches later if necessary.

OK, here are the questions:
1)  For now, I'm thinking about keeping it simple by just putting all of my SQL statements into one file in the C# project as named strings, and calling them from the forms.  Does that make any sense?  If so, how would I do it (ie, what kind of file should I put them in, and how would I call them)?  FYI, I anticipate needing several dozen SQL statements, most not very complicated but some a bit involved.

2)  From what I've read, some recommend keeping all of my SQL statements as stored procedures, which I understand are stored in SQL Server?  But from what I've read stored procedures are more complicated than straight SQL and harder to maintain/troubleshoot.  Is that correct?  Any good reasons to move everything to stored procedures (or not)?

3)  I've done a bit of reading on Entity Framework, and how it is "the future".  Frankly it sounds like an interesting and potentially simpler approach and I've thought about moving in that direction, but I'm concerned that it could be too complicated for a beginner like me.  Any recommendations?
Watch Question
Senior Developer
This problem has been solved!
Unlock 3 Answers and 10 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE