I have a c# application i want to connect it to sql server , the server and the application are used on the same machine , but I application cannot connect to the sql however if I installed the application on another machine and keeping the server in the first server they are connected !
The server is installed on windows 8.
Thanks
Please do the following -
Make sure your local database server has both Windows Authentication and SQL Server Authentication mode enabled. But
If you are using Windows Authentication then change the connection string to -
connectionString="Data Source=LENOVO;Initial Catalog=APP;Integrated Security=True;"
If you are using SQL Server authentication mode, which it seems since you are providing username yourself then
connectionString="Data Source=LENOVO;Initial Catalog=APP;Integrated Security=False;User ID=sa;Password=111"
Also, APP is usually a reserve keyword in some systems. Its better to avoid such name as the name of the DB.
If you are using default server instance you can use . or (local) instead of LENOVO
Check that the firewall is not preventing you from connecting.
Related
I know this is an answered question , however I have tried all the steps mentioned
like:
In SQL Configuration manager have Enabled TCP/IP,Shared Memory and Named Pipes.
Restarted the SQLSERVER
In Services.msc I have restarted SQLSERVER and SQL Server Agent
Added port 1433 in firewall's Inbound rule
Restarted PC number of times
Even Registered Local Server in SQL Server Management tools
I can open SQL Server Management tools
It was working fine and now its not. However I am able to login , then I cannot do any work on it, it gives me an error as :
Tried all the steps mentioned in the accepted answer HERE
I have some other application accessing same SQL Server and they are working fine.
My Connection string :
public SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Database=RMS_EXPRESS;Integrated Security=True;User Id=sa;Password=xxxxxx");
I also tried with the following connection string
Data Source=.;Database=RMS_TS1;Integrated Security=False;User Id=sa;Password=xxxxxxx
It gave me an error. "Login Failed for User 'sa'"
My application and Database are on the same PC
So , please help.
Thanks
You can not setup in your connection string both Integrated Security=true and User Id=sa;Password=xxxxxx
Integrated Security = true means it is using windows credentials (of the user the application is running into). A database connection cannot use both windows AND SQL Server authentication.
You should either:
Use Integrated Security=true and do not specify User Id nor password. This would use windows authentication.
Use Integrated Security=false or remove it althogether and specify both User Id and Password. This would use SQL Server user/password authentication.
Besides that, there was an adittional problem, originally you put Data Source=.\SQLEXPRESS but had to access default instance via Data Source=. which caused that error.
Allow Remote Connections enabled under Connections in SQL Server Properties.
Connecting to SQL Server with a SQL username and password is no problem at all.
These scenarios work just fine:
from C# code using SqlClient in ASP.NET / MVC
with ssms
with Visual Studio Connection Properties Wizard
For example, this works in an MVC project:
new SqlConnection("Server=DB1;Database=testdb;User Id=testuser;Password=testuser1;").Open();
But this identical code fails when run in a desktop application project. It causes the following inner exception error:
user name or password is incorrect
(yes there is a space between user and name)
I've tried:
making sure the protocols are right in SQL Config Manager.
opening up ports in the firewall on client and server.
connecting to another database to see if it's the database and not the client.
the NuGet version of SqlClient.
restarting the dev machine and the server.
ensured the Browser SQL service is running.
ensured the main SQL service is set to automatic and is running.
SqlClient, OLEDB, and ODBC along with appropriate connection strings.
Storing the username and password in a separate credentials object.
Specifying Integrated Security=False in the connection string
But still, nothing.
Why can I connect to SQL Server from everything except from code running in a desktop .NET application? Is something along the way stripping my password out before the connection happens?
Let's try :
data source=DB1;initial catalog=testdb;persist security info=True;user id=testuser;password=testuser1;
User must be a SQL user.
I've just start working on an ASP.NET Web Application in Visual Studio 2015 and SQL Server 2014. Somewhere, I need to connect to my local database and I use this:
ConnStr = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=my_db;Integrated Security=True";
It works fine when I run the application through VS in my browser. I can access the db and do whatever I want to do. But, when I publish my application in IIS Web Server, and then I open it in browser, it still works OK until I have to access the db. At that moment it throws SqlException:
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. .
Maybe it is a server name problem and I should use an IP and a Port instead of that name, but I'm not sure because I don't have experience on this.
You are correct: You should use server name and/or IP in your connection string.
Using something like "local" or "localhost" means that your code is not portable. Another option would be to store your connection strings in two separate config files - one for your local copy (for development and troubleshooting) and one for your server (for portability). I have two config files in my solutions:
Web.config
WebServer.config
Then, when I deploy to the server, I just delete Web.config and rename WebServer.config to Web.config. It's totally portable and you'll never have connection string troubles again!
Also noteworthy: you're not including credentials in your connection string, which means that you're using windows authentication when connecting to SQL server. When debugging through visual studio, the application will run as you - and if you have the needed permissions, it will work. However, when running in IIS, it won't be running as you (at least, it SHOULDN'T be) - so you could run into issues there, as well.
EDIT
This link might be useful for you: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
Your connection string should look like this:
Data Source=192.168.1.10,1433;Initial Catalog=YourDatabaseName;User ID=SqlUserName;Password=SqlPassword;Connection Timeout=60; Max Pool Size=3000;
(you can set max pool size and connection timeout to whatever you want - or omit them entirely.
Also, regarding your windows issues - you need to make sure that the windows account IIS is using has permissions to traverse your network and reach your SQL Server instance. That said, I suggest that you use a SQL account instead of windows authentication.
Since you are using integrated security in connection string you will have to modify the Identity of App pool under which your application is running.
The Identity will be your windows username and password.
To change the username and password you need to go the advance settings of the app pool and process model you can see identity where you can add your windows credentials
Hope this helps
There are two problems in you connection string:
"Integrated Security" means you are using the native windows system for authentication. Similar to opening SSMS on the database using your Windows password. IIS is now trying to connect to the database, and connection string is telling to use the process that IIS is running under.
You can create a non-windows user in SQL Server and put the credentials into the connection string. Or you can grant the IIS user DB privileges. Or you can a lot different things here, but theses are the easiest to get you moving.
THe second problem in the connection string is the data source. Is there SQL Server on you local machine? If so that's why it's not working. Try to run your app in VS but against the remote SQL Server. That should be your next step.
The problem was that I thought that SQL Server was installed automatically with VS or at least with SQL Server Management Studio. BUT NO. So, as far as I understand, till know I have not worked with a real SQL Server. When I checked SQL Server Configuration Manager there were nothing running at SQL Server Services and so I realized that I was missing something.
Then, I installed SQL Server Express and build my db there. Now it is working fine even when I publish it. The connection string is
Data Source=.\\SQLEXPRESS;Initial Catalog=my_db;User ID=username;Password=pass
It can also be:
Data Source=localhost\\SQLEXPRESS;Initial Catalog=my_db;User ID=username;Password=pass
I'm trying to test an application and I need some friends to test it for me who live elsewhere. I've got VS2010 installed on a WinXP machine and I have SQL Server 2008 R2 installed on the same computer. Both applications are installed on the C:\ drive of the WinXP computer.
I gave my IP address to my friends. They can see the website, however they can't access any of the data.
Is there some standard connection string I would have to access this data, or is this one of those "I have to sit in front of your computer to know what's wrong"-type things? The connection string I currently have in my web.config is:
<add name="MySiteConn"
connectionString="Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
If you have access via VS but not via the website, its probably because your (I assume) IIS account doesn't have access to the database. You are using integrated security.
It might be easier to add a user and password to the connection string, then give that user access to your DB and tables etc. I would just use the IIS user, but there are probably best practices in this case you can follow. Since you are hosting it all on XP I assume security isnt your top priority.
If the connection isn't the problem (if you have VPN set up or whatever means of connection), then it might be your SQL Server is not configured to get connection from other computers. Make sure you allow other connection to SQL Server, check SQL Configuration Manager and configure Firewall Inbound Rules. Otherwise I'd go to Elizabeth's answer.
I'm trying to write a program that will fetch data from a SQL Server database. The program is written on top of the .NET platform using C#. Furthermore, the program will be running on a Linux host (with mono). The problem that I am faced with is connecting to the database from the Linux machine, all of my code works fine from a windows host (since the server uses windows authentication), so my question is this: how can I connect to/authenticate with the SQL Server instance?
I've looked at http://www.mono-project.com/SQLClient, and it says to use a string similar to this: "Server=MyServer;Database=pubs;User ID=MyWindowsDomain\\MyWindowsUserid;Password=MyWindowsPassword;Integrated Security=SSPI" but that is not working. Is there maybe something I can do on the server (not likely that I'll be allowed to, but as a last resort)?
Thanks.
You cannot use sql server integrated authentication with linux, you have to use sql server authentication
As A. DIMO says, you cannot authenticate using integrated security on *NIX. It's not a windows box.
In your SQL Server Management studio, create a login that uses SQL Server authentication and not Windows Authentication. Ensure it has the necessary writes on the database in question.
Next, remove the Integrated Security=SSPI part of the connection string. That should do the trick.
While not a direct answer to your question, this site is useful for almost any connection string question: http://www.connectionstrings.com/.
At the risk of sounding like a commercial: It's your one-stop-shop for connection strings!