Unable connect to SQL Server 2008 R2 - c#

I have a C# project with SQL Server 2008 R2 and the project runs with no problem on my local machine and when I publish it to the server Win2008 R2. But the project doesn't work and can't connect to the server with no error. All the database setting check and I enabled TCP/IP setting and etc. But still not worked. I think the problem is with connection string that is below.
When pages going to connect to the database, nothing happened in browser and browser stick in loading mode (waiting for respond).
Connection string on my computer
Data Source =(Local);Initial Catalog=Bulk;Integrated Security=True;Timeout=0;Max Pool Size=5000;
Connection string on Windows server
Data Source=My Ip Windows Server,1433;Network Library=DBMSSOCN;Initial Catalog=Bulk;User ID=administrator;Password=My Windows Login Pass;Timeout=0;Max Pool Size=5000;
I got 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

In your connection string of Server you have:
Data Source=My Ip Windows Server,1433;Network Library=DBMSSOCN;
To connect via IP Address and Port of 1433 and over TCP/IP protocol
Initial Catalog=Bulk;
Bulk is your database name
User ID=administrator;Password=My Windows Login Pass;
Authenticate via SQL Server connection by administrator login user and its password.
I think your problem is in third part that you want to connect to SQL Server by your Windows user name; You can see valid logins of your SQL Server in Security branch of your registered server in SQL Server Management Studio that will shows your Windows user name like Domain Name\User Name so you need to change User Id to that.

Configure the Windows Firewall to Allow SQL Server Access. open port 1433 in windows firewall.
see this link. Or this.

Please Try to connect in your local Sqlserver With Live Login Detail,
If you are Success Then There should be Permission Need to set From the Live Hosting Server
if is it possible then please add the More specification.

You should log the errors that occur, e.g. in Application_Error if it's actually an ASP.Net project.
Using the log you should get errormessages that hint you or enable you to give more details so nobody has to guess what your actual issue is.
Regarding your issue:
Did you give your Windows account the necessary READ/WRITE access on SQL Server?
On MS SQL Server the account needs access to:
1. Be added to the general Security/Logins for an actual match of Windows account to Login.
2. Be added to the specific database Security/Users tab with the necessary database role membership.
In addition to specifiying your Windows account + password in the connectionstring: maybe it's better to run the application pool using a service-account and use
Integrated Security=True
and remove
User ID=administrator;Password=My Windows Login Pass;

I have changed my connection string to this and problem solved.
public static string ConnectionString = "Data Source=My Ip,1433;Initial Catalog=My DataBase;UID=sa;Pwd=My Pass;Timeout=0;Max Pool Size=5000;";
In sql server management studio
Click "Security" => "Logins" => "sa" right-click and click properties
in the left side click "Status" and in the right side,Choose enable in login

Related

SqlServer2012-Error 40-Could not open a connection in SQL Server

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.

Connect a Winforms app to a SQL Server from a computer without SQL Server 2017 installed

To start, I am fairly new to both Visual Studio and SQL Server. I am attempting to run an application from computer B which does not have SQL Server installed, and have it connect to a SQL Server database that is hosted on computer A. Both computers are connected over Wi-Fi to the same network.
I have enabled remote connections in SSMS as well as started the SQL Server Browser in services.msc. Exceptions for 1433 and 1434 ports have been added in the Windows firewall out and in rules. In SQL Server config manager I have TCP\IP enabled and the IPAll section in properties has the TCP port set to 1433.
The problem is when I try to add a user to a login on SSMS it only looks for logins on my host machine(I don't know if this is how it should work but I digress). So I am using a SQL Server login username and password (that is enabled in SSMS) in my connection string in the Winforms app.
However, when I run the published app from computer B, I get this error:
Login Failed. The login is from an untrusted domain and cannot be used with integrated authentication
How would I rectify this problem to make computer B a trusted domain?
Like I said I am new to SQL Server and VS so a lot of what I found online was either too complicated or I had already done. Thanks in advance.
Edit: My connection string is "Data Source=DESKTOP-24DR8LC\SQLEXPRESS2; Initial Catalog=HouseFinances; User ID = user; Password = password;Integrated Security = False"
Whenever I need to check a connection string I do the following:
Create a new text file on the desktop called test.txt.
I rename it test.udl.
Double clicking on that file will bring up the Data Link Properties window.
Start on the first tab and select your source, go to the second tab and fill in the properties for your connection.
Click the test connection button.
If everything connects ok, close the window and open the file using notepad.
In there you will find your connection string.
Hope that helps.
Your connection string is specifying Integrated Security or TrustedConnection. You need to remove those options and add the User and Password for the SQL account

Why can't I connect to the SQL Server database when I create setup file in Visual studio?

I create application using C# windows forms which uses local SQL Server database server to store / read data using this connection string:
SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=mydb;Integrated Security=True");
After that I created a setup file (exe) then I installed the setup file in same PC and the application works well and can read data from the SQL Server database.
Now the problem came when I went to another PC I installed SQL Server and I manually attached the same database (mydb) and installed my application and it did not work. The error is shown in the screenshot:
I have different SQL Server name in the other PC but I specified in the connection string that the SQL Server is Localhost so I don't know why it didn't work. Please help me how to fix this error. Thank you
Make sure Sql Server is set to allow Tcp/Ip connections and that it's set to allow integrated security logins. Then, make sure the user on that machine is granted the correct access to the database. You may have better luck using Sql authentication over Windows Authentication here, if you want to be able to distribute this application and have it "just work".
Finally, if Sql Server here is meant to be a simple datastore for a typical local desktop application, it's probably overkill. Full Sql Server (including Express Edition) is a server engine. It works best when it's the only thing running on the machine and can use up all of the resources on the machine in order to cache data and handle requests from many remote machines. If you just want a local data store for a typical desktop application, an in-process engine like Sql Server LocalDb, Sql Server Compact Edition, Sqlite, or even MS Access would be a much more appropriate choice.
You need to set up Windows authentication for the user that's logged in on the other computer. Because you are using Integrated Security, it will attempt to connect to SQL Server using the windows login of the current user. This will be a different login on different computers, so the SQL Server on the other computer needs to have that user added.
You need to start SQL Server Browser Service. It lets your app connects to SQL Server:
SQL Server Browser listens for incoming requests for Microsoft SQL
Server resources and provides information about SQL Server instances
installed on the computer.
To enable it:
In SQL Server Configuration Manager, go to Properties => Service tab => Start Mode = Automatic.
Or
This computer => Manage => Services and Applications => Services => SQL Server Broswer
Note: You need a sql specific user for connect to it, you can not go with Integrated Security for every machine.
Create a Sql Server user and allow it to access the Database:
--//Creates the login AbolrousHazem with password '340$Uuxwp7Mcxo7Khy'.
CREATE LOGIN AbolrousHazem
WITH PASSWORD = '340$Uuxwp7Mcxo7Khy';
GO
--//Creates a database user for the login created above.
CREATE USER AbolrousHazem FOR LOGIN AbolrousHazem;
GO
Reference

ASP.NET SqlException when publishing in IIS Web Server

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

Regarding local network connection string

I need some opinions and ideas on how to choose and use sql connection string? i do search fom sqlconnectionstring's website, but i tried for few types couldn't work with my network system. can pros out there help me please?
My condition is like below:
I got 1 computer for hosting and i installed sql2008 express server(microsoft sql management studio), and login as window authentication. (ip: 192.168.1.101)
With my created database was with out any security login id and password. (.MDF)
I got another computers work as client to connect and access the hosting computer which hosting the sql server. there ip had set to static (192.168.1.102, 192.168.1.103, 192.168.1.104)
i had tested this connection string but i failed to get connect
Data Source=192.168.1.101,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase
I did configure the firewall to allow the 1433 ports which is my sqlserver current using port. Checked sqlserver tcp:enabled, via:enabled, Namepipe:enabled, shareMemory:enabled. Setting of allowed computer remote-able.
So far these are what i configured, but still unable to connect, can anybody guide me?
Maybe because you're using a windows authentication. If you're connecting to SQL server using another computer, the login of that computer should be:
A part of a domain where the server is (i.e. [domain]\myusername).
It should be added to the SQL Server's Security\Login. Go to management studio, then in the object explorer under the server, go to the folder Security\Login. You should see a list of users under login. Your user (other computer's login) should be there.
Now, if you're not part of a domain, you should use a SQL server authentication. I haven't tried using windows authentication in two computers which is not part of a domain.

Categories

Resources