Cannot connect to local database, C# - c#

I've got a problem to connect my website with database. The problem is, if I am trying install my website in IIS on the same machine where database is located, I am receiving error: "Login failed for user". But when I am installing my website on a different machine, connection with database works fine. My connection string looks like this:
<add name="DBConnection"
connectionString="Data Source=myServer; Intial Catalog=myDataBase; User Id=myUserId; Password=myPassword"
providerName="System.Data.SqlClient`" />
I have to install webservice on the same location where database is with the same password and login. How correct connection string should look like?

Related

I can't connect to a local SQL Server instance via Entity Framework using Windows authentication

I can't connect to my local SQL Server Express instance via Entity Framework. When I try to run the update-database command, I get this error message.
Login failed for user ''. Reason: An attempt to login using SQL authentication failed.
Server is configured for Integrated authentication only.
Error: 18456, Severity: 14, State: 58.
From what I understand, Visual Studio is attempting to log in to SQL Server via a user account even though I've requested that Windows authentication is used in the connection string.
I can still access the server via SSMS.
What I've tried. None of which helped
Different variations of the connection string
Opened port 1433
Created a test UDL file to test the connection.
A clean reinstall of SQL Server Express (not sure I managed to clean up all the files)
Checked the SQL Server browser is running
Checked server instance is running
Enabled TCP/IP & named pipes
Tried connecting via tcp which works
Added Integrated Security=SSPI to connection string
Changed server to accept Windows authentication and SQL Server authentication.
Restarting PC
This is a new laptop on windows 11, I don't know if that is causing any issues as I've never had an issue with this process on Windows 10.
I seem to have two instances; .\SQLEXPRESS & (localdb)\\MSSQLLocalDB I'm not sure if this is causing some conflict or if this is the intended behaviour.
Here is the connection string, I pulled this from the server explorer within Visual Studio so I am pretty sure this is correct. On top of that, I have tried numerous variations of this to attempt to fix the issue
"DefaultConnection": "Data Source=LAPTOP-51LB4QTQ\\SQLEXPRESS;Initial Catalog=MicroBlog;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
This is where I get the connection string in my Program.cs file
builder.Services.AddDbContext<BlogContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
At this point I'm completely lost, I've read a lot of articles but have not come across any fixes. I'm not a dba just a programmer so have limited knowledge of this side of SQL Server.
You may try like below:
In Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=SUBRATATALUKDER;Initial Catalog=MyDB;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
In appsettings.json file:
"ConnectionStrings": {
"DefaultConnection": "Server=SUBRATATALUKDER;Database=MyDB;Trusted_Connection=True;TrustServerCertificate=True;",
}
Note:
Server Name = SUBRATATALUKDER
Database Name = MyDB
100% tested.

Is There a Connection String For Connecting to Client Machine

I installed a windows form application on a client computer but when I try to log into my application it gives me this error:
a connection was successfully established with the server, but then an error occurred during the login process. (provider: shared memory provider, error: 0- No process is on the other end of the pipe.)
This is a c# windows form application running with SQL Server database, with login details. I have added the in and out bound rule on my firewall, I have tried changing my connection string severally, I have enabled tcp/ip port with its default port number, I have tried using the same network for both computers, I have tried using window and sql authentication, I have disabled firewall on client computer
Here is my connection string
<add name="connstrng" connectionString="Data Source=(local);Initial Catalog=AnyStore;Integrated Security=True"/>
I expect to be logged in to my application on the client computer using the same database with my server computer.
Connection strings are usually defined within the Web.config file. As #Panagiotis Kanavos said, the string is connecting to a local data source, which should work as expected within your dev environment. The web.config file should be located within the root directory of your project. Edit this file on the client's machine to point to the expected server / database. You may want to keep a blank or default version of this file in source control to avoid overwriting other's connection string info.
Example String:
<add name="SomeDB" connectionString="Data Source=snapdragon\SQL2k14;database=DBNAME;User Id=username; password=password" providerName="System.Data.SqlClient" />
Edit: "snapdragon" would be the host computer name. "SQL2k14" would be your sql server instance.
ok, i found the solution i changed my connection string and its working fine now, thanks guys.
here is my connection string-
<connectionStrings>
<add name="connstrng" connectionString="Data Source=DESKTOP-5I8TFY3\EMEKA;Initial Catalog=AnyStore;User ID=adminpos;Password=********" />
</connectionStrings>
</configuration>

MVC Website Cant Connect to Database on Azure

This is my first MVC Website with a database hosted on Azure.
I have deployed the database and the website. However I get an Access Denied error message:
[SqlException (0x80131904): 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: Named Pipes Provider, error: 40 - Could not
open a connection to SQL Server)]
What I don't get is that I can run the app via Visual Studio connected via the Azure connection string locally without any problems. I can speak to the db remotely but not within Azure!!!
Here's the connection string I'm using:
<connectionStrings>
<add name="myConnString"
connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source = tcp:mybdurl.database.windows.net,1433;Initial Catalog=myDb;
persist security info=True;
User ID=MyId;
Password=MyPassword;
MultipleActiveResultSets=True;
application name=EntityFramework""
providerName="System.Data.EntityClient"
/>
</connectionStrings>
I'm guessing there is a missing magic Azure element that allows access. Both are in the same data centre and within the same account.
Cheers
I was publishing the web.config file individually in Visual Studio, not realising it wasn't actually updating on Azure. I went back to old fashioned FTP and indeed the live web.config was still the original not the one for Azure.
Manually uploaded it and it worked!
What didn't it publish???

Hosting ASP.NET Web App on IIS DB connection problems

I have used IIS before, but I've never used a database and I've never done a web app. I did the same steps for putting a site onto IIS and everything works EXCEPT for when I go to a page that has a DB connection on it. I've tried a lot and this is all I get.
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: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
Windows Application Event Log:
Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.
This was built in Visual Studio and I have a SQL Express DB.
I posted my connection strings, any help is greatly appreciated.
I have added this to my applicationHost.config:
<add name="DefaultAppPool">
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
And now I get:
An attempt to attach an auto-named database for file |MY DB1 FILE| failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB1.mdf;Initial Catalog=aspnet-ICCAA_VIZIER-20150223043129;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB2.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>
I was able to figure it out. I changed all my data connections to .\SqlExpress. Which led me to another error of:
Login failed for user ‘NT AUTHORITYNETWORK SERVICE‘
To fix this I followed this guys article to figure it out.
Login Failed For user NetworkService

Connect to MySQL DB using ASP.NET Entity Framework

I have my connection string set as
<add name="myconn" connectionString="server=192.X.Y.Z;user id=myusername;persistsecurityinfo=False;database=mydatabasehere;port=3306;Password=mypassword;" providerName="MySql.Data.MySqlClient" />
and later this error appear:
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred
in MySql.Data.dll but was not handled in user code
Additional information: Authentication to host '192.X.Y.Z' for
user 'myusername' using method 'mysql_native_password' failed with
message: Access denied for user
'myusername'#'crlspr-myipaddresshere.myacc.net' (using password: NO)
What will be the problem of this exception?
my connection seems to be ok since I just tested from MySQL Workbench as well in Visual Studio * Server Explorer
Why the error message get my ipaddress, 'myusername'#'crlspr-myipaddresshere.myacc.net'? maybe is a firewall problem.
Since my problem was the DB connection string and I was not sure which one #Raptoy suggestion will work or not, Then I went to MySQL website and get one of their example
4.14.2 Usage: Creating A New MVC Web Application
and the connection string that was generated by the wizard finally works:
<add name="myconn" connectionString="server=192.X.Y.Z;user id=myusername;password=mypassword;persistsecurityinfo=True;database=mydatabasehere" providerName="MySql.Data.MySqlClient" />
You will notice the connection string contains:
server
user id
password
persistsecurityinfo=True
database

Categories

Resources