Instance Failure Exception - c#

It was working still yesterday but now it is not working because sqlexpress service is stopped and it is not starting. I have connection string:
Data Source=.\SQLEXPRESS;
AttachDbFilename=C:\Program Files\Setup1\DB.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True
When I open this connection it throws exception Instance Failure. This is my WPF application and I am using .NET Framework 4.0.

#"Data Source=.\SQLEXPRESS;
AttachDbFilename=C:\Program Files\Setup1\DB.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True";
Make sure your SQL Server instance(MSSQLSERVER) is running.
To do this,
(i)Open run dialog box and type
(ii)"services.msc" (without quotes) and hit Enter.
(ii)In Service Management Console you can check whether it is running or not.
Or
Try Deleting the folder C:\Users\User\AppData\Local\Microsoft\Microsoft SQL Server Data also restart VS after deleting the folder.

I just stopped mssqlserver service and started sqlexpress service then sqlexpress service get started..now problem is that mssqlserver and sqlexpress both services cannot start simultaneously

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.

How to make ConnectionString work on client computer?

I created a setup for a management system in Visual Studio and I used a Microsoft SQL Server database file, but when I used it on the user's computer or on another computer, I get this message when I tried to connect to the database. What is the problem?
I used SQL Server 2017 Express.
My connection string to work on client's PC :
#"Data Source=.\SQLEXPRESS01;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|gym.mdf";
I installed SQL Server 2017 Express on the client machine, but it didn't work.
What is the problem ?
By default, when you don't change any settings during installation, a SQL Server Express instance will get the SQLEXPRESS instance name - so try this:
Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|gym.mdf
(not SQLEXPRESS01 - that's not standard)
The error you get means that the server is not correct.
First, you can try the following code to get installed ServerName and instanceName.
var instances = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow instance in instances.AsEnumerable())
{
Console.WriteLine(instance["ServerName"]);
Console.WriteLine(instance["InstanceName"]);
}
Second, you can use the following connectionstring to connect db file.
string connstr = #"Data Source=server\\instance;
AttachDbFilename=D:\Product.mdf;
Integrated Security=True;
Connect Timeout=2;";

SQL suddenly fails to generate a user instance

I have an application deployed to Windows Server 2016 VM on IIS, using SQL Express.
It has been working perfectly all day, but suddenly I am seeing this error when trying to log in:
Failed to generate a user instance of SQL Server due to a failure in copying database files. The connection will be closed.
This is after making no changes to the code or web.config whatsoever.
I have tried these solutions already:
Restart SQL service and Delete all contents in C:\Users\SKotze\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS
Add User Instance=true to the connection string
Ran this bit of SQL before restarting the service:
exec sp_configure 'user instances enabled', 1.
GO
Reconfigure
This is driving me crazy I just dont't understand why it suddenty stopped working.
Please let me know if I can provide any further details.
My connection string:
<add name="WebEntities1" connectionString="Data Source=ServerName\SQLEXPRESS;Initial Catalog=DBName;Integrated Security=True" providerName="System.Data.SqlClient" />

Entity Framework Connection to localDb

I am trying to follow the pluralsight course ASP.NET MVC 4 Fundamentals. But can't have my database connected.
Here is the error I got:
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
I have visual studio 2013 professional and SQL server 2012 installed on my machine. When I installed my SQL server, I created a server name "ABC" on my computer.
I have also installed sql localdb 11.0 separately, but it seems VS can't find the localDb connection. When I check Server Explorer -> add Connection, under server name list, only "ABC" is shown up.
Here is the connection string.
I also tried to use "Data Source = ABC; ...." it doesn't work either.
Update
Here is my connection string
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-eManager.Web-20141223223418;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-eManager.Web-2014122322341‌​8.mdf" providerName="System.Data.SqlClient" />
You could try this
In server explorer, right click, Choose Add Connection
enter (localdb)\v11.0 in as the server name
Choose your database and press connect
Right click properties on your new connection
Use that connection in string in your default connection
I.e.
<add name="DefaultConnection" connectionString="<Paste-connection-string-here>" providerName="System.Data.SqlClient" />
If that doesn't work, lets try starting it from the command line
Open command prompt
Run SqlLocalDB.exe start v11.0
Follow original steps , use the named pipe as your server name
If that doesn't work, lets try and connect via named pipes
Open command prompt
Run SqlLocalDB.exe info v11.0
Copy the Instance pipe name that starts with np:...
Follow original steps , use the named pipe as your server name
e.g
Run this command to make sure what is the version of your LocalDB
sqllocaldb info
So in my case the version is MSSQLLocalDB then the connection string will look like this
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=IdentityManagerDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
To add on to TheGeneral use view - SQL Server Object Viewer and look immediately under the "SQL Server" object. You'll see the name of the connect looking something like "(localDB)\ProjectsV13" Hand enter that into the connection box then you can browse the server for the database you want to use.

Database connectivity error in Visual Studio?

I have lost connectivity using Visual Studio 2012 database...
when i try to reconnect and run the application then here us the Exception
NOTE: The previous file was deleted mistakenly, and trying to attach the new one it says it already exist?
what is the Solution?
What kind of database are you running? Specify:
If you have SQLEXPRESS Database:
First you need to change your Data Source name like this.
"Data Source=(LocalDB)\v11.0" to "Data Source=.\SQLEXPRESS"
To connect to your local SQL Server instance, issue the following string:
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname Trusted_Connection=Yes;
To connect to your local SQL Server instance with a database file located in a data directory, issue the following string:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname Trusted_Connection=Yes;
To connect to LocalDB Automatic Instance, issue the following string:
Server=(localdb)\v11.0;Integrated Security=true;
To connect to LocalDB Automatic Instance with a specific data, issue the following string:
Server=(localdb)\v11.0;Integrated Security=true; AttachDbFileName=C:\MyFolder\MyData.mdf;
To connect to LocalDB named Instance, issue the following string:
Server=(localdb)\MyInstance;Integrated Security=true;
You need to get a string that matches your specific DB exactly, for it to connect.
In you connection string just add the 'database='. This prevents SQL server to create the auto-named database.

Categories

Resources