首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > WinCE >

请教CE自带数据库的开发

2012-03-19 
请问CE自带数据库的开发最近要开发一个基于wince的本地数据库,很简单的就可以,所以想用CE自带的数据库,但

请问CE自带数据库的开发
最近要开发一个基于wince的本地数据库,很简单的就可以,所以想用CE自带的数据库,但是在网上看了很多资料,大部分都是关于SQL的,有网友提到CE自带数据库的开发,但也只是提到了几个方法,如CeCreateDatabaseEx等。
我想请问大家大概的开发CEDB的流程是什么样的呢,还有CeCreateDatabaseEx是在System.Data.CEDB里吗?我怎么找不到呢?我装的是vs   2005.
谢谢大家!

[解决办法]
我使用vb.net是这样做的,你可以参考

Imports System.IO
Imports System.data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Data.SqlServerCe
Imports Wincon

Module globaldb

'数据库名称和路径。
Public Const DBPath As String = "\CompactFlash\JZKZ.sdf "
Public ssfields As String '保存实时存储数据库的实时数据字段名称

Public dbconn As New SqlCeConnection
Public dbcmd As New SqlCeCommand


'获取存在实时数据时的字段名称
Sub get_fields_name()
Dim i As Integer
ssfields = "时间 "
For i = 1 To Pac1_MaxCDNo
ssfields = ssfields + ", " + CD_Info(i).name
Next
End Sub

#Region " 机组控制 新建数据库 by w.c.d 2006.11.12 "
'新建发电机组控制系统的数据库。
' by w.c.d 2006-11-12
Sub createDB()
' Dim dbconn As New SqlCeConnection
' Dim dbcmd As New SqlCeCommand
dbconn.ConnectionString = "data source = " + DBPath
dbcmd.Connection = dbconn
' File.Delete(DBPath)
If File.Exists(DBPath) Then
'dbconn.ConnectionString = "data source = " + DBPath
dbcmd.Connection = dbconn
Exit Sub
' If MsgBox( "数据库已经存在,你确定需要重新建立(新建会删除全部数据)? ", MsgBoxStyle.DefaultButton2 Or _
' MsgBoxStyle.Critical Or MsgBoxStyle.YesNo, "对话框 ") = MsgBoxResult.No Then ' User chose Yes.
'Exit Sub
' End If
' Application.DoEvents()
' File.Delete(DBPath)
End If

'Dimdbconn.As(New SqlCeConnection)
' Dim cmd As New SqlCeCommand
Dim ds As DataSet
Dim dlg As New Form
' if database does not exist, create one
If Not File.Exists(DBPath) Then
Dim sqlEngine As New SqlCeEngine( "Data Source= " + DBPath)
sqlEngine.CreateDatabase() '生成数据库

'连接数据库
dbconn.ConnectionString = "data source = " + DBPath
dbconn.Open()
dbcmd.Connection = dbconn

'生成 发电机组测点控制信息
dbcmd.CommandText = "CREATE TABLE CDDY(ID int Primary Key NOT NULL, " & _
"测点名称 nvarchar(20), " & _
"卡号 tinyint, " & _
"站号 tinyint, " & _
"通道号 tinyint, " & _
"工程单位 nvarchar(8) " & _
") "
dbcmd.ExecuteNonQuery()
'把测点信息存入数据库
Dim i As Integer
For i = 1 To Pac1_MaxCDNo
dbcmd.CommandText = "insert into cddy (id,测点名称,卡号,站号,通道号,工程单位) values ( " _
+ CD_Info(i).ID.ToString() + ", ' " + CD_Info(i).name + " ', " + CD_Info(i).kh.ToString + ", " _
+ CD_Info(i).zh.ToString + ", " + CD_Info(i).tdh.ToString + ", ' " + CD_Info(i).dw + " ') "
dbcmd.ExecuteNonQuery()
Next

'生成 实时存储数据


'Dim i As Integer
Dim tmp As String
tmp = "时间 datetime "
For i = 1 To Pac1_MaxCDNo
tmp = tmp + ", " + CD_Info(i).name + " float "
Next
dbcmd.CommandText = "CREATE TABLE SSdata( " + tmp + ") "
dbcmd.ExecuteNonQuery()

'cmd.CommandText = "alter TABLE SSdata add " + CD_Info(41).name + " float "
'cmd.ExecuteNonQuery()

'生成系统日志
dbcmd.CommandText = "CREATE TABLE SysLog(时间 datetime , " & _
"类型 nvarchar(12), " & _
"说明 nvarchar(30) " & _
") "
dbcmd.ExecuteNonQuery()

'生成电量累计和时间累计。
dbcmd.CommandText = "CREATE TABLE sumLog(时间 datetime , " & _
"累计电量 float, " & _
"累计时间 float " & _
") "
dbcmd.ExecuteNonQuery()

'生成报警数据
dbcmd.CommandText = "CREATE TABLE BJData(时间 datetime , " & _
"报警类型 nvarchar(12), " & _
"报警说明 nvarchar(50) " & _
") "
dbcmd.ExecuteNonQuery()
dbconn.Close()
' MsgBox( "数据库建立成功! ")
Else
MsgBox( "该数据库已经存在,不需要建立! ")
End If
' dbconn.Dispose() : dbcmd.Dispose() :
dlg.Dispose()
Application.DoEvents()
End Sub

热点排行