Microsoft KB Archive/320785

= HOW TO: Create a Database, a Table, and Run a Query (SQL Server CE Sample) =

Article ID: 320785

Article Last Modified on 1/3/2007

-

APPLIES TO


 * Microsoft Encarta Interactive World Atlas 2001
 * 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 Service Pack 1

-



This article was previously published under Q320785



IN THIS TASK
SUMMARY
 * SQL Server CE Sample
 * How to Create a Database
 * How to Create a Table
 * How to Query a Table

REFERENCES



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(&quot;ADOXCE.Catalog.3.1&quot;) cat.Create &quot;Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0; data source=\DATAB.sdf&quot; MsgBox &quot;Database Created&quot; 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 = &quot;Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0; data source=\DATAB.sdf&quot; Set cn = CreateObject(&quot;ADOCE.Connection.3.1&quot;) cn.Open str_Connection cn.Execute &quot;Create table table1(id int NOT NULL, field1 nvarchar(1))&quot; cn.Execute &quot;CREATE UNIQUE INDEX idx1 ON table1(id)&quot; cn.Execute &quot;insert table1 values (1, 'A')&quot; cn.Execute &quot;insert table1 values (2, 'B')&quot; cn.Execute &quot;insert table1 values (3, 'C')&quot; cn.Close Set cn = Nothing MsgBox &quot;Table Created&quot;

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(&quot;ADOCE.Connection.3.1&quot;) Set rs = CreateObject(&quot;ADOCE.Recordset.3.1&quot;) cn.Open &quot;Provider=Microsoft.SQLSERVER.OLEDB.CE.1.0; data source=\DATAB.sdf&quot; rs.Open &quot;select * from table1 where field1='A'&quot;, 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

</li> Save the project, and then run the project either through an emulator or a device.</li> Click the Create Database command button.</li> Click the Create Table command button.</li> Click the Run Query command button. Note the message box that displays the character &quot;A&quot;.</li></ol>

back to the top

<div class="references_section">