Solved

C# sql proper declaration

Posted on 2007-03-18
2
179 Views
Last Modified: 2010-04-16
Is it better to do this in a form:
form beginning:
 SqlCommand cmd;

then throughout the code
cmd = new sqlcommand("select * from table", Connection);

or should it be like this
Form Beginning
SqlCommand cmd = new sqlcommand();

then throughout the code
cmd.connection = connection;
cmd.commandtext = "select * from table";


0
Comment
Question by:bman9111
2 Comments
 
LVL 15

Accepted Solution

by:
stanscott2 earned 500 total points
ID: 18745870
Since they're functionally equivalent, I always prefer the shorter one -- doing it in one line, rather than two.  EIther is correct, though.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18746393
it depends. if you create a command object that is used several times/often, define it in the global class, and use it all over the project.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Diagnostics with Net and Net.Sockets 2 31
PrintingFoundIt(Almost!(Damn!)) 1 30
C# LINQ ForEach() question 6 54
What are the simplest ways to create a Merge Sort? 3 29
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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