在没有 ADO 的情况下访问 MS SQL Server CE v1.0/v2.0
在 MS SQL Server CE v1.0/v2.0 中添加、编辑和删除记录/表/数据库。
引言
我相信对于那些将要实现或已经实现了 MS SQL Server CE v1.0/2.0 的人来说,已经使用了微软提供的免费工具 (isqlw_wce.exe),并且总是在脑海中有一个大大的问号,关于如何编写这样的应用程序,对吧?
所以,这可能是对于那些想要了解更多关于此内容的人来说是正确的文章。在本文的最后,您将能够访问您已安装在 PocketPC 设备中的 MS SQL Server CE 数据库,其中包含您自己的宝贵信息,并使您的应用程序更强大,并朝着企业级发展。 :)
为什么我们需要 MS SQL Server CE 而不是 CD 数据库?因为 MS SQL Server CE 提供了更好的性能,并支持我们一直熟悉的 SQL DDM/DDL。这意味着,它将加速我们的应用程序/产品开发周期。
背景
为了利用各种现成的函数,必须具备 SQL DDM 和 DDL 的基本知识。因为您需要编写自己的 SQL 语句来创建表,以及从任何表中添加/更新/删除记录。
使用代码
在您开始查看代码之前,您需要记住 MS SQL Server CE 仅支持在任何时候与任何数据库的单一连接。
当您查看 SqlSvrCE.cpp/.h 时,您将获得所有现成的函数,您最终将从自己的 WIN32API 或 MFC 应用程序中调用这些函数,这取决于您的选择。
在您开始下载示例代码之前,让我们看一下您可以在这个免费的源中拥有的函数。
// Create a new SQL Server CE database provider. HRESULT CreateSqlSvrCeProvider (void); // Create a new SQL Server CE database session right // after successful connect to the pass in database // name (*.sdf) in fullpath. HRESULT CreateDBSession (void); // Retrieve the last known OLE DB error message, due to // error occur through out the database access process. HRESULT GetErrorMessage (LPTSTR lpszBuffer, int MaxBuffer); // Connect the SQL Server CE database with reference to // the pass in database name (*.sdf) in fullpath. HRESULT ConnectDB (LPTSTR lpszDBName); // Disconnect from the current open SQL Server CE database // with reference to the pass in database name (*.sdf) in // fullpath under the CreateDB/ConnectDB function. HRESULT DisconnectDB (LPTSTR lpszDBName); // Create the SQL Server CE database with reference to // the pass in database name (*.sdf) in fullpath. HRESULT CreateDB (LPTSTR lpszDBName); // Delete the SQL Server CE database with reference to // the pass in database name (*.sdf) in fullpath. HRESULT DeleteDB (LPTSTR lpszDBName); // Compact the SQL Server CE database with reference to // the pass in database name (*.sdf) in fullpath. HRESULT CompactDB (LPTSTR lpszDBName); // Create new table with reference to // the pass in table name and first column information // DUE TO SQL SERVER CE 2.0 MUST HAVE AT LEAST 1 // COLUMN IN THE CREATED TABLE. HRESULT CreateTable (LPTABLECOLUMNINFO lptci); // Delete the existing table with reference to // the pass in table name.. HRESULT DropTable (LPTSTR lpszTableName); // Create new column with reference to // the pass in information in TABLECOLUMNINFO structure. HRESULT CreateColumn (LPTABLECOLUMNINFO lptci); // Drop the existing column with reference to // the pass in information in TABLECOLUMNINFO structure. HRESULT DropColumn (LPTABLECOLUMNINFO lptci); // Create new index with reference to // the pass in information in TABLEINDEXINFO structure. HRESULT CreateIndex (LPTABLEINDEXINFO lptii); // Drop the existing index with reference to // the pass in information in TABLEINDEXINFO structure. HRESULT DropIndex (LPTABLEINDEXINFO lptii); // Execute the SQL Statement with does not require to // return a RowSet object. // EXAMPLE: // SELECT, DELETE, CREATE TABLE, DROP TABLE, INSERT INTO & etc... HRESULT ExecuteSQL (LPTSTR lpszQuery); // Load the records into a RowSet object with reference // to the pass in SQL statement. HRESULT GetRowset (LPTSTR lpszQuery); // Process the Rowset object created by the GetRowset function. HRESULT ProcessRowset (IRowset *pRowset);
总共有十一个函数,如上所示,您需要记住并在您的数据库应用程序中调用它们。
但请注意,ProcessRowset
是完全定制的,以满足所附示例应用程序的要求,该应用程序将把所有检索到的记录和列标题显示到提供的列表视图控件中。因此,您需要修改 ProcessRowset
函数,甚至编写您自己的 ProcessRowset
函数来解释您从 MS SQL Server CE 数据库中读取的数据。
所有必要的注释都可以在源文件中找到,有了这些注释,我相信当您开始修改或编写自己的 ProcessRowset
函数时,它会使您的生活更轻松。
因此,使用上述函数非常简单。您所需要的只是使用正确的对应参数调用相关函数。
例如,您希望在 \Windows 文件夹 中创建一个名为 MyDb.sdf 的数据库。您所需要的只是调用 CreateDB
如下
_tcscpy(szDBName, TEXT("\\windows\\MyDb.sdf")); hr = CreateDB(szDBName); // Validation if (!FAILED(hr)) // Display result message text. MessageBox(hWnd, TEXT("Database successful created."), TEXT("Infor"), MB_OK); else { // Get OLE DB error message GetErrorMessage(szErrMessage, wcslen(szErrMessage)); // if (E_OUTOFMEMORY == hr) { // Compose result message wsprintf(szErrMessage, TEXT("(0x%x) Out of memory!"), hr); } // Display result message text. MessageBox(hWnd, szErrMessage, TEXT("Error"), MB_ICONSTOP | MB_OK); }
您可能会注意到,示例程序的用户界面不适合 MS PocketPC (240x320) 屏幕。这是因为示例应用程序是基于 Windows CE .NET 平台开发的,我所做的只是创建一个新的 PocketPC 应用程序并直接复制所有相关的 cpp/h 文件。希望您不要介意。
最后但并非最不重要的一点是,我希望您喜欢示例代码,并希望它能帮助您在您的任何应用程序开发周期中。
更新
基于之前的源文件,总共实现了 6 个新函数,这 6 个函数是
CreateTable
DropTable
CreateColumn
DropColumn
CreateIndex
DropIndex
这 6 个函数将为您提供一种简单的方法,通过编写复杂的 SQL-92 命令来管理您的数据库。