This article contains a step-by step sample that creates a database, a table, and that adds records to a SQL Server CE database. The sample also queries the new table and displays the records in the table.
SQL Server CE Sample
Open a new project in Microsoft eMbedded Visual Basic. Form1 is created by default.
Add three command buttons to Form1.
Change the caption for the command button named Command1 to Create Database. Change the caption for the command button named Command2 to Create Table. Change the caption for the command button named Command3 to Run Query.
On the Project menu, click References. Make sure that the following references are selected:
Microsoft CE ADO Ext. 3.1 for DDL Microsoft CE SQL Server Control 1.0 Microsoft CE ADO Control 3.1
To make sure that the Microsoft ADO for Windows CE SDK (ADOCE) and SQL Server CE components are downloaded to the device, on the Project menu, click Properties. On the General tab you will see an Update Components section. In the Frequency for the Update Components combo box, click to select Always for the frequency. In the Component to Update section, click to select both the Runtime Files and the Project Components check boxes.
To query the table you created previously, follow these steps:
Copy, and then paste the following code into the Command3_Click event:
Dim cn As ADOCE.ConnectionDim rs As ADOCE.RecordsetSet cn = CreateObject("ADOCE.Connection.3.1")Set rs = CreateObject("ADOCE.Recordset.3.1")cn.Open "Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0; data source=\DATAB.sdf"rs.Open "select * from table1 where field1='A'", cn, adOpenKeyset, adLockReadOnlyDim x As Integerx = 0For x = 0 To rs.RecordCount - 1 MsgBox rs(1).Value rs.MoveNextNextrs.CloseSet rs = Nothingcn.CloseSet cn = Nothing
Save the project, and then run the project either through an emulator or a device.
Click the Create Database command button.
Click the Create Table command button.
Click the Run Query command button.Note the message box that displays the character "A".
Microsoft SQL Server 2000 Windows CE Edition, Microsoft SQL Server 2000 Windows CE Edition 1.1, Microsoft SQL Server 2000 Windows CE Edition 1.1 Service Pack 1, Microsoft SQL Server 2000 Windows CE Edition 1.1 SP1 Update