Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
i have been given a task to establish a connection with .sdf file,but I am familiar with .mdf file which is directly a database file.
i did this task using sqlconnection,sqlcommand,and sqldatareader but there is network related error when i open the connection.
please help in this regards.
Posted

Hello ,

Some changes are required to connect with .sdf file .here i am using "Northwind" Database . first of all you have to modify you connectionstring in your web config file . you must mention what type of Provider you are using.



<add name="NorthwindConnectionString" connectionString="Data Source=|DataDirectory|\Northwind.sdf" providerName="Microsoft.SqlServerCe.Client.3.5" />


in the next stage ,

before you connecting to your database ,add this namespace.

using System.Data.SqlServerCe

because you are using Sql Compact edition .

Here , you have to use
SqlCeConnection ,SqlCeDataAdapter
instead of
sqlconnection,sqlcommand,and sqldatareade
to connect with database.

DataSet ds = new System.Data.DataSet();
       void Bind()
       {
           SqlCeConnection con = new SqlCeConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
           SqlCeDataAdapter da = new SqlCeDataAdapter("select p.[Product ID],p.[Product Name],c.[Category ID],c.[Category Name] from Products p inner join Categories c on p.[Category ID]=c.[Category ID]", con);
           da.Fill(ds);
      }


here you will get a collection of Products from "Northwind" database .

thanks
 
Share this answer
 
to connect to sdf files you have to use System.Data.SqlClient namespace and connectionstring would be like
"Data Source = sdffilepath; Password = pass; File Mode=Exclusive; Persist Security Info=False;
 
Share this answer
 
Comments
[no name] 11-Dec-13 1:50am    
so which classes should i use ? sqlCommand or sqlcecommand
Member 8622273 11-Dec-13 3:01am    
use sqlcecommand

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