1

I am trying to connect to a database with my connection string and recieve the following error when trying to connect to the database. For intergrated Security I user SSID so I don't have to enter a username and password. Also, the database resides on the same machine and was created inside VS2010. I can connect to the db without a problem using the SqlDataSource, but I am looking to start writing my own connection strings.

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string source = "server=(local)" + "integrated security=SSPI;" + "Connect     Timeout=30; "  + "database=Name";
        SqlConnection conn = new SqlConnection(source);
        conn.Open();

        conn.Close();

    }

The Error I get is this:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

0

5 Answers 5

2

Try the following syntax as connection string:

string source = "Data Source=Server Address;Initial Catalog=Database Name;Integrated Security=SSPI;";

Where Server Address should be localhost or .\SQLExpress

Hope thats correct. I have'nt installed a vs for testing

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

1 Comment

Thanks, instead of using server=(local) I switched to Data Source with the full address. If anyone else tries this make sure you use double backslashes in the address. For example, C:\\MyDoc\\database.mdf.
2

You are missing a ; after the server name.

Why are you concatenating all the parts of the connection string if they do not change? It makes it more difficult to read.

Try this:

string source = "server=(local);integrated security=SSPI;ConnectTimeout=30;database=Name";

5 Comments

Your absolutley correct, I was but now I am back to my old error which is.
Cannot open database "Name" requested by the login. The login failed. Login failed for user 'X\xm'.
I thought if I use windows inegrated security the credentials that were supplied at logon are passed to SQL Server. Which allows me to remove the uid and pwd and insert Integrated Security=SSPI.
@jpavlov: Is your database called "Name", really?
@jpavlov - correct, so long as the database Name exists and the account your are logging in as has permissions to use it (Security/Users section).
2

You need a semicolon after (local)

string source = "server=(local);" + "integrated security=SSPI

I notice you tagged the question with asp.net by default asp.net runs under a system account, when you are using integrated security then that account is trying to access your database, it probably doesn't have permission.

Take a look here for some information.

Comments

1

I would recommend you to use the SqlConnectionStringBuilder: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder(v=VS.100).aspx

That will probably make it easier.

Comments

0

To connect to SQL Server from C#.NET, you need to create a connection string such as below:

private SqlConnection connection;
private string connectionString =
    @"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );

Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below:

SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid",
    connection); 

The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.

Next to execute the SQL queries in the database, you use the following methods: ExecuteReader - to execute SELECT queries ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database. For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html ) Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.

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.