开发者

running a query from C# on sql server

开发者 https://www.devze.com 2023-01-23 18:57 出处:网络
when i run a query like this: SqlDataAdapter da开发者_如何学JAVAp = new SqlDataAdapter(\"select * from some table\", myConnection);

when i run a query like this:

    SqlDataAdapter da开发者_如何学JAVAp = new SqlDataAdapter("select * from some table", myConnection);

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


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


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];


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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消