I've created a c# windows application(2.0 framework) which uses MS SQL database.
During developing I've used MS VISUAL STUDIO 2010 and SQL 2008 MANAGEMENT STUDIO.
My connection string during development is :
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SL;Integrated Security=True");
Everything works fine....
Now I want to run this application on client system.
So installed MS SQL SERVER 2008 EXPRESS successfully on client system.
Stopped sql services of my system and copied the .mdf and .ldf files from my machine and pasted in "c:\Database\" of the client.
But while running comes the problem.
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 connection strings which I've tried many times are :
SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS;Initial Catalog=SL;Integrated Security=True");
SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS;Initial Catalog=SL;Persist Security Info=True;User ID=sa;Password=pass");
SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS; AttachDbFilename =C:\\Database\\SL.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
When I use User ID=sa;Password=pass in connection string I get:
authentication failed for 'sa'
Am I missing some steps or doing wrong? Please tell me what should I do after copying the database from my system. What should be my connection string in C#?
Thanks !
You have only copied the files you need to attach the database into SQLExpress, take a look at using OSQL, or alternatively install the client tools onto the PC where you have SQLExpress and attach the databases.
First of all, be sure to check out Sres' answer.
If you don't tell SQL Server in the connection string to attach your database (with AttachDbFilename, like in your third example), you have to do the attaching yourself as he said.
Concerning your three connection string examples: all of them only work under certain circumstances. You might want to check out connectionstrings.com.
Here are your three connection strings, plus short explanations of their issues:
Data Source=.\MSSQLEXPRESS;Initial Catalog=SL;Integrated Security=True
--> This uses the current Windows user that you app is running under. So the current windows user must have permissions on the database on the client's machine.
Data Source=.\MSSQLEXPRESS;Initial Catalog=SL;Persist Security Info=True;User ID=sa;Password=pass
--> This uses the special 'sa' user account. In order for this to work, you have to make sure that the following prerequisites are met:
Mixed mode authentication must be set up (if you don't do this, you only have Windows authentification, and 'sa' is a SQL Server authentification user name)
the password of 'sa' needs to be specified, and of course it must be the same as on the development machine.
But this is not the best solution anyway. 'sa' is an admin account with full permissions, and you shouldn't use an admin account to access SQL Server with your app.
If you really want to use SQL Server authentification (instead of Windows authentification), it's better to create a new account with the minimal necessary permissions that your app needs.
Data Source=.\MSSQLEXPRESS; AttachDbFilename =C:\Database\SL.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
--> same issue as with the first connection string (the current Windows user must have permissions).
Plus, User Instance=True needs to be enabled in SQL Server.
Quote from connectionstrings.com:
To use the User Instance functionality you need to enable it on the
SQL Server. This is done by executing the following command:
sp_configure 'user instances enabled', '1'. To disable the
functionality execute sp_configure 'user instances enabled', '0'.
Are you sure the new instance name is 'MSSQLEXPRESS'?
My R2 Express instance is called 'SQLEXPRESS' (this is actually due to a known bug in the installer).
You might want to check that the instance is actually called what you think it is.
Related
I have some working code that is currently sitting on our test dev server that connects to an SQL server using Integrated Security/Trusted_Connection at the moment.
I will need to deploy this to a clients server and it will be scheduled to run every hour regardless of if a user is logged on or not.
as the user may not be logged in I assume that the Integrated security will not work.
The credentials I am putting in are the same as those I use to connect to the dev server. I have tried both with the domain "organisation\username" however this does not work either.
SqlConnection myConnection = new SqlConnection("User id=username;" + "Password=xxxxxx; + "Data Source=datasrc;" + "Initial Catalog=catalog; " + "connection timeout=30");
myConnection.Open();
The above code does not work and results in the program 'hanging' before running myConnection.Open()
However putting "Integrated Security=True;" and removing the "User id" and "Password" sections of the string works.
When I try to use "User id=name;" + "Password=xxxxxx;" it does not work and just hangs.
Below is an error message from trying to login with username and password (windows authentication works fine) on the SQL Server 2014 Management Studio
"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) (Microsoft SQL Server, Error: 53)
The network path was not found"
Integrated Security=True; means that the current credential being used to run the application pool will also be used to login to the SQL database. It doesn't make sense to use Integrated Security=True; and a User ID + Password. One or the other.
So, if your IIS instance is using an application pool logged in by User123, and the .NET application sitting on that IIS instance connects to a SQL database, then that .NET application will attempt to login to the SQL database using User123, so long as Integrated Security=True;. Typically people do this to ensure no plaintext passwords are present in the web.config, for security reasons. EDIT: you can see which user the application pool is using by right clicking the application pool in IIS manager and going to 'Advanced Settings'.
If it only works when you have Integrated Security=True; then that means only that user has permissions to access the SQL database. I would open up SSMS, navigate to the SQL database, and review the Logins under the Security navigation tree. Keep in mind there is a Login for the database server, as well as for the database itself, so you may have to configure this in two places.
I have a working SQL connection here:
SqlConnection conn =
new SqlConnection(#"server=localhost\SQLEXPRESS;
AttachDbFilename=C:\Seach ENGINE (June 22, 2015)\SE\SE\MainDatabase.mdf;
Integrated Security=True;User Instance=True");
but the problem is I need to provide the complete path. Unlike asp.net, all I have to do is to add |DataDirectory| to it and include the database name ..
Then I tried to use the |DataDirectory| in this connection string and I get an error:
invalid value for key 'attachDbFilename'
I published my application and I can't locate the database now because I installed it in a different computer.. so the attachDbFilename is not true ..
If the DataDirectory is not working is there any other code that I can use similar to data directory?
AttachDbFilename is used with local database. If you have remote database then the connection string will be different . Have a look at article - How to configure SQL Server 2005 to allow remote connections.
Access mdf file
As you see here
System.Data.SqlClient resolves the substitution strings into full
paths against the local computer file system. Therefore, remote
server, HTTP, and UNC path names are not supported. An exception is
thrown when the connection is opened if the server is not located on
the local computer.
If you want to connect to an already-running SQL Server Express instance on another computer over TCP, then you need to set-up TCP connections on that server first. Note that you cannot use AttachDbFilename either because that option only applies to "user instances" of SQL Server Express.
Hope this will help you.
SqlConnection require you to specify the fully-qualified name of the
db that is being attached.
No way around that.
I made a win forms app with SQL Server Express with Visual Studio 2010.
When I deploy my app by right click on Solution Explorer selecting Properties and then Publish and following all the procedure.
When I install on other system, error occured. The error was:
cannot open database [dbname] login requested by user [username] failed...
I googled much but no use. I think it might be in connectionstring, if it is so, what and how should be connectionstring or any other alternative.
If you have Integrated Security = true in your connection string you will have to provide a sql server login and a database user in SQL Server for your users. Alternatively, you can set up a specific SQL Server login and database user for your app, grant it least-privaledged permissions in SQL Server, and then specify it in your connection string as user=myuser;password=mypassword;
Then remove the integrated security = true portion altogether.
1st- check if your connection string is correct and make it specific like dont use dot. try to use IP address on your server name(dont forget to include \SQLexpress if needed). also if u are using windows authentication(without user id and password) you need to add on the connection string: "integrated security=SSPI"
2nd- off firewall. firewall blocks all remote connection
3rd- check hardware, ping the server and make sure the client can see the server
I am helping my friend in his college final year project, Its an C# application in a client machine and it uses SQL server in the windows server 2008 environment.
VM ware environment:
3 clients(windows 7) and 1 SQL database (in windows 2008)
The connection between he server and the client is working, the port 1433 for SQL is open and its listening.
When I try to run the program am getting the following error.
This is the connection string which am using.
cn = new SqlConnection(#"Data Source=192.168.1.2,1433;Initial
Catalog=Test;Integrated Security=True");
And i have also tried to refer this website for connection strings, but i don't know how it works.
http://www.connectionstrings.com/sql-server-2008
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN; Initial
Catalog=myDataBase;User ID=myUsername;Password=myPassword;
I am getting an error. Tried to give a simple username and password in the SQL server and tried it, still the same issue. please check the image. The Username and password which i used is the System login(windows authentication)
I just want to access the SQL server in the Windows 2008 machine.
Where should i check the credentials for this connection string ?
Thank you soo much for reading this patiently.
You can go to Connectionstrings.com to get more information on connection strings and its usages.
Try removing the port from your connection string (1433 is the default SQL port):
cn = new SqlConnection(#"Data Source=192.168.1.2;Initial Catalog=Test;Integrated Security=True");
For a connection using sql authentication (which would eliminate trust issues between machines) use the following (replacing xxxx and yyyy with the appropriate user name and password):
cn = new SqlConnection(#"Data Source=192.168.1.2;Initial Catalog=Test;Integrated Security=False; User=xxxx; Password=yyyy");
My sqlconnection is
SqlConnection(#"Data Source = John\Administrator; Initial Catalog = TicketingSystem; Integrated Security = true;");
I try to connect to the server but i cant this error pops up
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 think that the error is in the Data Source but I cant find it. When I open Microsoft SQL Server Management Studio it says that my server is "John" and the Connection is "John\Administrator" so please help me.
if the server is actually called 'John', then that is your data source. When you're running locally, you could probably just set Data Source=(local), tho.
Other than that, you'd need to specify a user to connect with
http://connectionstrings.com/
An easy way to get your connection string is
create a file called x.udl
double click on it
Follow wizard
open x.udl file in notepad
and inside you will find your connection string.
When I open Microsoft SQL Server
Management Studio it says that my
server is "John" and the Connection is
"John\Administrator" so please help
me.
That means you're logged on to your server's default instance (John) as Administrator. Remove the \Administrator part, and you should be good to go!
var cn = new SqlConnection(#"Data Source = John; Initial Catalog = TicketingSystem; Integrated Security = true;");
You can get your connectionstring from SQL Server Management Studio from the properties window.
Just click on the John\Administrator node, then press F4 to open the properties window, and search for the connection string there...
The Integrated Security=True part of your connection string means "connect as the current user". When you run an asp.net page, the current user by default is a special ASPNET account. You can "impersonate" a different user, but I'm guessing you haven't done that and the ASPNET user doesn't have access to your database.
It's possible that your server isn't configured to allow remote connections. You can check this in the SQL Server Configuration Manager tool.
Have a look here too.
Your Data Source is the server. So you only need "John", not "John\Administrator", that's not a valid data source. You're using integrated security which means it will use the same username as the program is running under and it's credentials, if it's on the domain.
Data Source = John\Administrator
Specifying John\Administrator as your Data Source, means ADO should connect to the SQL instance named "Administrator" on the server "John". Although instances are quite useful, I don't think that's what you want. (SQL Instances allow you to run multiple copies of SQL Server on one server)
As "d." mentioned, you will need to change Data Source to the correct server name or (local) ((local)\SQLEXPRESS if it's SQL Express).
Also, Integrated Security=true means that the user running ASP.NET, or at least the AppPool, will need access to the database. (The default is NETWORK SERVICE on pre-Windows 7, IUSR / IIS_USRS on 7).
If, however, you use ASP.NET windows authentication with <identity impersonate="true" /> then the user accessing the site will need access to the database.
Create a file called x.udl
Double-click on it
Follow the Wizard
Open x.udl file in notepad
and then you will find your connection string inside.