1

I am new to C#. I want to use my Microsoft SQL Server database file test.mdf in my output software in C#. In the past, I had just copied the connection string in Visual Studio like this :

Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Home\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True

as you see the database file path is : C:\Users\Home\Documents\test.mdf;

When I create setup for my sofware in Visual Studio 2008, and install the software on another PC, it errors :

An attempt to atach an auto-named database for file C:\User\Home\Document\test.mdf failed ...

So I want to address the file with the installation folder path whith this :

string dir = Application.StartupPath + "\\" + "test.mdf";

but when I want to run program in Visual Studio 2008 it erros

string dir = Application.StartupPath + "\\" + "test.mdf";
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + dir + ";Integrated Security=True;Connect Timeout=30;User Instance=True");

Error 1 A field initializer cannot reference the non-static field, method, or property 'phonebook.Form1.dir' C:\Users\Home\Documents\Visual Studio 2008\Projects\phonebook\phonebook\Form1.cs 25 95 phonebook

UPDATE

When I use

 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename="+ Application.StartupPath +" \\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

it errors :

One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup. Cannot open user default database. Login failed. Login failed for user 'Home-PC\Home'.

While I have copied right test.mdf file there

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SoheilYou
  • 907
  • 5
  • 23
  • 43
  • try using a relative path instead, like ".\\test.mdf" if the file is in the program root. – Thorarins Jul 08 '15 at 08:05
  • 1
    Apart from fixing your error I suggest you use `SqlConnectionStringBuilder` to create your connection strings to avoid getting invalid values. – Thorsten Dittmar Jul 08 '15 at 08:06
  • @Thorarins this does not work : [code]SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=./test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");[/code] – SoheilYou Jul 08 '15 at 09:12
  • also this : SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=.\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); – SoheilYou Jul 08 '15 at 09:14
  • @ThorstenDittmar whould you plz giv me the right code ? – SoheilYou Jul 08 '15 at 09:14
  • @soheilyo I could, but then you'd never read the documentation on how to use `SqlConnectionStringBuilder`. So I suggest you try it first, if you don't succeed, you modify your question and *then* we can help you. – Thorsten Dittmar Jul 08 '15 at 10:27
  • I coudent use it :( its emergency – SoheilYou Jul 08 '15 at 11:28

1 Answers1

3

As the error message says, you can't use the value of one instance field when initializing another. You probably don't want dir as a field anyway. Just move all of this into the body of the constructor... or ideally, only create your SqlConnection when you need it anyway. Don't use a single instance throughout your application, but go through a "create, use, dispose" cycle every time you need database access. (Ideally, don't do this in your GUI code, either...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I could not solve my problem . would you plz give me a relative right code ? – SoheilYou Jul 08 '15 at 09:15
  • 1
    @soheilyo: No - you've given no indication *how* you tried to apply my answer, or what problems you ran into. Stack Overflow isn't a "gimme teh codez" site. I've suggested a fairly significant change to your code, within the answer (creating a `SqlConnection` only when you need to, and disposing it immediately after use) - have you tried implementing that? – Jon Skeet Jul 08 '15 at 09:20
  • problem is also solved here http://stackoverflow.com/questions/3500829/sql-express-connection-string-mdf-file-location-relative-to-application-locatio – Thorarins Jul 08 '15 at 09:26
  • @Thorarins: No, that's a different problem. The OP is running into a compile-time error which doesn't occur in that question. – Jon Skeet Jul 08 '15 at 09:30
  • @Jon yes, but his main problem is the connection string which they solved in the answers I posted so probably it will help to solve his problem – Thorarins Jul 08 '15 at 10:14
  • @Thorarins: Well you say "his main problem" - but while the code can't compile, there's no way of even *getting* to that problem. – Jon Skeet Jul 08 '15 at 10:29
  • he also stated that he has installed it on another pc so the code must have been able to compile some time, anyhow isn't this all about helping each other not discussing whatever or not something is a problem or not – Thorarins Jul 08 '15 at 10:46
  • I must Give this project ... plz help if you know – SoheilYou Jul 08 '15 at 12:11
  • @soheilyo: I've already tried to help you, but we have no idea which of the suggestions you've tried, or what went wrong. If you update the question with your new code (below the existing question - don't change the question in a way that makes my existing answer useless) and what goes wrong with it, we may be able to help more. – Jon Skeet Jul 08 '15 at 13:05