Connection between windows service and sql database - windows-services

I am trying to establishing a connection between a Windows service written in C# and a SQL Server Express database. But when connection.open is called I get this exception
System.data.sqlClient.sqlexception:
{"Failed to generate a user instance of SQL Server due to a failure in
starting the process for the user instance. The connection will be
closed."}
But the same code works fine when I run it as console application.Can someone please help me with this??
_connection = new SqlConnection("Data Source=.\\MSSQLSERVER1;AttachDbFilename=D:\\vinay\\project\\LightHistorian\\LH_DB.MDF‌​;Integrated Security=True;User Instance=True;");
i gave same credentials as local system.It works a times and doesnt at some other time.But when ever this exception happens,If i restart the machine it works.Wonder what the problem is??
I have used same connection for both windows service and console application

When you execute your console application, the EXE runs under your user credentials.
When you execute your service, which are user credentials? Is your service running under Locl Service Account? Or System? Or what?
Try to manually set the credentials your service must run with (in Windows services panel, check your service properties) and try to execute it with the same user/pwd you run your console app with.
UPDATE taken from here:
Using an User Instance on a local SQL Server Express instance
The User Instance functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server 2005 instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL Server instance to a user with limited administrative rights on the computer.
I could think that your service credentials are not granted to create a new SQL server instance, so try to remove that part.
Anyway, set user credentials on service properties and see what happens.

Related

Cannot connect to Sql Server from Visual Studio (in the process of creating a new Entity Data Model): login failed for user 'sa'

I know there are similar questions, but I have a specific problem.
Microsoft Visual Studio (2017) pops up this error when I try to create a DB connection for my Entity Data Model
The attempt to attach to the database failed with the following
information: Login failed for user 'sa'.
What I've already checked:
1) The sa user's password hasn't expired or isn't different than what I have specified.
2) The sa user has not been disabled, either by being denied permission to connect to the database engine nor having the login disabled.
3) My instance of SQL Server 2016 (Developer Edition) is configured for SQL authentication.
4) SQL Server is running and I also can connect to it by SQL Server Management Studio (v17.1) via sa and Windows Authentication as well.
What the problem could be?
UPDATE:
I tried to create a DB connection, when adding ADO.NET Entity Data Model -> EF Designer from database -> New Connection -> Microsoft SQL Server Database File (the SchoolDB.mdf wasn't attached to SQL Server... maybe this is the source of the problem)
To be honest, I tried to carry out the same action (make a connection to database (SchoolDB.mdf) and attach the SchoolDB.mdf file to SQL Server), and then Visual Studio gave me a hint:
The database file that you are attempting to connect is not compatible
with the current instance of SQL Server.
Then VS suggested me to upgrade the database file (SchoolDB.mdf).
After I'd done an upgrade of the *.mdf file (that I was trying to attach to SQL Server and make a connection to this DB after that), I could establish a connection to this database (already attached to SQL Server).
Screenshot
PS: I found out that SchoolDB.mdf was created by SQL Server 2012, while I tried to connect it to SQL Server 2016.

Umbraco cannot start. A connection string is configured but the Umbraco cannot connect to the database

I'm using Umbraco CMS v7.4 with SQL Server 2014, the project was working fine until a few hours ago, when an error started showing up:
Umbraco cannot start. A connection string is configured but the Umbraco cannot connect to the database
The error occurs when I tried to open the website, see the image below:
I tried the following:
Checked to see if SQL Server is running
Checked the connection string credentials in web.config
Any help is appreciated.
After a few tries, even I loaded the backed up DB, I found that it caused by the DB login, the user in the connection string was having a password expiration enabled (the password had expired).
So, this is what I did:
Open the SQL server
Logged in as the user used in the connection string
Created a new password
Updated the web.config connection string "Password"
Hope this will help
So, in my case I tried to get started with the existing project. I got all the files, restored DB backup locally, made sure all permissions are set for IIS AppPool user etc. But I kept getting the error in question. It turns out that I cannot use server=localhost;database=... in connection string. After I replaced it with server=.\;database=..., it started to work right away.
Also note that if you're running it in Azure with an Azure SQL database you'll have to go to the Azure SQL Server settings and under "Firewalls and virtual networks" you'll need to tick "Yes" for "Allow Azure services and resources to access this server".
I was getting this error when running an Umbraco project locally, with the DB pointing to an instance on Azure. In that case, this problem happens if you haven't allowed the IP access within the Server Firewall of the DB in Azure.
You can fix this by finding out your IP and then adding it to the DB:
Click All Resources within your Azure portal, then click on the SQL Database your project points to.
Click Set Server Firewall on the right-side window.
Add in your IP as the start and end points, and call it something memorable.
Click Save and once you get the confirmation message that It has been applied, refresh your site locally.
I experienced the same problem and here is how I fixed it.
Right-click on your localhost in SSMS and select Properties
Got to the Security page and select "Select authentication" as "SQL Server and Windows Authentication mode"
Hit OK.
Go to Services and find your SQL Server service and restart it.
Check your local umbraco website now, you should be able to see it without any issues. If you still have problems, then you might need to check your local setup and permissions.
Further details are below and on this blog post.

Can't Connect to DelphiXE5 AND SQL SERVER 2012

I have a Delphi application that is connected to SQL Server 2012. On my local, I've been able to connect successfully. I've also tried it on another VM, and it work fine. I deployed it on a Server, and it won't connect to the database. I check the connection string; it looks fine. I am not sure what I am doing wrong here.
SQL Server 2012 is installed on a different server
I can reached the server
I check the provider and it's using SQLNCI11
I have SQL Server 2012 Natice Client Install
I check and make sure that the username/password have access to the database, and they can login
What am I missing here?
Despite the fact that you can change the vendor lib in the provider, that change will have no effect as the name of the DLL is hard-coded into the provider. It will try to use sqlncli10.dll, no matter what the property is.
Install sqlncli10.dll and it should work.

Delphi DataSnap SocketServer "Failed to create key 211"

If I try to terminate the Borland SocketServer application for Delphi 2009 the message above appears.
The application window remains open and I can only terminate the application in the task manager.
Operating system is Windows 2000 Professional SP4
The same error also occurs with the Delphi 2007 version of scktsrvr
scktsrvr attempts to store its settings under HKEY_LOCAL_MACHINE\SOFTWARE\Borland\Socket Server registry key which probably fails under a limited user account. Try running it as administrator, or as a service.
Edit: you can also modify it to use HKEY_CURRENT_USER, for example. It comes with source code.
Run the socketserver as a service, and add the proper permission to the registry key the service user needs to set. Remember that the user you use to run the SocketServer is important. The server acts like a proxy, and in turn call the DCOM server that implements the remote server. Unless changed, the user of the connecting process is used for DCOM security, thereby you have to ensure the right user is used, or you may lead to big vulnerabilities.

How can I create a local user profile for the anonymous user of an ASP.Net MVC application under IIS 7?

I've been experimenting with ASP.Net MVC, and have come across a problem that is probably not specifically MVC related. But I cannot get the authentication in the default MVC application (the one created by the wizard when you create a new MVC project) to work properly under IIS 7 on Windows 7.
If I run under the Visual Studio environment, it works, but if I switch the settings to run under IIS instead, I get the following exception trying to submit the login or registration:
Failed to generate a user instance of
SQL Server due to failure in
retrieving the user's local
application data path. Please make
sure the user has a local user profile
on the computer. The connection will
be closed.
I believe that this is because the website runs under my own account in Visual Studio, but under the IUSR account in IIS. Google searches on the exception message have been unhelpful so far.
So, can one create a local user profile for the IUSR account? If so, how? Is there something else I should be doing to get the SQLExpress engine to work under the anonymous account in IIS 7?
I also tried configuring the IIS website to use my account, but since this is my home machine, my account doesn't have a password, and it appears that IIS won't let a website be configured to use an account without a password. Or, since this is my first experience with IIS 7, and configuration feels very different than IIS 5/6, I am just missing the right setting that will let me configure the account to use for anonymous access.
EDIT: Some additional information. If I empty the App_Data folder and try again from IIS, SQLExpress attempts to create my database and fails, but the exception message has further information with the following suggestions.
SQLExpress database file auto-creation
error:
The connection string specifies a
local Sql Server Express instance
using a database location within the
applications App_Data directory. The
provider attempted to automatically
create the application services
database because the provider
determined that the database does not
exist. The following configuration
requirements are necessary to
successfully check for existence of
the application services database and
automatically create the application
services database:
If the applications App_Data directory does not already exist, the
web server account must have read and
write access to the applications
directory. This is necessary because
the web server account will
automatically create the App_Data
directory if it does not already
exist.
If the applications App_Data directory already exists, the web
server account only requires read and
write access to the applications
App_Data directory. This is necessary
because the web server account will
attempt to verify that the Sql Server
Express database already exists within
the applications App_Data directory.
Revoking read access on the App_Data
directory from the web server account
will prevent the provider from
correctly determining if the Sql
Server Express database already
exists. This will cause an error when
the provider attempts to create a
duplicate of an already existing
database. Write access is required
because the web server accounts
credentials are used when creating the
new database.
Sql Server Express must be installed on the machine.
The process identity for the web server account must have a local user
profile. See the readme document for
details on how to create a local user
profile for both machine and domain
accounts.
I've pretty extensively confirmed that the first three suggestions have been satisfied. The fourth seems to be the cause of my problems, but I can't figure out how to do that. And although the suggestion claims there is a readme document that describes it, I have not been able to find that document.
I got this problem as well running under medium trust. The process that creates the database requires at least High trust. You can check this by looking in your Web.Config for
<trust level="TrustLevel" />
If there is no trust specified in your Web.Config, try adding it and set it to either Full or High. If this doesn't work, there is a machine.config in your IIS which you would need to modify.
That being said, the best route I have found to solve this is to just use aspnet_regsql.exe to create the necessary tables and then change the connection string in your Web.Config to look at it directly.
I solved "The directory 'LocalApplicationData' does not exist." error when running an SSIS package through an SQL Job by ensuring the SQL SSIS service and SQL Server Agent service were running under the same account as the swql job was set to use!
This in my case was a domain account.
Solution: Try UNINSTALLING any updates before you started experiencing the issue. I spent countless hours - wasted hours that I will never get back in my life - reading and following every solution possible without success. I uninstalled all SQL Server updates and now everything works fine.

Resources