user3085866
user3085866

Reputation: 125

connectionString.open() throws an error

I have successfully used the connection string generated by the Server explorer.

SqlConnection myConnection = new SqlConnection("Data Source=SHIRWANIPC;" + "Initial Catalog=TEST DATABASE;"+"Integrated Security=True");

but as I write myConnection.open(); it throws an error saying:

Error 1 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'open' and no extension method 'open' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?)

Here is a similar problem posted by someone over here in stackeverflow; here is the link

Connecting to SQL Server in ASP.NET

From what I understand that question's OP says that along with the conenction string we also have to pass the sql command. But what I wish to do is to just open the connection and as some button is clicked then only the query is run. How do i just open the connection?

P.S. I have not missed a directive as I have written this command

using System.Data.SqlClient;

Upvotes: 0

Views: 2004

Answers (1)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Method names start with capital letters in C#. It should read

myConnection.Open();

By the way, the safest way to create a connection string is to use the SqlConnectionStringBuilder class like this:

SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
csb.DataSource = "SHIRWANIPC";
csb.InitialCatalog = "...";
...

SqlConnection conn = new SqlConnection(csb.ConnectionString);

Upvotes: 3

Related Questions