Jump to content

SQL server not found

stefanmz
Go to solution Solved by Franck,

hold on if the name is like "Something\something" you wont need the leading .\

When you use \ in a string it's an escape character so you have 2 choices. Double up the character so \ become \\ or use literal string

 

string escapeExample = "Some text with single escaped \\ back slash and double escaped \\\\ backslashes";
string stringLiteralExample = @"Some text with single \ backslash and double \\ backslash without any need for escape characters";

 

Trying to build a .NET CORE web app with an sql server attached and this is what I get when I try to connect to the server through the app:" Microsoft.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. " WTF? I can access the server via the Microsoft SQL Server  Management Studio. Localhost server

.

Link to comment
Share on other sites

Link to post
Share on other sites

32 minutes ago, stefanmz said:

Trying to build a .NET CORE web app with an sql server attached and this is what I get when I try to connect to the server through the app:" Microsoft.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. " WTF? I can access the server via the Microsoft SQL Server  Management Studio. Localhost server

.

I think a better question would be why are you using .NET and MS SQL for a web app 🤦‍♂️.
Post some code so we can take a look at it. Most probably the server and the db are there. ready and waiting and you just a have a malformed connection string in your code.

VGhlIHF1aWV0ZXIgeW91IGJlY29tZSwgdGhlIG1vcmUgeW91IGFyZSBhYmxlIHRvIGhlYXIu

^ not a crypto wallet

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Biohazard777 said:

I think a better question would be why are you using .NET and MS SQL for a web app 🤦‍♂️.
Post some code so we can take a look at it, most probably the server and the db are there. ready and waiting and you just a have a malformed connection string in your code.

because it's my homework I literally don't know what I am doing. I think my connection string is correct,I don't know what to post but I am posting my DbContext class

using Microsoft.EntityFrameworkCore;
using MyProject1.Models.Home;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyProject1.Entities.Repositories
{
    public class Addressbook_context:DbContext
    {
        public DbSet<User> Users { get; set; }

        public Addressbook_context()
        {
            this.Users = this.Set<User>();
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=localhost;Database=Addressbook;User Id=people;Password=datapass;");
        }
        
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

It simply doesn't see your server instance

9 hours ago, Biohazard777 said:

I think a better question would be why are you using .NET and MS SQL for a web app 🤦‍♂️.

I don't see the problem there. I mean for example canadian tax system that millions use is that. Blazor.net front and mssql db in the back for every file you have to fill in. I personally use a couple of these and as an example i have a small front end server in .net that does 220 million transaction per day and i know it can handle more but still i don't see why it would be any problem.

 

9 hours ago, stefanmz said:

I think my connection string is correct,I don't know what to post but I am posting my DbContext class

 

checkout connectio nstring website. It have most different connection string there.

https://www.connectionstrings.com/

But i do see an issue right away. I am unsure if your Server instance name is reall "localhost" and if it is really that you are missing the location of that server. You need to provide the IP or DNS name and instance name to the Server=[server address]

Typically the format is for example :

Server=MyPCName\SqlServerInstanceName

using SQLExpress usually the instance name is called "SQLExpress" os you could specially write it like this Server=.\SQLExpress

The dot mean local. Note that this will only work if the SQL is running on the same computer that the code is running.

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, Franck said:

It simply doesn't see your server instance

I don't see the problem there. I mean for example canadian tax system that millions use is that. Blazor.net front and mssql db in the back for every file you have to fill in. I personally use a couple of these and as an example i have a small front end server in .net that does 220 million transaction per day and i know it can handle more but still i don't see why it would be any problem.

 

 

checkout connectio nstring website. It have most different connection string there.

https://www.connectionstrings.com/

But i do see an issue right away. I am unsure if your Server instance name is reall "localhost" and if it is really that you are missing the location of that server. You need to provide the IP or DNS name and instance name to the Server=[server address]

Typically the format is for example :

Server=MyPCName\SqlServerInstanceName

using SQLExpress usually the instance name is called "SQLExpress" os you could specially write it like this Server=.\SQLExpress

The dot mean local. Note that this will only work if the SQL is running on the same computer that the code is running.

well isn't it running on the same computer when I have downloaded SQL developer for example and installed it from the ISO file? I will try a different string as well but I think it's running on my computer

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, stefanmz said:

well isn't it running on the same computer when I have downloaded SQL developer for example and installed it from the ISO file? I will try a different string as well but I think it's running on my computer

If you installed it on the computer that you are running on currently then yes it local but localhost is a network keyword not an SQL instance name. You have to get the SQL server instance name. If you use SQL management Studio just run the following query

 

select @@servername

This return the variable where the server name is stored which is the value you are looking for. So you should have the connection string value of the Server= to be :

dot, backslash, value from the query so if the query returned ABCD the server value should be :

Server=.\ABCD

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, Franck said:

If you installed it on the computer that you are running on currently then yes it local but localhost is a network keyword not an SQL instance name. You have to get the SQL server instance name. If you use SQL management Studio just run the following query

 

select @@servername

This return the variable where the server name is stored which is the value you are looking for. So you should have the connection string value of the Server= to be :

dot, backslash, value from the query so if the query returned ABCD the server value should be :

Server=.\ABCD

The problem is the name is SOMENAME(not real name)\SQLEXPRESS and when I type  .\NAME\SQLEXPRESS it thinks the \ in the name is an escape sequence and gives an error

Link to comment
Share on other sites

Link to post
Share on other sites

hold on if the name is like "Something\something" you wont need the leading .\

When you use \ in a string it's an escape character so you have 2 choices. Double up the character so \ become \\ or use literal string

 

string escapeExample = "Some text with single escaped \\ back slash and double escaped \\\\ backslashes";
string stringLiteralExample = @"Some text with single \ backslash and double \\ backslash without any need for escape characters";

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Franck said:

hold on if the name is like "Something\something" you wont need the leading .\

When you use \ in a string it's an escape character so you have 2 choices. Double up the character so \ become \\ or use literal string

 

string escapeExample = "Some text with single escaped \\ back slash and double escaped \\\\ backslashes";
string stringLiteralExample = @"Some text with single \ backslash and double \\ backslash without any need for escape characters";

 

yeah that worked thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×