HOW TO: Create a Database, a Table, and Run a Query (SQL Server CE Sample)
This article was previously published under Q320785
This article has been archived. It is offered "as is" and will no longer be updated.
IN THIS TASK
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.
How to Create a DatabaseTo create a database, use this code sample:
Copy, and then paste the following code into the Command1_Click event:
back to the top
Dim cat As ADOXCE.CatalogSet cat = CreateObject("ADOXCE.Catalog.3.1")cat.Create "Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0; data source=\DATAB.sdf"MsgBox "Database Created"
How to Create a TableTo create a table, use this code sample:
Copy, and then paste the following code into the Command2_Click event:
Dim str_Connection As StringDim cn As ADOCE.Connectionstr_Connection = "Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0; data source=\DATAB.sdf"Set cn = CreateObject("ADOCE.Connection.3.1")cn.Open str_Connectioncn.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.CloseSet cn = NothingMsgBox "Table Created"
back to the top
How to Query a TableTo 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".
SQL Server CE Books Online; topic: "Error Handling"
For additional information, visit the following Microsoft Web sites:
Troubleshooting SQL Server CE Connectivity Issues
SQL Server CE 1.1 Service Pack 1 Update
back to the top
Article ID: 320785 - Last Review: 12/07/2015 10:28:44 - Revision: 2.4
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
- kbnosurvey kbarchive kbgraphxlink kbhowto kbhowtomaster KB320785