Database connectivity error in Visual Studio? - c#

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.

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;";

How to run .mdf database from LAN?

I have a C# (.NET Framework 4.5 - MVS 2015) project which has a Service-based Database with a local generated .mdf file. My Microsoft SQL Server that I use has this version: 13.0.1601.5.
Everything works fine on the server PC. I share in LAN the application with the .mdf files, but the clients from the same LAN can't open the application. It seems that the connection with the DB is bad.
My connection string is formed like this:
public static string attachedDbFile = "AttachDbFilename=" + currentWorkinglocation + "NCI_DB.MDF;";
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;" + attachedDbFile + "Integrated Security=True");
What am I missing, please?
Later Edit:
After following the steps provided by you guys:
My Data Source is Microsoft SQL Server now (and not MSQL Server Db file). I have attached the .mdf file to a database from my server (in MSQL Server Management Studio), the "Allow remote connection option" is checked and I have changed the connection string to:
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=myDbName;Integrated Security=True");
Now, the application works on my local host (the PC that has the sql server installed) without the need of the .mdf file. It's ok.
But if the clients try to access the application from LAN, from my shared location, the app .exe crashes once again with this error message:
Did I missed something?
SOLUTION:
After following some pointers from our colleagues and after investigating further the problem the solution is:
- install sql server 2017 (full version ! - not express edition)
- install microsoft sql server management studio
- attach mdf to a database
- configure an account with rights for that database
- update the connection string according to this:
public SqlConnection mySqlConnection = new SqlConnection(#"Data Source=ip,port(1433-default);Initial Catalog=db_name;User ID=user;Password=pwd");
SQL server only supports database files on local disks (or more exotic links than LANs, such as iSCSI, etc). If you want to share access to a database across a local network, it's time to stand up a SQL Server instance that will own the file and then connect to that SQL Server client/server. Stop trying to access it as a file across the network.

C# app with SqlClient does not work on another computer

I have written an application that uses SqlClient and a local mdf database. However, it does not work on another machine (throwing an exception that the server cannot be run) and I assume this is because there is no SQL server installed? But I thought that using local database and SqlClient elimites the need for the server, something like the old SQL CE. Is that the problem?
EDIT Exception: System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing 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
As for connection string, I keep the one generated by Visual Studio Express, just modify the path to use the app folder.
SqlConnection s = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ="+ System.IO.Path.GetDirectoryName(Application.ExecutablePath)+ #"\Database1.mdf; Integrated Security = True");
Is there a simple way how to rework this so that my app does not require SQL server installed? All I need is to have it use the local database file in its folder.

SQL Server connection string causing error?

In a Winforms application, I have a connection string that causes an error
data source=(local);initial catalog=RegisterDB;persist security info=False
Error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information:
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)
Thanks for the help Arif Ansari but it still gives the same error.
I'm using Microsoft Visual Studio 2013 Express with SQL Server. I created the database from within Microsoft Visual Studio 2013 Express
Can anyone help?
Hi marc_s
Sorry it didnt work. I created the Database RegisterDB manually with Microsoft Visual Studio 2013 Express in server "(localdb)\v11.0"
Can anyone help ???????
Please Try it
SqlConnection con = new SqlConnection("Data Source=(local);Database=RegisterDB;Integrated Security= true");
try this query as your connection string format
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;
or this
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
basically check the instance of the server that you are running.
Check if the instance service is running or not.
Type services.msc in the start menu to open the Services List.
Check for the name SQL SERVER (your instance name).
If the instance status is Started, issue is in your connection string or user permissions.
Else if the status is empty or stopped, start the instance and then try connecting.
you may also refer this site for connection string format.
The SQL Server that comes with Visual Studio 2013 is the 2012 Express version - and that by default install the "LocalDB" support.
So I would try this connection string instead:
"data source=(localdb)\v11.0;initial catalog=RegisterDB;Integrated Security=SSPI;"
Does that work? If not: are you auto-creating the database, or if not, did you manually create that database RegisterDB that you're referencing in the connection string?

Categories

Resources