一起和新手朋友学习一下WinCE下对数据库的操作方法
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemDataOleDb;
using SystemDataSqlServerCe;
using SystemIO;
namespace ReadSDFFileOnDesktop
{
public partial class MainForm : Form
{
public static string strSQLPath = DBsdf;
public static string strConString = Data Source = + strSQLPath + ; Password = ;
public static string strAccessString = Provider=MicrosoftJetOLEDB;Data Source=Pubmdb;;
public MainForm()
{
InitializeComponent();
}
private void buttonCreateDB_Click(object sender EventArgs e)
{
// 创建数据库
if (FileExists(strSQLPath))
{
// 删除数据库
FileDelete(strSQLPath);
CreateDataBase();
}
else
{
// 创建数据库
CreateDataBase();
}
}
private void CreateDataBase()
{
// 创建数据库文件
SqlCeEngine engine = new SqlCeEngine(strConString);
engineCreateDatabase();
engineDispose();
MessageBoxShow(数据库创建成功!);
}
private void buttonCreateTable_Click(object sender EventArgs e)
{
if (FileExists(strSQLPath))
{
// 创建表
SqlCeConnection cn = new SqlCeConnection(strConString);
cnOpen();
try
{
string strSQL = @CREATE TABLE employee(emp_id nvarchar()
fname nvarchar()minit nvarchar() lname nvarchar()
job_id nvarchar()job_lvl nvarchar() pub_id nvarchar()
hire_date datetime);
SqlCeCommand SqlCeCmd = new SqlCeCommand(strSQL cn);
SqlCeCmdExecuteNonQuery();
MessageBoxShow(数据库中的表创建成功 提示信息);
}
catch (SqlCeException ex)
{
MessageBoxShow(exMessage);
}
finally
{
cnClose();
}
}
else
{
MessageBoxShow(请先创建数据库!);
}
}
private void buttonImportData_Click(object sender EventArgs e)
{
// 导入数据
OleDbConnection OleDbCn = new OleDbConnection(strAccessString);
try
{
OleDbCnOpen();
OleDbCommand OleDbCmd = new OleDbCommand(SELECT * FROM employee OleDbCn);
OleDbDataReader OleDbDr = OleDbCmdExecuteReader();
SqlCeConnection SqlCeCn = new SqlCeConnection(strConString);
try
{
SqlCeCnOpen();
SqlCeCommand SqlCeCmd = new SqlCeCommand();
SqlCeCmdConnection = SqlCeCn;
while (OleDbDrRead())
{
string strSQL = ;
strSQL = StringFormat(INSERT INTO employee +
(emp_id fname minit lname job_id job_lvl pub_id hire_date)
VALUES ({} {} {} {} {} {} {} {})
OleDbDrGetValue(OleDbDrGetOrdinal(emp_id))
OleDbDrGetValue(OleDbDrGetOrdinal(fname))
OleDbDrGetValue(OleDbDrGetOrdinal(minit))
OleDbDrGetValue(OleDbDrGetOrdinal(lname))
OleDbDrGetValue(OleDbDrGetOrdinal(job_id))
OleDbDrGetValue(OleDbDrGetOrdinal(job_lvl))
OleDbDrGetValue(OleDbDrGetOrdinal(pub_id))
OleDbDrGetValue(OleDbDrGetOrdinal(hire_date)));
SqlCeCmdCommandText = strSQL;
SqlCeCmdExecuteNonQuery();
}
MessageBoxShow(数据导入成功!);
}
catch (SqlCeException ex)
{
MessageBoxShow(exMessage);
}
finally
{
SqlCeCnClose();
}
}
catch (OleDbException ex)
{
MessageBoxShow(exMessage);
}
finally
{
OleDbCnClose();
}
}
private void buttonShowData_Click(object sender EventArgs e)
{
// 显示数据
SqlCeConnection SqlCeCn = new SqlCeConnection(strConString);
try
{
SqlCeCnOpen();
string strSQL = ;
strSQL = StringFormat(SELECT * FROM employee );
SqlCeCommand SqlCeCmd = new SqlCeCommand(strSQL SqlCeCn);
SqlCeDataReader SqlCeDr = SqlCeCmdExecuteReader();
listViewItemsClear();
while (SqlCeDrRead())
{
ListViewItem item;
item = new ListViewItem(SqlCeDrGetValue
(SqlCeDrGetOrdinal(emp_id))ToString());
itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
GetOrdinal(fname))ToString());
itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
GetOrdinal(minit))ToString());
itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
GetOrdinal(lname))ToString());
itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
GetOrdinal(job_id))ToString());
itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
GetOrdinal(job_lvl))ToString());
itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
GetOrdinal(pub_id))ToString());
itemSubItemsAdd(SqlCeDrGetValue(SqlCeDr
GetOrdinal(hire_date))ToString());
listViewItemsAdd(item);
}
}
catch (SqlCeException ex)
{
MessageBoxShow(exMessage);
}
finally
{
SqlCeCnClose();
}
}
}
}