Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am Trying to insert data into .mdf db file when i am specifying the full path in connection string then it's working but when i am specifying path using |DataDirectory| then runtime it's showing data is inserted but data is not inserting into .mdf database file

What I have tried:

it's Working
connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Kishan\Documents\Visual Studio 2008\Projects\Shriya Graphics\Shriya Graphics\Shriya_Graphics.mdf;Integrated Security=True;User Instance=True");

it is not Working
connection = new SqlConnection(Properties.Settings.Default.ConnString);
Posted
Updated 9-Feb-17 8:19am
Comments
[no name] 9-Feb-17 14:12pm    
Start by finding out what "Properties.Settings.Default.ConnString" is.

1 solution

If the top line is working - and you shouldn't be using that, hard-coded connections strings are a bad idea - and the second isn't, then you need to use the debugger and see exactly what Properties.Settings.Default.ConnString is returning. Almost certainly, it isn't a valid SQL connection string, or it addresses a different DB.

Do remember that Attaching databases is only available for development, and cannot be used except on Express editions of SQL Server. If you try to use it in production, it will always fail.
 
Share this answer
 
Comments
Kishan N. Trivedi 9-Feb-17 14:28pm    
Then What should i do..??
Kishan N. Trivedi 9-Feb-17 14:37pm    
Actually i putted my .mdf database file inside my Project Folder and from the Project->Properties->Settings-> i am creating Connection String and for the connection string i selected that database file as Value..is it correct or not..??
OriginalGriff 9-Feb-17 14:41pm    
That's ok for dev - though I'd put it in a dedicated "Databases" folder - but it won't work in production where the DB must be permanently controlled by SQL Server.

Use the debugger, and find out what the connection string is. Until you have that, you have no info to "fix" the problem with, and running your app is the only way to get that.
Kishan N. Trivedi 9-Feb-17 14:51pm    
Can you Tell me How to Publish My C# App with DB in Client PC
OriginalGriff 10-Feb-17 4:00am    
That's going to depend on the client, and how / what your app does.
If your app uses SQL Server, then you need an instance installed where the client PC can get at it - and sufficient permissions to add a DB to it. That's non-trivial, because it's likely to involve things the average joe user isn't going to know, like connection strings, user ID's - you know the problems.
But if you aren't trying to use a multiuser DB (and I have no idea what your app is) then it may be easier to change your code to use a single user system like SqLite or even Access - neither of which require a server installation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900