wizzardmr42
wizzardmr42

Reputation: 1644

Can I get the SqlConnection from SqlDataAdapter?

Is there any way to get the SqlConnection from an SqlDataAdapter?

I had expected to find an SqlDataAdapter.Connection property.

Upvotes: 2

Views: 123

Answers (4)

Farfarak
Farfarak

Reputation: 1527

You can get it from relevant SqlCommand.

Depending on which one of the is set:

        adapter.SelectCommand.Connection;
        adapter.InsertCommand.Connection;
        adapter.UpdateCommand.Connection;
        adapter.DeleteCommand.Connection;

Upvotes: 3

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

Use this:

var da = new SqlDataAdapter();
....
string connectionString = da.SelectCommand.Connection.ConnectionString

Upvotes: 1

kmatyaszek
kmatyaszek

Reputation: 19296

SqlDataAdapter has property SelectCommand (type of SqlCommand) and SelectCommand has property Connection (type of SqlConnection).

Upvotes: 3

dotjoe
dotjoe

Reputation: 26940

Look at the SelectCommand or whichever command you're interested in.

Upvotes: 4

Related Questions