Connection string to SQL by EntityFramework and MVC - c#

I write web page in MVC, which use Entity Framework as ORM mapper. I put my connection string to web.config file. Here is it:
<add name="JP_CMS" connectionString="Data Source=.;Initial Catalog=JP_CMS;
User Id=CmsWebUser;Password=abcd1234abcd1234;"
providerName="System.Data.SqlClient"/>
I try to get data by this C# code
public DataBaseContext()
: base("JP_CMS")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataBaseContext>());
}
//------------------------------
var db = new DataBaseContext();
var result = db.Articles.ToList(); //<- here is thrown exception
//------------------------------
My local SQL server is running, this table is exists, but I get excepion like in this:
I think it is the most important expection: "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)"
Could you help my by give some idea what I make wrong?

"The system cannot find the file specified" error. Check carefully your connection string.

hi Error indicates EF tried to connect to the server but failed.
Could you Checkk,
1) SQL Server should be up and running.
Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> SQL Server Services, and check if SQL Server service status is “Running”.
In addition, ensure that your remote server is in the same network. Run “sqlcmd -L” in your command prompt to ascertain if your server is included in your network list.
2) Enable TCP/IP in SQL Server Configuration
For more info you can go here

Instead of DataSource =. try DataSource = .\sqlInstanceName. Also DataSource=(local) might work.

Related

How to connect to a SQL Server instance with C# using Visual Studio

I am unable to connect to SQL Servers that have the '\' char which is used when connecting to a server instance. Any other type of server works without any issues. Any support or resource on this issue would be extremely helpful as I have exhausted my possible solutions.
Thanks!
Chris
I have tried:
- adding/removing the '#'
- using a double '\'
- hard coding the server name and connecting
var masterConnString = $#"Server={Server}; Database={ProjectCode}; Trusted_Connection=True;";
using (var connection = new SqlConnection(masterConnString))
{
connection.Open();
//do something
connection.Close();
}
The following error occurs:
System.Data.SqlClient.SqlException HResult=0x80131904 Message=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)
Source=.Net SqlClient Data Provider
Update:
When attempting to update the SQL Server Configuration Manager the connection fails due to lack of permissions.
Remove the trusted param and add this param
"Integrated Security=SSPI"
This always works with me when the server don't have any authentication

Insert ErrorA network-related or instance-specific error occurred while establishing a connection to SQL Server

I got a error while i upload my application on server and it works fine at my localhost.
On server when doing any entry i got the following error.
Insert ErrorA 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).
and with same connection string i m using it on my localhost.
my connection string is look like
string connection = "Data Source=XXX.XXX.XXX.XXX; Initial Catalog=abcdefgh; Persist Security Info=True; User ID=ag_packers; Password=agpackers";
and when i used the same connection string in my local host it works fine and at server it gives the following error.
This could have different reasons, heres allready 2:
DataSource incorrect, if it's on the same machine try: ./SqlServerName
Your SqlServer service is not running, enable it at services.svc
Some points for you to check
Check whether your database server name/IP address is reachable from your web server by using ping dos command.
Check whether your target database is a Defaule SQL Server Instance or a Named instance. If it a named instance use this format for that.
Enable the named pipes/TCP IP for SQL server using server configuration manager.
Good luck.

How to create external connection with local database in test project?

I have in Visual Studio two projects: My normal project and my test-project. I have chosen to use SpecFlow for testing and therefore my tests are UI based. So a database connection is not needed for the UI validation.
In some cases I have to set some preconditions to the system, like filling some sample data in the project. I've tried to connect my project-database to my test-project, but it just won't work. I tried to add connectionstrings to my test-project app.config like this:
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data Source=C:\Users\Martijn\Documents\VS11\Projects\Gastouderuren.nl\testprojectl\App_Data\example.sdf"
providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>
When I use this connection string in the code as follows:
MyContext context = new MyContext(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
context.Product.Count();
When I run my test, the following error occurs:
The provider did not return a ProviderManifestToken string. -> This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection. -> 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)
I've searched already for a solution, but couldn't find anything. How can I solve this problem?
Thanks!
You cant connect to a external localdb (file based) like the same way you used to connect to normal SQL database
(connect string)
its because of localdb limits (No more than 1 apps can connect at once)
but i have a soulution : (How to use 2+ applications with 1 localdb)
You have to create a TCP server listener (In pc where LOCALDB is) and
X clients where your other pc(s) are
client pc(s) connect to server pc
So everytime you want to execute a sql command you send command from
CLIENT to SERVER and Server executes it ! (Can send back response to)
you can set some pre definded commands too(i did same thing)
SERVER :
if(CLIENT DATA . equals("ReadProducts"){
sql.execute("SELECT * FROM Products");
//convert...
SendBack(result);
}
CLIENT 1 >SEND> SERVER : "ReadProducts" (Predefinded command)
SERVER >SEND> CLIENT 1 : "Apple,Melon,Orange"
CLIENT 1 :
data = SERVER RESPONSE;
string[] sp=data.split(',');
//add a for loop which add all products to your listview
CLIENT 2 >SEND> SERVER : "INSERT INTO Products(name) VALUES(#name)"
SERVER :
if(CLIENT DATA . contains("INSERT"){
sql.execute(CLIENT DATA);
}
Using algorithm abow you can handle many connections to a single LocalDb file
from another computers but you have to deal with IP problem
get a Static Ip address for server app if you developing something big

Error connecting SQL server to MVC3 application - A network-related or instance-specific error

I'm getting this error since I've moved my mvc3/entity framework site onto the live server from localhost. Normally when I would get this error, I'd check the database name, password and server are all correct in the connection string. I have checked this - they all seem fine.
I have aspnet Membership provider on the site within the database, and it allows me to login, verifies me, then tries to redirect me to another page, and then that's where the error happens - i.e., as soon as I connect to the database outwith the membership provider.
The database is on the same server as the site, and when I connect from localhost to the remote server, it works perfectly.
Here is the full error message:
Exception message: 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)
Here is the connection string:
<add name="ApplicationServices" connectionString="Data Source=192.168.1.43;Initial Catalog=BlueLadder;User Id=BlueLadderAdmin;Password=Auth1991;timeout=30" />
Anyone anything I could try?
Ok, despite my connection string in the web config, it was actually being ignored and the application was still trying to connect to a local version. Apparently you need to pass the connection string through the dbContext constructor, like so.
public Context()
: base("ConnectionString")
{
}
Question was answered here
Thanks for your help anyway.
Try removing the Data Source and Initial Catalog, and replace them with something like this:
Server=.\SQLEXPRESS;Database=BlueLadder;
I don't know if you're using SQLEXPRESS, so just modify accordingly if you're not.
Check if your server is accepting remote connections and has TCP/IP provider enabled. You can configure this using "Sql Server Configuration Manager" on your server
You could try specifying the named instance of your server. "Sql Server Configuration Manager" can tell you what instances are installed on your server.
Server = 192.168.1.43/MSSQLSERVER or
Server = 192.168.1.43/SQLEXPRESS

Error when connecting to .mdf from ASP.NET (C#)

When trying to connect to a .mdf databse in ASP.NET (using c#) I am given 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: SQL Network
Interfaces, error: 26 - Error Locating
Server/Instance Specified)
Does this mean my connection string is wrong? What should it be?
Aside from that, I am very new to ASP.NET. I am trying to connect to this database in the same way I would in c# normally (using data adapters and SqlCommands). Is this the right way to go about it, or is there a different way? When I started a new website adding user accounts worked (there was a wizard or something?) but I couldn't work out how to add more user information. What is the best way to connect to an SQL database and add user accounts with login and personal details in ASP.NET?
EDIT: My Connection String is data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Users.mdf;User Instance=true
If I read your connection string properly, you're never specifying a database to use!
data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\Users.mdf;User Instance=true
Add the database to the connection string - and also, loose the extra backslash after the |DataDirectory|:
server=.\SQLEXPRESS;database=YourDatabase;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|Users.mdf;User Instance=true
OK, I've fixed it. For reference for anyone else:
The correct connection string was given in the Web.config file.
The connection string I needed was Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;User Instance=True

Categories

Resources