2

I'm using Visual Studio 2015 and I just created a Windows Forms application with a SQL Server database. I finished the program and tried to run the application on the other computer but it didn't run. I also tried to install .Net Framework v4.0 and SQL Server Express on that computer and also put the database in path C:\ so that the SqlConnection path on my computer to another computer will be the same.

The code is like this:

SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;

string connection = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Database1.mdf;Integrated Security=True";

But I got an error that is the database I placed in path C:\ is not writable whatsoever. I want to know what I can do in order to run the application.

halfer
  • 19,824
  • 17
  • 99
  • 186
Theo Martin
  • 27
  • 1
  • 9
  • Maybe check [this answer](https://stackoverflow.com/questions/6054997/database-attached-is-read-only) – John Wu Jun 16 '17 at 03:09
  • 1
    Never call a database by the .mdf file name. Always have credential issues.Integrated security uses windows credentials to connect to database.So get rid of the AttachDBFilename from connection string. For windows credentials to work you need the two computers in the same windows Group.Then user must have same account on both PCs.Usually I create a group account and have database use the group account for windows credentials.Then add user to the group account. Normally at work we already have group account on windows for people working in same dept or same project.So the group account exists – jdweng Jun 16 '17 at 03:17
  • I created the database on Visual Studio 2015 using T-SQL. Where do I put that code(the answer link that you commented) on my program? I am new to programming by the way. I can do the basics on SQL like Update, Insert and Delete a data from the database. – Theo Martin Jun 16 '17 at 03:18
  • @jdweng Can you please explain to me and demostrate how to do that? I'm desperately wanting to know how this application will work. – Theo Martin Jun 16 '17 at 03:27
  • Just use the proper connection string based on the SQL Server configurations on the computer you are trying to connect. Just make sure that you can connect to that one. – jegtugado Jun 16 '17 at 03:27
  • This problem is AttachDbFilename=C:\Database1.mdf. You must create new folder in C driver, after you set security on that folder is can write and read. Copy Database1.mdf file into that folder, you edit AttachDbFilename=C:\\[new folder]\\Database1.mdf again. I hope it will work for you. – Tien Nguyen Ngoc Jun 16 '17 at 03:40
  • @TienNguyen I'll do that. Thanks for the help. I appreciate it. – Theo Martin Jun 16 '17 at 03:52
  • @jdweng How can I create a Group account like what you said? – Theo Martin Jun 16 '17 at 03:52
  • The database owns the mdf file and user credentials will not work. Do not use MDF file name. What operating System? You would need to search web. It may be under adding a window credential or adding window group. – jdweng Jun 16 '17 at 04:13
  • @jdweng that is a very specific case. There are valid reasons for using an MDF filename (certain editions of SQL work that way). Also the concept of duplicating logins and workgroups is for the specific case where you are using windows authentication without active directory. It's likely that none of this applies. – Nick.Mc Jun 16 '17 at 04:26
  • Nick : The connection string says : Integrated Security=True. You are making assumptions that may be totally wrong. I think you are assuming very old editions of SQL. All cases where I've use SQL I get connection issues using mdf file because the database owns the file and will not allow a user to access the file. – jdweng Jun 16 '17 at 04:45

3 Answers3

0

You must install SQL Express on the computer/server where you want to have the database and then attach the MDF file.

Important: - Remember to open port 1433 in the Windows Firewall. - If you are going to use SQL Server authentication, you must create and / or activate the user with the desired password. - To connect you can use the IP of the computer/server, preceded by the instance of sql express. Example: YourIP\SQLExpress

Good luck!

ymoran
  • 57
  • 2
0

you need to create sharing server for database and after this, you can access your DB to anywhere and any computer Thanks

michael john
  • 95
  • 3
  • 12
0

So much misunderstanding and misinformation around LocalDB.

While it was being developed, your application was using an edition of SQL Server called LocalDB. This is a low admin lightweight edition that lets you reference the MDF file directly (contrary to some comments here)

When you deploy your app you decide whether you want to stick with LocalDB or upgrade to a more scaleable edition of SQL Server.

In your case, I suggest you stick with LocalDB

There's some background here and a link to a video if you have time

LocalDB deployment on client PC

https://blogs.msdn.microsoft.com/sqlexpress/2011/07/12/introducing-localdb-an-improved-sql-express/

Now if you really want to an answer you need to post the exact error message that you see, not a vague recollection of it.

The problem might be that LocaDB is not installed, or it might be that there is some SQL Server access issue. We can't tell because you did not post the error message

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91