473,574 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 connectionStrin g =
"server=YourSer ver; uid=sa;
pwd=YourPasswor d; database=ProgAS PDotNetBugs";

// 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, connectionStrin g);

DataSet dataSet = new DataSet();

// fill the dataset object
dataAdapter.Fil l(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.Ad d(
dataRow["BugID"] +
": " + dataRow["Descriptio n"] );
}
Nov 16 '05 #1
6 4299
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**********@y ahoo.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Hi,

internally, from the connectionstrin g parameter (third in the SqlDataAdapter
ctor), the DA creates a connection, opens it using the connectionstrin g
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
"goodstart1 23" <go**********@y ahoo.com> wrote in message
news:a0******** *************** ***@posting.goo gle.com...
For example, the following code works fine without SqlConnection.
Any Answer to the question?

string connectionStrin g =
"server=YourSer ver; uid=sa;
pwd=YourPasswor d; database=ProgAS PDotNetBugs";

// 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, connectionStrin g);

DataSet dataSet = new DataSet();

// fill the dataset object
dataAdapter.Fil l(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.Ad d(
dataRow["BugID"] +
": " + dataRow["Descriptio n"] );
}

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:<opsb62edp gklbvpo@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.co m> wrote in message news:<MP******* *************** *@msnews.micros oft.com>...
goodstart123 <go**********@y ahoo.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**********@y ahoo.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(c onnectionString ));

and

SqlDataAdapter da = new SqlDataAdapter( command, connectionStrin g);

--
Jon Skeet - <sk***@pobox.co m>
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
4000
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 runtime, the report still showed the results from the default query (from within the Crystal Report). Then I tried the XSD solution where you define a...
1
4117
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 it actually :/ I'm probably missing something on the way. Could somebody explain me how do I get data and even WHERE I should retrieve it to?...
2
2486
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 a form that uses SqlDataAdapter. It fires off a thread to fill a DataSet. Not a big deal, this works. I've also got a requirement that the Thread...
11
2878
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 reduces the need to getting and returning connections to the pool repeatedly if a page has multiple calls to the DB, and each one manages its own...
4
1116
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
2393
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(); adapter.Fill(dt);
1
1596
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. Now to my question if I check the connection after the SqlDataAdapter has been created the connection is then closed ? So when does the...
0
7832
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7747
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8262
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7843
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3768
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2262
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1094
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.