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.
Related
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.
When I try to connect to the MS SQL server in the local domain with SQL with SQL server authentication using the following code fails:
SqlConnection sql = new SqlConnection(conString);
sql.Open();
sql.Close();
I can connect to my local SQLExpress test database just fine, using either windows or sql authentication. I tried using many different connections strings for the domain database, including using the connection string generated by (successfully) adding the server as a DataSource. What am I doing wrong?
EDIT: When I add the server in the server explorer of visual studio, I can connect successfully. I would assume that the connection string thereby generated is valid:
Data Source=mySubDomain.myDomain.local;Initial Catalog=myDatabase;Persist Security Info=True;User ID=myUser;Password=myPassword;
I checked and remote connections are allowed. I do not have permission to access the windows server which the sql server is running on, so I can't check any further settings.
EDIT 2: The following message comes with the exception:
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)
EDIT 3:
I tested my application on a different computer in the same domain, and it worked. Does someone have a clue what's going on or how I could find out?
The Group Policy or firewall (or both, not sure) of this company domain make my .exe file (and any other .exe) behave in the following way:
Launching it from C:\Program Files (or C:\Program Files (x86)) as any user works fine
Launching it from anywhere else as a normal user is not allowed and fails
Launching it from anywhere else as a local admin will execute the application, but connections are blocked
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'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.
I Get this error message whenever I tried to up my aspx page.
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
whic is connected in this connection string
SqlConnection conn = new SqlConnection("Data Source=192.168.xxx.xxx;Initial Catalog=DBSample;User ID=dev;Password=pass;Integrated Security=SSPI;"))
The weird thing is that the server that I'm connecting has already hosting some aspx page. I don't knnow if there's missing in my connectiong string Thanks. and I know the server that I'm connecting to is already allowed remote connection since it's already hosted some aspx websites. :(
Thanks!
Do you need to add an instance name to your connection string? Do you have the SQLBrowser service running on the target machine, or do you have to specify a port for the instance?
You also get that very same error when the database doesn't exist at the location that you are trying to connect to. Have tried looking at the connection strings of the aspx pages that are successfully connecting?
edited: Specifying Integrated Security=SSPI means you will be using Windows authentication to login to the database. What user is your aspx page running as (check your app pool)? Does it have the rights to log in to the database? This could also explain why it works on one server but not another.
Are you trying to connect to a hosted SQL Server over TCP/IP?
The reason I ask is some firewalls block traffic over Port 1433.
If not then it is simply a case of validating the connection string details and ensuring the SQL Server Engine is actually running...
Have you EVER been able to connect to this database from the PC you are currently attempting this connection on?