Search code examples
sql-servervb.netvisual-studio-express

SQL Server Compact connection


I created a SQL Server Compact database file aa.sdf and I am trying to connect it to my VB.NET code. But it can not connect to it

Dim MyConnexion As SqlConnection = New SqlConnection("Data Source=C:\Users\W8\Documents\aa.sdf;" & "Integrated Security=SSPI;Initial Catalog=toto")
Dim Requete As String = "Insert into toto(ID, sumAp) values (11,22)"
Dim Commande As New SqlCommand(Requete, MyConnexion)

MyConnexion.Open()

I get this message:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

I am a beginner at SQL...

My table is toto:

ID    sumAp
1     11
2     21

Thank you


Solution

  • To connect to Compact Edition databases, you need to instead use SqlCeConnection (in the System.Data.SqlServerCe namespace, if you haven't already imported it), so instead, I'd expect your code to look a little like:

    Using con As New SqlCeConnection("Data Source=C:\Users\W8\Documents\aa.sdf;Integrated Security=SSPI;Initial Catalog=toto")
        con.Open()
        Using cmd As New SqlCeCommand("Insert into toto(ID, sumAp) values (11,22)", con)
            cmd.ExecuteNonQuery()
        End Using
    End Using