fatemeh sarikhani
fatemeh sarikhani

Reputation: 1

Connect to SQL Server CE database

I'm having trouble getting a connection to a .sdf (SQL Server Compact edition) database. When I debug my project in opening database appear this error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I have no luck. I get this code:

Qconnection.ConnectionString = "Data Source=C:\\Users\\Admin\\Desktop\\New folder\\WindowsFormsApplication2\\WindowsFormsApplication2\\Database1.sdf";
//connection.ConnectionString = " Data Source=C:\\Users\\Admin\\Desktop\\WindowsFormsApplication2\\WindowsFormsApplication2\\Database1.sdf";
Qcommand.Connection = Qconnection;
Qconnection.Open();

for (int i = 0; i < orderColection.counter1; i++)
{
   string commandText = "Insert into order values(@RID,@amount,@type,@date)";
   Qcommand.CommandText = commandText;
   Qcommand.CommandType = CommandType.Text;

   Qcommand.Parameters.AddWithValue("@RID", orderColection.list[i].rep_id);
   Qcommand.Parameters.AddWithValue("@amount", orderColection.list[i].amount);
   Qcommand.Parameters.AddWithValue("@type", orderColection.list[i].type);
   Qcommand.Parameters.AddWithValue("@date", orderColection.list[i].date );
   Qcommand.ExecuteNonQuery();
}
Qconnection.Close();

Upvotes: 0

Views: 314

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94653

You have to use System.Data.SqlServerCe provider API to connect with SQL Server Compact data source. (Add the reference of System.Data.SqlServerCe.Dll).

using(SqlCeConnection cn = new SqlCeConnection(@"Data Source=C:\path\sample.sdf"))
{
 //
}

Upvotes: 1

Related Questions