3

I'm new to C# and .net and just worked with php and mysql.
to connect to sqlserver express (in visual studio 2010) with c# we should provide a connection string that has lots of formats i found on the web specially in connectionstrings.
for example in standard format :
"Data Source=myServerAddress;Initial Catalog=myDataBase;UserId=myUsername;Password=myPassword;"

what is username and password? where can i find them? are they "root" and "" like in php mysql or something else?

as mentioned , i have created a database named "db.sdf" in sqlserver express in visual studio 2012.

I'm really confused. please help.

1
  • sdf is a SQL Server Compact Edition database. Commented Sep 2, 2012 at 15:49

1 Answer 1

4

If your database extension is SDF,you are creating SQL Server CE (Compact Edition) database file. Use this connection string

Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyData.sdf;Persist Security Info=False;

OR

Data Source=MyData.sdf;Persist Security Info=False;

More on this link.

UPDATE 1

You need the System.Data.SqlServerCe namespace.

SqlCeConnection conn = new SqlCeConnection("Data Source=\\Mobile\\Northwind.sdf;");
conn.Open();
.
.
.
conn.Close();

From MSDN:
Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

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

8 Comments

not working . when i add con.Open() , the rest of code dose not execute , but also no errors. i don't know why
can you post your code?maybe you are using wrong dataprovider.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ConsoleApplication1 { class First { static void Main() { //string connectionString = ConsoleApplication1.Properties.Settings.Default.ConnectionString; SqlConnection con = new SqlConnection("Data Source=db.sdf;Persist Security Info=False;"); // 2. Open the connection con.Open(); Console.WriteLine("ok"); Console.ReadLine(); } } }
You need to have included the System.Data.SqlServerCe namespace (not the System.Data.SqlClient). It's in the System.Data.SqlServerCe.dll.
this link has provided a great tutorial for creating application that uses sqlce database file. it will even teach you how to add the assembly file you need :)
|

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.