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

7
  • try using a relative path instead, like ".\\test.mdf" if the file is in the program root. Commented Jul 8, 2015 at 8:05
  • 1
    Apart from fixing your error I suggest you use SqlConnectionStringBuilder to create your connection strings to avoid getting invalid values. Commented Jul 8, 2015 at 8: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] Commented Jul 8, 2015 at 9:12
  • also this : SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=.\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); Commented Jul 8, 2015 at 9:14
  • @ThorstenDittmar whould you plz giv me the right code ? Commented Jul 8, 2015 at 9:14

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

9 Comments

I could not solve my problem . would you plz give me a relative right code ?
@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?
problem is also solved here stackoverflow.com/questions/3500829/…
@Thorarins: No, that's a different problem. The OP is running into a compile-time error which doesn't occur in that question.
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.