SUMMARY
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.
back to the top
How to Create a Database
To create a database, use this code sample:
Copy, and then paste the following code into the
Command1_Click event:
Dim cat As ADOXCE.Catalog
Set cat = CreateObject("ADOXCE.Catalog.3.1")
cat.Create "Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0; data source=\DATAB.sdf"
MsgBox "Database Created"
back to the top
How to Create a Table
To create a table, use this code sample:
Copy, and then paste the following code into the
Command2_Click event:
Dim str_Connection As String
Dim cn As ADOCE.Connection
str_Connection = "Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0; data source=\DATAB.sdf"
Set cn = CreateObject("ADOCE.Connection.3.1")
cn.Open str_Connection
cn.Execute "Create table table1(id int NOT NULL, field1 nvarchar(1))"
cn.Execute "CREATE UNIQUE INDEX idx1 ON table1(id)"
cn.Execute "insert table1 values (1, 'A')"
cn.Execute "insert table1 values (2, 'B')"
cn.Execute "insert table1 values (3, 'C')"
cn.Close
Set cn = Nothing
MsgBox "Table Created"
back to the top
How to Query a Table
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.Connection
Dim rs As ADOCE.Recordset
Set 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, adLockReadOnly
Dim x As Integer
x = 0
For x = 0 To rs.RecordCount - 1
MsgBox rs(1).Value
rs.MoveNext
Next
rs.Close
Set rs = Nothing
cn.Close
Set 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".
back to the top