Can not connect to local address in SQL Server - c#

Cannot create SQL connection with this connection string:
<connectionStrings>
<add name="SchoolContext"
connectionString="Server=.;Database=SchoolContext;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I get this error:
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)
but there's no problem with this connection string:
<connectionStrings>
<add name="SchoolContext"
connectionString="Server=DESKTOP-7M2F9E2\MOJTABA;Database=SchoolContext;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Why can it not connect to local?

That's most probably cause you are trying to connect to a named instance called MOJTABA and not to a default instance as can be seen from your posted code connectionString="Server=DESKTOP-7M2F9E2\MOJTABA. You can as well say Server=localhost\MOJTABA or .\MOJTABA

I would try this:
<connectionStrings>
<add name="SchoolContext" providerName="System.Data.SqlClient" connectionString="Server=.\MOJTABA;Database=SchoolContext;Integrated Security=True;"/>
</connectionStrings>
Server=.\MOJTABA means: local machine (.), MOJTABA database server instance.
The dot alone is not enough to know which instance to connect to.

This is not a C# problem, so the tag for that was wrong. The Network code does not care if the other end is on the same computer, same switch or the Voyager 2 Probe.
Proper installation and administration of a SQL Server is a totally seperate mater from programming.
And finally connection strings are a mater so complicated, there is a dedicated webpage for that.
This is either a SQL Server Administration issue (the instance you try to connect does not exists/is not allowed to take connections) or a Connection String issue (you formated it faulty). It is really impossible to tell without knowing how exactly your Environment looks, wich is something only you can realy do.

Related

Service-Based Database Entity Framework Connection Error On released version

I am writing a Windows Forms application, where I want to save data inside the application. For the reason I used a serviced-based database with a .mdf database file, and I use Entity Framework.
So far everything is working fine until I released the file and opened it on another computer. The application opened and everything worked fine but when it comes to the database interaction, it throws a big error like this:
************ Exception Text ************
System.Data.Entity.Core.EntityException: The underlying provider failed on Open.
System.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. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.)
Here a screenshot of the error: https://www.dropbox.com/s/gwban6ab97c6fya/22093543_10213995193571435_760919436_n.png?dl=0
In case you need the project its uploaded to here: https://www.dropbox.com/s/ubpc683ggtihh6k/usercontrol.zip?dl=0
I have tried in so many ways but nothing is working, same thing happens for the installer version as well.
Can anyone help me on this please?
Here are my connection strings:
<add name="DatabaseEntities"
connectionString="metadata=res://*/dbModel.csdl|res://*/dbModel.ssdl|res://*/dbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="OPLCheque.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
I'm pretty sure this post would be flagged as duplicate since a similar question has been answered multiple times already, but here you go.
You are using LocalDB which means it cannot be shared since it runs on SQL Server Express.
If you simply want a LocalDB, see this answer to implement it after publishing your WinForms app.
Basically you want to publish your app with prerequisite components so you should publish those along your application.
If you want to use a single database to be shared between instances of your application, learn how to setup one using SQL Server here.
Afterwards, learn what connection string format you should use.
Sample to connect via IP Address:
connetionString="Data Source=IP_ADDRESS,PORT;
Network Library=DBMSSOCN;Initial Catalog=DatabaseName;
User ID=UserName;Password=Password"
See this for more.
If ever you plan on using a shared database, better make sure that the connection string that your database context should use is specified.
Config:
<add name="DefaultConnection" connectionString="" providerName="" />
Database Context:
public DbContext()
: base ("DefaultConnection")
{
}

Database Connection: Web.config Asp.net

What's wrong with my connection string?
I copied my files from one computer to another. Then I altered my connection string because I moved my files from another computer, it is not connecting to database. When I tried connecting to SQL Management Studio, it successfully connected to my server and when I try running my codes, it didn't.
Here's my Connection String: (Web.config) - My server is Windows Authentication
<add name="ConnectionStringMe" connectionString="Data Source=ZOZOW-PC\MSSQLEXPRESS;Database=sample_db;Persist Security Info = True" providerName="System.Data.SqlClient" />
It gives me error:
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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
Is there any conflict if my database is from SQL Server Authentication then I moved it to Windows Authentication?
Thank you.
I believe you need this Integrated Security = true into your connection string and it should work. If you don't have this in your connection string you need to specify password and username.
<add name="ConnectionStringMe" connectionString="Data Source=ZOZOW-PC\MSSQLEXPRESS;Database=sample_db;Persist Security Info = True;Integrated Security=True" providerName="System.Data.SqlClient" />

Getting SQL Server error in ASP.net application when using different computer

I have a simple ASP.NET C# application which works fine on the computer it was created. However, whenever this project is compiled and run on any other computer, I get the following error:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
On the following line:
bool admin = HttpContext.Current.User.IsInRole("Admin");
I could previously run the application on a different computer without error. I have made no changes to any SQL server settings.
In Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-INT422Project-20140320070224.mdf;Initial Catalog=aspnet-INT422Project-20140320070224;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="DataContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Lab5;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|INT422Project.mdf" />
</connectionStrings>
Your better change your connection String value to use a real sql server database.
SQL Server Express or LocalDB is not used for production web applications because it is not designed to work with IIS.
Please check the following:
Make sure the sql server service is up and running.
Make sure the TCP/IP ports are enabled.
If both are fine, I would suggest you to read the servername and database name from the app.config/ web.config which makes life easier.
Dude, Check this answer.
https://stackoverflow.com/a/16054490/3081461
you can read more about localdb at microsoft TechNet as well :
http://technet.microsoft.com/en-us/library/hh510202.aspx

Create Simple Website using DevExpress Templates

I'm new to .net world and I started to create asp.net web site.
So I Created a New Project using DexExpress template like below image
When I was run, It appear as;
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
What is the reason for this ?
Is there any other way to use devExpress Templates ?
Sorry for wasting your valuable time for my basic problem here. Because I'm stuck here.
Thank you,
EDITED
here is my connection string,
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
<add name="DataConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data.mdf;Integrated Security=True;User Instance=True;Connect Timeout=120" providerName="System.Data.SqlClient"/>
I suspect that either -
Instance names are not configured properly in connection string.
Or the concerned Sql Server instance is not running.
Can you please let us know which instance of Sql Server is running on your PC?
You can see SQL Server Services by typing services.msc in Run box. Please see snapshot to understand how to get instance name.

SQL connection failure in LINQ-to-SQL

I'm cloning a local C#-SQL Windows Forms application to a new laptop. I've successfully cloned the C# project and back-up-and-restored the SQL database to the new machine, but the new project is unable to read/query any data from the new, cloned SQL database.
Since the SQL DB is local, the LINQ-to-SQL addressing is slightly different on account of the computer having a different name. So, I deleted the SQL table representations from the existing LINQ .dbml's graphical interface, removed the old server connection from the Server Explorer, added the new server connection, and click-and-dragged the table objects from the Server Explorer's new Data Connection object to the now-empty .dbml file.
Upon rebuilding and running the application in debug mode, I found that the dropdown menus which pull from the SQL DB are empty. Upon trying a button function which pulls from the database, I get the following error:
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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
(The cloned SQL server is configured to allow remote connections, TCP/IP is enabled in SQL, and the original C#-SQL application works without a hitch.)
Check in your Web.Config and make sure that your Connection String is set up correctly. Also check to make sure that when you initialize your DataContext that you are using the correct Connection string from your Web.Config.
Sometimes when you remove dbs from the dbml, it doesn't completely clear the web.config of the old entries. Look for a line like this in your designer.cs file:
public SomeDbDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["SomeDbConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
Compare it with the connectionStrings in your web.config
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="someDBConnectionString" connectionString="Data Source=SomeServer;Initial Catalog=SomeDB;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="theOLDDBConnectionString" connectionString="Data Source=SomeServer;Initial Catalog=OldDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I would check a few things:
1) Is SQL Server started on the new machine?
2) Is the firewall configured to allow SQL connections?
3) Is the client connection mechanism (named pipes, tcp/ip) also configured in SQL Server.
4) Is there a valid network connection in the new machine (we used to have a lot of problems on laptops when they were not connected to the network).
You might have much better luck if you change the SQL Server machine name to . or (local) if you are always going to have the DB installed on the same machine as the app.

Categories