How to make ConnectionString work on client computer? - c#

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

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 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.

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.

Instance Failure Exception

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

How to change connection string in web.config for entity framework

I need to deploy WCF. Database is SQL Server 2008 R2. Access to database is only possible from app server because of Windows Authenticaton (sql server authentication not work).
I need to change my connection string to new data source i tried but not successfull.
i have connection address and everything what i need.
My connection string :
<connectionStrings><add name="MyEntities" connectionString="metadata=res://*/MyDatabase.csdl|res://*/MyDatabase.ssdl|res://*/MyDatabase.msl;provider=System.Data.SqlClient;provider connection string="data source=myMachine-LAPTOP;initial catalog=MyDatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings></configuration>
to delete connection string and generate new is not possible, because i have not access from dev computer and app server dont have visual studio to try it from there.
i need help how to change it.
Please try to change connection string by opening configuration file with notepad..

Categories

Resources