LINQ to SQL works only in debug mode? - c#

This is my code :
DataClassesDataContext data = new DataClassesDataContext();
var hey = from p in data.Provas
select p;
Response.Write(hey.First().title_it);
if I open this playing start to Visual Studio, it opens http://localhost:51180/Website1/ and I get the value taken from DB.
But, if I open the website with http://localhost:8080/ (which usually I open with this) I get :
System.Data.SqlClient.SqlException: Cannot open database "Website1" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\Website1'.
How can I should settings? IIS problem?

As #usr pointed out the answer is in front of you.
When you run the app in debug mode form Visual Studio the user connecting to the database is the user logged into the PC and running Visual Studio - the odd are that this user (you!) is an administrator and has administrator rights with SQL Server (I'm assuming a local instance).
When you run the app via the local installation of IIS (which it is reasonable to assume is the full version, not express) then, unless you change the app pool for the application, it will be running as a local system user (right now I forget which one, you can go look at the app pool to see) and by default that user will not have access to the databases in SQL Server.
To fix this you need to do one of two things either:
a) Change the user in the app pool to one that has rights to the database in SQL Server (if you want good practice then you should define an explicit user)
b) Grant the appropriate rights to the appropriate system account to access the database
(Note that in both cases you're going to need to add the user to SQL Server using Management Studio or equivalent)

Verify login names on http://localhost:8080/. Possibly this article will help you.

Related

Login failed for user in SQL Server 2008 for Windows application

I have two PC (both have Win7). On PC "A" I have installed Microsoft SQL Server 2008 and developed a Windows application. On PC "A" my application is running well and there is no connectivity issue.
But when I run this application on PC "B" (connected with PC "B" through LAN and able to share any file) it shows error message login failed for user'....'.
I have already make some possible solution but can't solve it.. Need help
My connection string is:
Data Source=WIN7-PC\PRABHAT;Initial Catalog=plproject;User ID= win7-PC\Prabhat;MultipleActiveResultsets=true
Is there any problem?
The SQL Server error log contains addtional information about failed logins. Look there and find the code that maps to the table listed here: https://support.microsoft.com/en-us/kb/555332
One possible issue is that you are running as a machine account that doesn't exist on machine B. You need the Windows identity to flow from one machine to another and this is typically accomplished with a domain service account. If you want it to work from machine B, you need to access it with an account machine B is aware of. Supplying the User ID is typically for SQL Server authentication, not Windows authentication. You could try adding the password and see if that works.
Here is a breakdown of SQL Connection String syntax: https://www.connectionstrings.com/sql-server/
You need to enable TCP/IP for cross machine SQL SERVER access. Just follow below mentioned steps to ensure whether it is enable or disabled.
Click on window start menu.
Navigate to Microsoft SQL SERVER => Configuration Tool => SQL
Server 2008 Configuration Manager.
In Opened window Navigate to SQL SERVER Network Configuration =>
Protocols for <MSSQLSERVER> (Protocols for your SQL server
instance name)
Check Whether TCP/IP is enabled or not.

SQL Server database is readonly error

I am trying to upgrade one of our web application from Windows Server 2003 to a new box with Windows Server 2012. The application allows us to create multiple instances of another application as virtual directories within a website. Its more like we create an instance of a web application on run time for every signup which is customer specific. Each instance consist of SQL server MDF file that it attaches to SQL server.
The problem I am having is that MDF file is from SQL 2008 and system fails to upgrade it to 2012. If I upgrade it manually then system complains that database is in readonly mode. I have already set application pool identity to Network Service account and I believe it should have sufficient rights to attach/read/write the database. I am unable to reproduce the problem on local system where I am using Windows 8 and SQL 2012. Any help would be appreciated.
Failed to update database "some.MDF" because the database is read-only.
Network service has not part in readonly database configuration.It could depend from login role.Have you check via ssms if login user has been bind to the database and if its has the right permission.
Take a look to the image below:
if not you need to setup right configuration for your customer and so on.
The only way to get it working was by allow Network Service full control permissions using NTFS folder permission dialog.

how to access localDB from Windows Service

I need to use data base locally. I have created database in (LocalDB)\v11.0 Server with Visual Studio 2012. Everything is ok when connecting from e.g. console application. But my application is Windows Service. trying to connect to my data base outputs with:
"... The login failed.Login failed for user 'NT AUTHORITY\SYSTEM"
Is there any solution for this? Maybe I should think of setting up other db server for it? If yes, how to set up it so that it would be visible only locally?
So i found out solution myself, how to connect instance of (localdb) from Windows Service which runs as Local System:
I used this articles:
http://technet.microsoft.com/pl-pl/library/hh212961.aspx
http://dba.fyicenter.com/faq/sql_server_2/Verifying_a_Login_Name_with_SQLCMD_Tool.html
http://social.technet.microsoft.com/wiki/contents/articles/4609.troubleshoot-sql-server-2012-express-localdb.aspx
So i shared my localdb (as in 1st article) the problem was I couldn't connect with sqlcmd named pipe np://. I found anwer in 3rd article:
When using sqlcmd, ensure that you are using the SQL Server 2012
version (found in %Program Files%\Microsoft SQL
Server\110\Tools\Binn). If you have previous versions of sqlcmd
installed, calling sqlcmd alone from the command line will most likely
use the old version (which isn't localdb-aware) since the older path
appears first in your PATH environment variable. It may be a good
idea, in general, to manually adjust your PATH environment variable so
that the 110 versions are picked up first.
This small information in fact was crucial;)
so i created user: user with password: pass321!#.
In my windows service my Sql connectionString looks:
"Data Source=(localdb)\\.\\MyInstanceShared;Integrated Security=false;User Id=user;Password=pass321!#"
Integrated security set to false value is also important.
Maybe it will help somebody.
Probably your connection string is using the current Windows User account (the account that runs the service could be the one seen on the error) to connect to the database (and that user account does not have access).
What I would do is that I would change the database to allow "mixed authentication" (this means SQL users and Windows users too) and then I would create a new user, assign that user the required permissions to work with your app and then use that userName and password in the connection string from your windows service.
You could also leave the DB as it is and add user "System" from Windows and then assign the required permissions to it. You can just add "dbo" profile to a user to make it work fast (but check security later before releasing your product).
I think that you need to allow remote connection to your SqlServer.
In the Sql Server configuration manager, under SQL Network Configuration, chose your server and check that TCP/IP is enabled.
In my case, doublic click on TCP/IP, go to the bottom "IPAll" and set dynamic ports empty and TCP Port to 1433.
Let me know if it's enough.

Unable to connect to SQL Server 2008 in .Net

I am working on a C# web service.
I have deployed a web service which is working perfectly on my local system but when I try to run the same service on my virtual dedicated server it is returning an error.
This is my connection string
Data Source = <serverinstance>\\SQLEXPRESS; Initial Catalog = DomainTable; User ID= <serverinstance>\\admin; Password = <Windows_Login_Password>
Before this I was using this connection string
Data Source=<serverinstance>\\SQLEXPRESS; Initial Catalog=DomainTable; Integrated Security=SSPI";
None of them is working for me. please help its really really important
Data Source=serverinstance\SQLEXPRESS;
-----------------------------------------^ only one slash here
Also make sure that the remote server can see whatever you've referenced as <serverinstance>. You may have this as localhost but your web server does not consider your machine to be localhost. It may also not be able to reach it by name, particularly if it is in a different domain.
If you are trying to use Windows Authentication, there is no way on earth you should be using:
User ID = serverinstance\admin; Password = Windows_Login_Password
Wow, never hard-code a Windows password anywhere. If you want to use SQL authentication, then set up . Otherwise you should be using this instead of User ID / Password:
Persist Security Info = true;
And ensuring that the IIS user has been granted access to your SQL Server.
Try just;
"data source=.\\SQLEXPRESS;Integrated Security=SSPI;initial catalog=DomainTable"
I believe the user name / password are generally only required for remote connections, and you must set them up for the specified DB. The above connection string should get you going on the running instance of sqlexpress on the local machine if you have not made login credentials required.
Edit
If you are using IIS to host, here is a chunk from a recent article I did for deploying and setting up App pools to work with SQL Express 2008. Even if you are not using IIS, you will still need to do something like this to get your hosting environment set up correctly with SQL Server.
Step 1 Create a DB Logon for the IIS USER
In the security section in MS SQL SERVER, need to right click users and your IIS APPPOOL(IIS APPPOOL\apppoolname, created in Section one, Step 2).Check out dp.'s answer here for more step by steps - Add IIS 7 AppPool Identities as SQL Server Logons
Step 2 Setting Permissions for the IIS USER on the db
Right click on the database name, and click properties. In permissions section add your APPPOOL user that you just created, be sure to give him the Execute \Select permissions needed .
Note: Website1 needs Execute, Select, Delete, Insert permissions as well. Website2 is also using this app pool which it shouldnt be because we dont not need to alow the user to have that kind of functionality from within the trimdynamics application. We could seperate the app pools so that our trim dynamics IIS USRS is only getting Execute and Select permissions.
If this does not help, it is hard to tell whats really going on without concrete knowledge of your hosting structure, as there are lot's of gotch-ya's when deploying from development to production environment :(

C# application accessing remote sql server 2008 instance ... not working

I have a console app that needs to connect to a remote sql server 2008 instance....
this particular line throws an error even though i have access to that database...
connection.open() is the line that is throwing the error...
Make sure that Sql server 2008 instance available at your system where you are running your application.
Atleast client version of Sql server 2008 should be installed on development machine.
Check connection string and server system also allow network connection.
Make sure the connection string in you console app uses the same credentials and settings that you use to successfully connect in management studio.
Are you using Windows authentication in one place and a SQL login in another?
Have you been able to connect to this remote server before? If you have been able to connect previously, but cannot do so now, that may be an indication that a backup copy of the database, from another server, has been restored to that server.
When you write that you "have access" to that database, how do you connect? Close your copy of SQL Server Management Studio and re-open it; when you get the Connect dialog, be sure to enter the user credentials that your console app uses to connect. Do you see your database? Are you able to execute stored procedures, run SELECT statements, or otherwise do what you would expect?
If you do not see the database--or if you cannot do the activities you expect, you have probably restored a database from another server. This is very common--a database developer is working on the next revision of the database on his development machine; when it comes time to deploy he backs up the database, copies it to the server, and restores the database. Now he can't connect--and can't understand why.
The reason is that the Login and User are presented in the SSMS UI as strings--but in fact they are integer IDs. You will have to do a bit of scripting to delete the invalid User record from the database in order to assign it to the new server's Login.
If this sounds like your problem, respond--I'll check back in a bit, and follow up with more help.
JM

Categories

Resources