-1

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 ?

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [Why am I getting “Cannot Connect to Server - A network-related or instance-specific error”?](https://stackoverflow.com/q/18060667/2029983) – Thom A Sep 28 '20 at 13:09
  • `Integrated Security=True` means that the connection is made as the current user - but is that user known in that specific copy of your database? – Hans Kesting Sep 29 '20 at 07:19
  • @hassan, Is there any update? If your question has been solved , you can click '✔' to mark the appropriate reply as the answer. – Jack J Jun Oct 07 '20 at 07:18

2 Answers2

0

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)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • it gives me another message " failed to generate user instance of sql server due to failure in starting the process for user instance, the connection will be closed" how to solve it ? – hassan khaled Sep 28 '20 at 14:45
  • If I were you - I'd stop using the `AttachDbFileName` and `User Instance` stuff - that's nothing but grief. Instead: fire up SSMS, and **attach** your `.mdf` to the SQL Server Express instance - and then reference it using the logical database name - something like `Data Source=.\SQLEXPRESS;Integrated Security=True;database=gym` and be done with it – marc_s Sep 28 '20 at 15:53
  • I did it , I changed the stringconnection and I Make copy of gym.mdf and paste it in another computer to data of sqlexpress and showed to me "Can not open database "gym" requested by the login, login failed, login failed for user 'lab-khaled\khaled' . I am sorry if I ask too much but i really need to make it work , thank u BTW – hassan khaled Sep 28 '20 at 16:43
0

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;";
Jack J Jun
  • 5,633
  • 1
  • 9
  • 27