一般CE上面的数据库程序都包括以下命名空间(包括与PC交互数据),这是从一个项目的单元文件里面拷出来的,调试环境:Windows 2003 + VS.NET 2003 + Sql Server 2000 + Sql Server CE 2.0 + Pocket PC 2003 SDK
以下代码中很多*号,是故意处理的,你可以用你自己的变量代替
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
using System.Data.SqlServerCe;
using System.Runtime.InteropServices;
using System.Threading;
using System.Data.SqlClient;
数据库连接代码,如下分别为连接PDA本机的SQLCE数据库和PC上的SQLSERVER数据库
private string strFile = @"My Documents\****.sdf";
private string strConn = "Data Source=" +
@"My Documents\****.sdf";
// Connection string.
string SqlConn =
"data source=Server;" +
"initial catalog=****;" +
"user id=sa;" +
"pwd=sa;" +
"workstation id=Sercer;" +
"packet size=4096;" +
"persist security info=False;";
建立数据库
{
if ( File.Exists(strFile) ) { File.Delete(strFile); }
SqlCeEngine dbEngine = new SqlCeEngine();
dbEngine.LocalConnectionString = strConn;
try
{
dbEngine.CreateDatabase();
}
catch( SqlCeException exSQL )
{
MessageBox.Show("Unable to create database at " +
strFile +
". Reason: " +
exSQL.Errors[0].Message );
}
}
建立数据表
SqlCeConnection connDB = new SqlCeConnection();
SqlCeCommand cmndDB = new SqlCeCommand();
connDB.ConnectionString = strConn;
connDB.Open();
cmndDB.Connection = connDB;
//
cmndDB.CommandText =
" CREATE TABLE *** " +
" ( ID integer IDENTITY (1, 1) not null " +
" CONSTRAINT PKID PRIMARY KEY" +
" , *** integer not null" +
// " CONSTRAINT ***" +
" , *** nchar(30) " +
" )";
cmndDB.ExecuteNonQuery();
//
cmndDB.CommandText =
" CREATE TABLE UserData " +
" ( ** integer IDENTITY (1, 1) not null " +
" CONSTRAINT PKUserID PRIMARY KEY " +
" , *** integer not null " +
// " , CONSTRAINT *** " +
// " foreign key (***) " +
// " references Cat(***) " +
" , *** nchar(5) not null " +
" , *** nchar(5) not null " +
" , *** real " +
" , *** nchar(20) " +
" , *** nchar(10) " +
" )";
cmndDB.ExecuteNonQuery();
connDB.Close();
以下代码中很多*号,是故意处理的,你可以用你自己的变量代替
CODE:
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
using System.Data.SqlServerCe;
using System.Runtime.InteropServices;
using System.Threading;
using System.Data.SqlClient;
数据库连接代码,如下分别为连接PDA本机的SQLCE数据库和PC上的SQLSERVER数据库
CODE:
private string strFile = @"My Documents\****.sdf";
private string strConn = "Data Source=" +
@"My Documents\****.sdf";
// Connection string.
string SqlConn =
"data source=Server;" +
"initial catalog=****;" +
"user id=sa;" +
"pwd=sa;" +
"workstation id=Sercer;" +
"packet size=4096;" +
"persist security info=False;";
建立数据库
CODE:
{
if ( File.Exists(strFile) ) { File.Delete(strFile); }
SqlCeEngine dbEngine = new SqlCeEngine();
dbEngine.LocalConnectionString = strConn;
try
{
dbEngine.CreateDatabase();
}
catch( SqlCeException exSQL )
{
MessageBox.Show("Unable to create database at " +
strFile +
". Reason: " +
exSQL.Errors[0].Message );
}
}
建立数据表
CODE:
SqlCeConnection connDB = new SqlCeConnection();
SqlCeCommand cmndDB = new SqlCeCommand();
connDB.ConnectionString = strConn;
connDB.Open();
cmndDB.Connection = connDB;
//
cmndDB.CommandText =
" CREATE TABLE *** " +
" ( ID integer IDENTITY (1, 1) not null " +
" CONSTRAINT PKID PRIMARY KEY" +
" , *** integer not null" +
// " CONSTRAINT ***" +
" , *** nchar(30) " +
" )";
cmndDB.ExecuteNonQuery();
//
cmndDB.CommandText =
" CREATE TABLE UserData " +
" ( ** integer IDENTITY (1, 1) not null " +
" CONSTRAINT PKUserID PRIMARY KEY " +
" , *** integer not null " +
// " , CONSTRAINT *** " +
// " foreign key (***) " +
// " references Cat(***) " +
" , *** nchar(5) not null " +
" , *** nchar(5) not null " +
" , *** real " +
" , *** nchar(20) " +
" , *** nchar(10) " +
" )";
cmndDB.ExecuteNonQuery();
connDB.Close();