Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project in C#.net ,Visul studio-2008 with database in SQL Server-2008.It is windows application project.

I have create a folder named 'Data' in Debug folder. & I copy and paste data base .mdf and ldf file into 'Data' folder

Now i want to connect databse in 'Data' Folder .
I have a code for connection string
"
sqlcon = new SqlConnection(@"Data Source='.\SQLExpress'; Initial Catalog=; Integrated Security=true; AttachDBFileName='" +Application.StartupPath+@"\Data\Laboratory1.mdf; Uid=sa;Pwd=zxc'");
"
but its not working.I got an error at execution time;

So i want to know
what is the connection string when the database copy paste in Data Folder with in debug folder?

Please help me.
Posted
Updated 9-Oct-12 20:41pm
v2

Dear you should have Sql sever express to run these .
then create your database in App_code .

for your reference connection string will be -

XML
<connectionStrings>
      <add name="DatabaseConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=true" providerName="System.Data.SqlClient"/>
   </connectionStrings>


for windows you can use this

C#
Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;
 
Share this answer
 
v2
Comments
Member 8470112 10-Oct-12 2:42am    
It is a Windows application project
Sarvesh Kushwaha 10-Oct-12 2:49am    
try to use this --

Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;
Seriously? I wouldn't do that, as it relies on the eventual production program having permission to write files on the folder, which will be below the "Program Files" folder, which is normally restricted to Admin only. You will probably be getting the same permissions problem here.

Instead, keep your database in a more accessible place - see here: Where should I store my data?[^]
 
Share this answer
 

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