By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,013 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,013 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.