472,090 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,090 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 4207
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Robert Muchacki | last post: by
4 posts views Thread by David Gilbert | last post: by
reply views Thread by leo001 | last post: by

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.