-2

When I added the Data at the Database I can not find it on, but its shows to me, it is added, but when I re-run the application I can not find this is my sqlconnection :

SqlConnection conn = new SqlConnection(@"Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|direct.mdf;Trusted_Connection=Yes;Integrated Security=True;User Instance=True");

and i use app config because i wanna deploye my application on machine client

 <add name="DIRECTPERFERMANCE.Properties.Settings.directConnectionString"
connectionString="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\direct.mdf;Trusted_Connection=Yes;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>
Kenan Zahirovic
  • 1,587
  • 14
  • 24
SIMO
  • 1
  • 4
  • try this http://stackoverflow.com/a/29558863/4513879 – Pradnya Bolli Apr 19 '15 at 13:47
  • 1
    Show us you code which actually writes to database. – Kenan Zahirovic Apr 19 '15 at 13:48
  • Did you include your MDF file between your project files? If yes what is the value of the property `Copy to Output Directory`? – Steve Apr 19 '15 at 13:58
  • WHEN EDIT MYSQL CONNECTION LIKE that : Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\direct.mdf;Database=direct;Trusted_Connection=Yes;Integrated Security=True;User Instance=True ITS WORK BUT WHEN I DEPLOY THE APPLIC ITS SHOWS ME THERE ARE OTHER DATABASE WITH THE SAME NAME – SIMO Apr 19 '15 at 14:14

1 Answers1

0

The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. Direct)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=Direct;Integrated Security=True
    

    and everything else is exactly the same as before...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • i try to add data but it shows me error echec d'instance. Data Source=.\\SQLEXPRESS;Database=Direct;Integrated Security=True – SIMO Apr 19 '15 at 17:16