Solved

C# sql proper declaration

Posted on 2007-03-18
2
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linq, c# 8 50
Save json data from URL using SSIS 1 73
Adjust the codes 3 62
web api controller with multiple posts that accept different objects 1 22
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

737 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