473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Why SqlConnection is needed if I can query database using SqlDataAdapter only?

For example, the following code works fine without SqlConnection.
Any Answer to the question?

string connectionString =
"server=YourServer; uid=sa;
pwd=YourPassword; database=ProgASPDotNetBugs";

// get records from the Bugs table
string commandString =
"Select BugID, Description from Bugs";

// create the dataset command object
// and the DataSet
SqlDataAdapter dataAdapter =
new SqlDataAdapter(
commandString, connectionString);

DataSet dataSet = new DataSet();

// fill the dataset object
dataAdapter.Fill(dataSet,"Bugs");

// Get the one table from the DataSet
DataTable dataTable = dataSet.Tables[0];

// for each row in the table, display the info
foreach (DataRow dataRow in dataTable.Rows)
{
lbBugs.Items.Add(
dataRow["BugID"] +
": " + dataRow["Description"] );
}
Nov 16 '05 #1
6 4285
Hi goodstart123,

What do you mean? You don't need to use an SqlConnection object to use SqlDataAdapter, and overloaded constructor accepts a command string and a connection string just fine. However, there might be a slight benefit of using an existing SqlConnection.
You don't need to use an SqlDataAdapter either if you use SqlConnection and SqlCommand.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
goodstart123 <go**********@yahoo.com> wrote:
For example, the following code works fine without SqlConnection.
Any Answer to the question?


No, it doesn't work without SqlConnection - it just constructs the
SqlConnection itself from the connection string you give it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Hi,

internally, from the connectionstring parameter (third in the SqlDataAdapter
ctor), the DA creates a connection, opens it using the connectionstring
property, fills the DS, closes it. All these is transparent to the end user.

However, if you need to create a DataReader or execute the ExecuteNonQuery
method of the command object, you need to have a connection object.

Regards
Joyjit
"goodstart123" <go**********@yahoo.com> wrote in message
news:a0**************************@posting.google.c om...
For example, the following code works fine without SqlConnection.
Any Answer to the question?

string connectionString =
"server=YourServer; uid=sa;
pwd=YourPassword; database=ProgASPDotNetBugs";

// get records from the Bugs table
string commandString =
"Select BugID, Description from Bugs";

// create the dataset command object
// and the DataSet
SqlDataAdapter dataAdapter =
new SqlDataAdapter(
commandString, connectionString);

DataSet dataSet = new DataSet();

// fill the dataset object
dataAdapter.Fill(dataSet,"Bugs");

// Get the one table from the DataSet
DataTable dataTable = dataSet.Tables[0];

// for each row in the table, display the info
foreach (DataRow dataRow in dataTable.Rows)
{
lbBugs.Items.Add(
dataRow["BugID"] +
": " + dataRow["Description"] );
}

Nov 16 '05 #4
So what is the difference between using and not using SqlConnection?
What kind of benefit?

"Morten Wennevik" <Mo************@hotmail.com> wrote in message news:<opsb62edpgklbvpo@morten_x.edunord>...
Hi goodstart123,

What do you mean? You don't need to use an SqlConnection object to use SqlDataAdapter, and overloaded constructor accepts a command string and a connection string just fine. However, there might be a slight benefit of using an existing SqlConnection.
You don't need to use an SqlDataAdapter either if you use SqlConnection and SqlCommand.

Nov 16 '05 #5
What do you mean 'it doesn't work without SqlConnection' ? I am a little confused.

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP***********************@msnews.microsoft.c om>...
goodstart123 <go**********@yahoo.com> wrote:
For example, the following code works fine without SqlConnection.
Any Answer to the question?


No, it doesn't work without SqlConnection - it just constructs the
SqlConnection itself from the connection string you give it.

Nov 16 '05 #6
goodstart123 <go**********@yahoo.com> wrote:
What do you mean 'it doesn't work without SqlConnection' ? I am a
little confused.


I mean that creating a SqlDataAdapter by passing in the connection
string is basically the same as creating it by passing in a
SqlConnection created with that connection string. The following two
pieces of code are equivalent:

SqlDataAdapter da = new SqlDataAdapter
(command, new SqlConnection(connectionString));

and

SqlDataAdapter da = new SqlDataAdapter(command, connectionString);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phin | last post by:
I need your HELP! I've seen all the posts on using Crystal Reports within vs.net (vb.net) and changing a SQL query at runtime. When I tried to pass in a dataset into the crystal report at...
1
by: Robert Muchacki | last post by:
Hello all! I'm fighting with this little problem. I'm trying to retrieve data from a table in SQL Server 2k. I'm dead. The only result I get is retrieving the headers of the columns - no data in...
2
by: Sgt. Sausage | last post by:
New to multi-threading (less than 24 hours at it <grin>) Anyway, it's all making sense, and working fairly well thus far, but I'm having a minor issue I'm not sure how to get around. I've got...
11
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
4
by: David Gilbert | last post by:
Is better to pass the same SQL connection to all of my user controls. Should I make a global object for my SQL connection, or is there some other better choice out there. Thanks DG
1
by: John Bailo | last post by:
using ( SqlCommand cmd = new SqlCommand("select * sample"), new SqlConnection( sqlDsn ) ) ) { SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable dt = new DataTable();...
1
by: Tony Johansson | last post by:
Hello! Below is a simple program that works fine. In this program there is no explicit open for the SqlConnection instead the DataAdapter will open the Connection to the database automatically....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.