• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

C# sql proper declaration

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
bman9111
Asked:
bman9111
1 Solution
 
stanscott2Commented:
Since they're functionally equivalent, I always prefer the shorter one -- doing it in one line, rather than two.  EIther is correct, though.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now