Creating a SQL Server CE Database
The following procedures show how to create a new Microsoft® SQL Server™ Windows® CE Edition (SQL Server CE) database in a new Microsoft Visual Studio® .NET project.
By performing the steps in these procedures, you can:
Create a new SQL Server CE database.
Create a table in the database.
Populate the table with data.
For the complete code that you can copy, paste, and run in a Visual Studio .NET project, see "Walkthrough Sample" earlier in this topic.
To create a new SQL Server CE database
Start Visual Studio .NET and open a new project.
Create references to the namespaces you are using:
using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
using System.Collections;
using System.Windows.Forms;
using System.Data.Common;
Create the WalkThrough class:
public class WalkThrough
{
static void Main()
{
SqlCeConnection conn = null;
try
{
Verify that a database with the name you plan to use does not already exist:
if (File.Exists ("Test.sdf") )
File.Delete ("Test.sdf");
Create an empty database, by using the SqlCeEngine object:
SqlCeEngine engine = new SqlCeEngine ("Data Source = Test.sdf");
engine.CreateDatabase ();
Connect to the new database, by using the SqlCeConnection object:
conn = new SqlCeConnection ("Data Source = Test.sdf");
conn.Open();
To create a new table
Create an instance of the command class, by using the SqlCeCommand object:
SqlCeCommand cmd = conn.CreateCommand();
Run a command to create the table:
cmd.CommandText = "CREATE TABLE TestTbl(col1 int PRIMARY KEY, col2 ntext, col3 money)";
cmd.ExecuteNonQuery();
To populate a new table with data
Run a command to insert a row of data:
cmd.CommandText = "INSERT INTO TestTbl(col1, col2, col3) VALUES (0, 'abc', 15.66)";
cmd.ExecuteNonQuery();
Using SqlCeParameter, create a command using parameters to insert data to the table multiple times:
cmd.CommandText = "INSERT INTO TestTbl(col1, col2, col3) VALUES (?, ?, ?)";
Execute the parameterized command to insert data to the table:
cmd.Parameters["p1"].value = 1;
cmd.Parameters["p2"].value = "abc";
cmd.Parameters["p3"].value = 15.66;
cmd.ExecuteNonQuery();
Clear the parameter, and check the data you inserted into the table:
cmd.Parameters.Clear();
//Set the command text to a SELECT query.
//
cmd.CommandText = "SELECT * FROM TestTbl";
Reading SQL Server CE Database Data
The following example shows how to read data in an existing SQL Server CE database by using the SqlCeDataReader class:
For the complete code that you can copy, paste, and run in a Visual Studio .NET project, see "Walkthrough Sample" earlier in this topic.
Updating Data in a SQL Server CE Database
The following procedures show how to update a SQL Server CE table by using the objects and classes that are used earlier in Creating a SQL Server CE Database and Reading SQL Server CE Data.
By performing the steps in these procedures, you can:
Change existing data in a SQL Server CE table.
Read data in a SQL Server CE table.
Handle errors.
For the complete code that you can copy, paste, and run in a Visual Studio .NET project, see the "Walkthrough Sample" earlier in this topic.
To update data in a SQL Server CE table
Set the command object to use the UPDATE statement:
cmd.CommandText = "UPDATE TestTbl SET col2 = 'some new value' WHERE col1 = 0";
cmd.ExecuteNonQuery();
To read data in a SQL Server CE table
Set the command object to use the SELECT statement:
cmd.CommandText = "SELECT * FROM TestTbl";
Create an instance of SqlCeDataReader to read the data:
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
MessageBox.Show("col1 = " + rdr.GetInt32(0) +
"col2 = " + rdr.GetString(1) +
"col3 = " + rdr.GetSqlMoney(2));
}
}
Catch any errors by using SqlCeException, and close the connection to the database:
catch (SqlCeException e)
{
ShowErrors(e);
}
finally
{
if(conn.State == ConnectionState.Open)
conn.Close();
}
}
public static void ShowErrors(SqlCeException e)
{
SqlCeErrorCollection errorCollection = e.Errors;
StringBuilder bld = new StringBuilder();
Exception inner = e.InnerException;