none
A network-related or instance-specific error occurred while establishing a connection to SQL Server RRS feed

  • Question

  • I followed the walkthrough.

    The only different is the mdf file path.

    namespace LinqConsoleApp
    {
        [Table(Name = "Customers")]
        public class Customer
        {
            private string _CustomerID;
            [Column(IsPrimaryKey = true, Storage = "_CustomerID")]
            public string CustomerID
            {
                get
                {
                    return this._CustomerID;
                }
                set
                {
                    this._CustomerID = value;
                }
    
            }
    
            private string _City;
            [Column(Storage = "_City")]
            public string City
            {
                get
                {
                    return this._City;
                }
                set
                {
                    this._City = value;
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                // Use a connection string.
                DataContext db = new DataContext
                    (@"C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Northwind.mdf");
    
                // Get a typed table to run queries.
                Table<Customer> Customers = db.GetTable<Customer>();
                // Attach the log to show generated SQL.
                db.Log = Console.Out;
    
                // Query for customers in London.
                IQueryable<Customer> custQuery =
                    from cust in Customers
                    where cust.City == "London"
                    select cust;
                foreach (Customer cust in custQuery) // exception here
                {
                    Console.WriteLine("ID={0}, City={1}", cust.CustomerID,
                        cust.City);
                }
    
                // Prevent console window from closing.
                Console.ReadLine();
            }
        }
    }

    However I GOT "A network-related or instance-specific error occurred while establishing a connection to SQL Server". I used SQL Server Management Studio 2012 and I can connect it. I am sure that the path is right.

    Monday, April 13, 2015 2:13 PM

Answers

  • Don' use the mdf file.  It is not necessary and can lead to issue.  The SQL Server is a service that runs on your PC.  The mdf file is mounted on the server and you can access the databaes through the service.

    Use the instance of the database that is on the SSMS login window.  Use connection string below.  The server should be the same as the SSMS login window which includes the PC name and instance of the database.

    Server=myServerAddress;Database=myDataBase; Integrated Security=SSPI;

    Integrated Security will use windows credentials with the database.  SQL Server can be setup to use either a private database credential or a windows credentials.  I recommend using windows.  I suspect you issue is the wrong type of credentials are being used.


    jdweng

    Monday, April 13, 2015 3:55 PM