2

I can't seem to connect to my local database. Everytime I run it, it gives me a pop up blank windows (blank command line windows).

What do I miss?

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace dbtest1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myConnectionString = "Initial Catalog=myDB;Data Source=localhost;Integrated Security=SSPI;";
            SqlConnection myConnection = new SqlConnection(myConnectionString);
            string myInsertQuery = "INSERT INTO tts (min, max, average, lh, stdev, main_id) Values(5,5,5,'ASU',5,55)";
            SqlCommand myCommand = new SqlCommand(myInsertQuery);
            myCommand.Connection = myConnection;
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();
        }
    }
}

when I run debug it gives me error at myConnection.Open();

SqlException was unhandled: 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)

2
  • 1
    You are not actually outputting anything to the console window, which is why it is blank. What actual error are you getting? Commented Jun 8, 2011 at 22:48
  • when I run debug it gives me error at myConnection.Open(); SqlException was unhandled: 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) Commented Jun 9, 2011 at 14:29

3 Answers 3

3

Your SQL Server is not configured to talk over TCP/IP (which is what localhost implies)

You can either change your connection string to use (local) instead of localhost (note: the brackets are important.) Like this:

Data Source=(local);

or

you can configure your SQL Server to accept TCP/IP connections.

Open up SQL Server Confuguration Manager and Navigate to SQL Server Network Configuration --> Protocols for

On the right hand side you'll see a list of protocols such as Shared Memory, Named Pipes, TCP/IP etc. Ensure that TCP/IP is turned on. (Right-click and select "Enable")

You mention elsewhere you are using SQL Server Express, in which case your data source must also include the instance name SQLEXPRESS, like this:

Data Source=localhost\SQLEXPRESS
Sign up to request clarification or add additional context in comments.

1 Comment

@JohnRyann please indicate which of the suggestions worked, the first or the second
1

I bet that the app works. Have you checked the database to see if the values are inserted?

The blank command line window is most likely because you have set the project type as a windows console project.

Maybe add a Console.WriteLine("No worries!"); at the end of the Main method.

4 Comments

With a Console.ReadKey(); after that so the console doesn't disappear right away.
I checked the database and there is no new record insert. the app does not work, obviously something is not right. the windows pop up doesnt disappear. it stays there. it actually happens at myConnection.Open().
sql server compact edition and sqlexpress are not the same product, and the use different connection objects.
Now that you added the exception to the question, its obvious that the error is in the connection string. Colin Mackay, seems to have the correct answer.
0
"ConnectionStrings": {
  "DefaultConnection":"Server=DESKTOPK0DSR57\\SQLEXPRESS;Database=item;Trusted_Connection=True;MultipleActiveResultSets=true"},

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.