Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how can i make connection string with .sdf database


this is my connection string but it doesn't work

" Data Source = 'Database1.sdf';"
Posted
Comments
Vedat Ozan Oner 23-Jun-14 15:05pm    
http://www.connectionstrings.com/
[no name] 23-Jun-14 15:05pm    
www.connectionstrings.com

Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
 
Share this answer
 
Comments
Heba Kamel 24-Jun-14 2:08am    
when i make add connection no databases appears !!
Heba Kamel 24-Jun-14 2:14am    
i made new database (Database1.sdf) then i made tables on it
as guys said go to this link
http://www.connectionstrings.com/sql-server-compact/[^]

it seems that you need to use following lines to get the right db connection string

string StartupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);


C#
string datalogicFilePath = Path.Combine(StartupPath, "Datalogic.sdf");
string connectionString = string.Format("DataSource={0}", datalogicFilePath);


for more info http://stackoverflow.com/questions/6744134/connection-string-with-an-sql-server-ce-database[^]
 
Share this answer
 
Comments
Heba Kamel 24-Jun-14 15:49pm    
i get this
Keyword not supported: 'datasource'.

and this is my code

string StartupPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

SqlConnection con = new SqlConnection(string.Format("DataSource={0}", System.IO.Path.Combine(Application.StartupPath, "Database2.sdf")));
Jafarinejadvazifehkhorani 24-Jun-14 18:29pm    
if it helped please make it as an answer
Heba Kamel 25-Jun-14 16:22pm    
this line is run successfully

SqlCeConnection con = new SqlCeConnection("Data Source="
+ System.IO.Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "MyDB.sdf"));

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