Web Service running locally won't connect to local database - sql-server

I have a web service that's running locally via IIS. Using IE to navigate to it to test a specific service instantly returns 0 (default value returned by the stored procedure that gets called when something invalid is entered). However, if I change the connection string in web.config to connect to the server where the live database is hosted it works just fine. If I change the connection string to an invalid name for the server and try to test the service, it will load for about 15 seconds before returning 0, so I'm fairly certain the whole connection string is correct. I'm thinking it's something permission related with either SQL, IIS or both.
My connection string is:
<add name="LocalSQLServer" connectionString="Data Source=.\SQL_R2;Initial Catalog=localDBcopy;Persist Security Info=True;Trusted_Connection=Yes;Pooling=False" providerName="System.Data.SqlClient" />
When I connect it to the live server I replace Trusted_Connection with a name and password.
Why can't I connect to my local database? Or if I am connecting, why can't I run a stored procedure that's called through the web service? Any thoughts, or need any additional info?

Related

How to fix "Server name cannot be determined. It must appear as the first segment of the server's dns name (servername.database.windows.net)"

I have a web application in visual studio 2017 .NET framework 4.7.1 with identity, that connects to azure sql server database. I am getting an error after submitting login form. The exception message is:
"Server name cannot be determined. It must appear as the first segment of the server's dns name (servername.database.windows.net). Some libraries do not send the server name, in which case the server name must be included as part of the user name (username#servername). In addition, if both formats are used, the server names must match."
The error is only in the web server (azure app service). When I run it locally (localhost) it works fine.
I did what error message suggests but, it did´nt work. I searched about this but, none of the suggestions or solutions I found have worked.
If I remove login stuff, application is able to connect and works so, the problem seems to be the connection string I use to connect to identity tables.
I have tried this connection strings in web.config but none of them have worked:
1.
add name="DefaultConnection" connectionString="Server=tcp:myServer.database.windows.net,1433;Initial Catalog=myDatabase;Persist Security Info=False;User ID=myUser#myServer;Password=myPassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False" providerName="System.Data.SqlClient"
2.
add name="DefaultConnection" connectionString="Data Source=myServer.database.windows.net;Initial Catalog=myDatabase;Integrated Security=False;User Id=myUser;Password=myPassword;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
First, did you set the Allow access to Azrue Service ON?
Please check your Azrue SQL Database firewall settings on portal:
Second, get the correct connection string:
Others, please see: Troubleshooting connectivity issues with Microsoft Azure SQL Database.
Hope this can helps.

Cannot get classic ASP app to connect to database

We have an application which we are attempting to revive that was written in classic ASP in 2010. So far, I've done the following:
I created a Microsoft Server 2016 instance on Amazon EC2
I installed IIS Server
I created the website inside the IIS Manager, added/linked the files, and successfully ran the application on http://localhost
I installed SQL Server 2017 and imported the DB Backup (and successfully created the database)
I created a SQL Server user and tested that I can access the DB using the created database user
I enabled SQL Server Login Authentication and Windows Authentication Mode
I added the connection to IIS Manager.
I confirmed that web.config.xml has the proper information.
Im getting the following error:
2018-01-21 01:36:16 ::1 POST /intranet/process.asp |178|80004005|[Microsoft][ODBC_SQL_Server_Driver][DBNETLIB]SQL_Server_does_not_exist_or_access_denied. 80 - ::1
I noticed in the code theres a file that has a Session config for a database connection that doesnt look the same.
Im not sure what to do at this point. We need to figure out how to connect this database to the application
Heres my web.config.xml in the root:
<configuration>
<appSettings />
<connectionStrings>
<add connectionString="Server=server\SQLEXPRESS;Database=dbname;User ID=dbuser;Password=dbpass" name="SqlServer" />
</connectionStrings>
</configuration>
Here's my web.config.xml inside the intranet folder which is for an admin area:
<add name="WEB"
connectionString="Server=server\SQLEXPRESS;Database=dbname;User ID=dbuser;Password=dbpass"
providerName="System.Data.SqlClient" />
I noticed a file global.asa that has this:
Sub Session_OnStart
Session("ConnectString") = "Server=adifferentserver;Database=adifferentdatabase;UID=adifferentuser;PWD=adifferentpassword;Driver={SQL Server};"
This is my first time using ASP so you'd have to guide me for what other files you need.
The global.asa file in Classic ASP acts as an initialisation script it is commonplace to set long-running variables such as Application and Session variables there.
If this file contains a connection string variable then that is the value to change to your migrated database. At the moment process.asp is using Session("ConnectString") and ADODB is trying to locate adifferentserver using the SQL Server provider and failing. Once you correct the connection string using the required provider and server, database combination the page should work.
The other two values come from the web.config which was brought in with IIS 7.0 and above, it's designed for Microsoft .Net and Classic ASP does not know about connection strings configured inside it. The likelihood is there is another web application that uses ASP.Net that was making use of those connection strings.

Getting Error: 'Cannot execute as the database principal because the principal does not exist...'

I am getting the error 'Cannot execute as the database principal because the principal "kc" does not exist...' when I try to execute a Microsoft web application locally that connects to a SQL Server database. It application works fine when it is connecting to a remote server.
I copied the database on the remote server to my local computer to make changes.
This is the connection string when connecting to the remote server:
<add name="Database" connectionString="Server=KC-DEV2;Database=ICKC;User Id=kc;Password=kc" providerName="System.Data.SqlClient" />
The owner of the database is 'KC\Administrator'. I use SQL Server Authentication.
The connection string when I am connected to the database locally is as follows:
<add name="Database" connectionString="Server=localhost;Database=ICKC;Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />
I restored the remote database to my local computer. The owner of the ICKC database is KC\GSantin.
I connect to the database using Windows Authentication.
From what I found on the web, this error can often be from no owner on the database but that is not the case for me. There is a login for 'kc' in the local database. It is a backup and restore of the remoste one so everything is the same.
Does anybody have any suggestions how I can fix this problem?
Thanks,
Gloria
kc should be a SQL Login based on your remote server. Once you restore the database from remote server to local, the SQL user came along. You must have created kc SQL login manually.
What is going to miss here would be mapping between them.
can you run sp_change_users_logins 'Report' and check if there is any orphan users. mostly you should see kc.
If yes then
run sp_change_users_logins 'update_one','kc','kc'
Else You need to capture profiler trace to find exact point of failure.

Deploying code first code migrations based web app to server, but it's not creating the database

I have my own box so I have complete control over the server. I'm using SQL server 2008. I run the application hoping it will create the database, but I keep getting the error:
Cannot open database "blah blah" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\appname'.
I've tried to go into SSMS and add the user IISAPPPOOL\appname but that hasn't helped any. Here's the connection string from my web.config
<add name="Context"
connectionString="Data Source=Server\DbInstance;Initial Catalog=blahblah;Integrated Security=True;User ID=myUser;Password=myPassword"
providerName="System.Data.SqlClient" />
and the initializer from my global.asax
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
Is the app failing because code first is failing and not creating the database or is it because of permissions issues? If I need to provide more specific information I can.
Well, one problem you have: you're defining both Integrated Security=True (i.e. Windows authentication) as well as User ID=...;Password=.... (i.e. SQL Server authentication) in your connection string.
Choose one or the other - but not both at the same time!
If you do have that user myUser specified as a login in your SQL Server, right now, it will not be used, because the Integrated Security=True setting will prevail ...
So change your connection string to:
<add name="Context"
connectionString="Data Source=Server\DbInstance;Initial Catalog=blahblah;User ID=myUser;Password=myPassword"
providerName="System.Data.SqlClient" />
and see if now, you're myUser user will be used.

Problem accessing database for Entity Framework

I've created a windows forms application that would use a client server model for accessing database. I created a connection for Entity Framework Model and it works fine with the following connection string:
connectionString="metadata=res://*/CartonOrderModel.csdl|
res://*/CartonOrderModel.ssdl|
res://*/CartonOrderModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=.\SQLEXPRESS;
AttachDbFilename=E:\Databases\RollMeasurements.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True;
MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"
I wonder if this connection string would still work when i deploy it on client environment which has a central db server and more than one instance of my app would be accessing it from client machines.
I tried changing the connection string to this:
connectionString="metadata=res://*/CartonOrderModel.csdl|
res://*/CartonOrderModel.ssdl|
res://*/CartonOrderModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=.\SQLEXPRESS;
Initial Catalog=RollMeasurements;
User ID=sa;
Password=sapassword;
Connect Timeout=30;
User Instance=True;
MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"
But now when I run my app it gives me this exception:
Failed to generate a user instance of SQL Server. Only an integrated
connection can generate a user instance. The connection will be
closed.
Please help me with this. Thanks!
I think you have to remove User Instance=True (or set it to False explicitely). User Instances are local copies of a SQL Express database and not suited for work in a multiuser environment with a central DB.
I think if you want to access to a central server, you need to change the Data Source=.\SQLEXPRESS; line to a central sql server instance.
english is not my mother language so maybe dont understand correctly the question.
Cheers

Resources