1

when i run a query like this:

    SqlDataAdapter dap = new SqlDataAdapter("select * from some table", myConnection);

before doing the select, should i be doing "use somedatabase; go" ??

2
  • 5
    That is specified in your connection string. Commented Nov 4, 2010 at 18:17
  • You can see what connection string is appropriate for your scenario by going to connectionstrings.com Commented Nov 4, 2010 at 18:18

6 Answers 6

4

No, your database and schema should be set in the connection string for myConnection.

Sign up to request clarification or add additional context in comments.

Comments

1

No you should specify the database name in myConnection

InitialCatalog = [databaseName] 

Your connection string should something look like this

data source=[ServerName];Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=[DatabaseName];

Comments

0

I imagine myConnection is already specifying a default catalog (i.e. database) in its connection string, so you don't need to use the use line.

See here for details.

Comments

0

No; the myConnection object's connection string should define which database needs to be used, along with the server and login information.

Comments

0

That should all be in the myConnection variable, since I presume that contains the connection string.

Although you might want to call using on the DataAdapter

using(SqlDataAdapter dap = new SqlDataAdapter("select * from some table", myConnection)
{
    //do stuff with dap here 
}//dispose of dap

Since it does inherit from something that implements IDisposable.

Comments

0

Your connection string tells it what database to connect to.

connectionString = "Data Source=SERVERNAME; Initial Catalog=DATABASENAME; Integrated Security=SSPI;";

That would create a connection to a server and database using windows authentication.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.