Solved

C# sql proper declaration

Posted on 2007-03-18
2
177 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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
install app on other machine 13 32
c# string handling 1 27
Facial recognition to look through the whole database for a person 3 43
C# Comparing String Arrays. 5 26
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

11 Experts available now in Live!

Get 1:1 Help Now