如何将数据导入到 SQL Server Compact Edition 数据库中(五)
系列文章导航: 如何将数据导入到 SQL Server Compact Edition 数据库中(一) 如何将数据导入到 SQL Server Compact Edition 数据库中(二) 如何将数据导入到 SQL Server Compact Edition 数据库中(三) 如何将数据导入到 SQL Server Compact Edition 数
系列文章导航:
如何将数据导入到 SQL Server Compact Edition 数据库中(一)
如何将数据导入到 SQL Server Compact Edition 数据库中(二)
如何将数据导入到 SQL Server Compact Edition 数据库中(三)
如何将数据导入到 SQL Server Compact Edition 数据库中(四)
摘要:在本系列文章中,我已经尝试在 Windows 桌面平台和 Windows Mobile 平台上将数据导入到 SQL Server Compact Edition 数据库中。不过,之前的示例程序都属于 Windows 应用程序。本文将介绍如何在 ASP.NET Web 应用程序中实现 SQL Server Compact Edition 数据库的生成和数据导入。
一、创建 ASP.NET 版的数据导入程序
1.使用 Visual Studio 2005 新建一个 Visual C# 版本的“ASP.NET Web 应用程序”项目,项目名称为“CopyTableASPNET”。
2.为新建的“CopyTableASPNET”项目添加对 System.Data.SqlServerCe.dll 程序集的引用,注意版本号是 9.0.242.0。这里特别声明版本号是考虑到大家可能安装了 Visual Studio 2008,它自带了最新的 SQL Server Compact 3.5,版本号是 3.5.0.0,可能也会出现在添加引用对话框中。而本文的示例是针对 SQL Server 2005 Compact Edition (v3.1) 编写的,程序集版本号为 9.0.242.0。
3.在“解决方案资源管理器”打开 Default.aspx 页面,默认显示的是 HTML 代码视图,需要切换到设计视图。从工具箱拖拽一个 Button 控件到页面上,修改其 Text 属性为“生成 SQL Server Compact 数据库”。
4.打开 Default.aspx 页面的 C# 代码编辑视图,或者直接打开 Default.aspx.cs 文件。从本系列文章第四篇的示例代码中,复制 From1.cs 文件的 GetTableNames, CopyTable, VerifyDatabaseExists, GenerateTableSchemaSql, GetSqlCeDataType 和 GetSqlCeNativeType 等方法到 Default.aspx.cs 文件中。
5.回到 Default.aspx 页面的设计视图,用鼠标双击 Button 控件,此时会跳到 Default.aspx.cs 的代码编辑视图,并生成了 Button 控件的 Click 事件处理方法。将以下代码复制到 Click 事件处理方法中:
// 创建源 SQL Server 数据库连接对象
string srcConnString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";
SqlConnection srcConnection = new SqlConnection(srcConnString);
// 创建目标 SQL Server Compact Edition 数据库连接对象
string destConnString = string.Format("Data Source={0};", Server.MapPath("Northwind.sdf"));
SqlCeConnection destConnection = new SqlCeConnection(destConnString);
VerifyDatabaseExists(destConnString);
srcConnection.Open();
destConnection.Open();
// 复制数据
string[] tableNames = GetTableNames(srcConnection);
string query;
for (int i = 0; i tableNames.Length; i++)
{
query = string.Format("SELECT * FROM [{0}]", tableNames[i]);
CopyTable(srcConnection, destConnection, query, tableNames[i]);
}
srcConnection.Close();
destConnection.Close();
// 重定向到生成的 Northwind.sdf 文件的 url 地址(使浏览器提示下载)
Response.Redirect("Northwind.sdf");
这段代码跟本系列文章第四篇的示例代码非常相似,大家注意代码中加亮的语句。
6.数据导入的代码已经编写完成,现在可以生成项目,并在浏览器中查看 Default.aspx 页面。
出错了!到底是什么原因呢?
二、解锁 SQL Server Compact Edition 对 ASP.NET 的支持
我在《SQL Server精简版支不支持ASP.NET?》曾经探讨过这个问题。“当前 SQL Server Compact Edition 并未针对网站数据库用途进行优化。默认情况下,在 SQL Server Compact Edition 中阻塞了来自 ASP.NET 应用程序的连接。SQL Server Compact Edition 经过优化后,可在应用程序中作为嵌入数据库使用。使用 SQL Server Compact Edition 作为网站数据库要求支持多用户和并发数据更改。这可能会导致性能问题。因此,不支持这些情形。其他版本的 SQL Server 2005(包括 SQL Server 2005 Express Edition)经过优化后,也可作为网站数据库使用。在使用 ASP.NET 创建 SQL Server Compact Edition 数据库以实现同步操作的应用方案中,SQL Server Compact Edition 可与 ASP.NET 配合使用。使用下列代码更改 SQL Server Compact Edition 的默认行为,以便与 ASP.NET 配合使用:AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true)”——引自《SQL Server 2005 Compact Edition 联机丛书》
7.为了解决步骤6出现的错误,我在 Default.aspx 页面上添加一个 CheckBox 控件 chkEnableSqlCe,并将它的 Text 属性设置为“允许在 ASP.NET 使用 SQL Server Compact”。在 Default.aspx.cs 的 Page_Load 方法中添加以下代码:
if (chkEnableSqlCe.Checked)
{
AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
}
else
{
AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", false);
}
8.重新编译项目,并浏览 Default.aspx 页面。页面加载完成后,选中“允许在 ASP.NET 使用 SQL Server Compact”检查框,再点击“生成 SQL Server Compact 数据库”按钮。此时,Default.aspx 页面将在后台生成 SQL Server Compact Edition 数据库文件 Northwind.sdf,并从 SQL Server 服务器的 Northwind 数据库导入数据,最后将提示您下载 Northwind.sdf 文件。
三、总结
在很多场景中,我们可以使用 ASP.NET 根据各种条件在服务器端快速生成 SQL Server Compact Edition 数据库文件(.sdf),并从数据库服务器导入初始数据,再将动态生成的数据库文件提供给客户端程序下载。这样可以加速和简化 Windows Mobile 应用程序初始化本地数据库的过程,因为只需要传输文件而不需要进行复杂的数据同步。本文介绍的方案并没有结束,实际上 SQL Server Compact Edition 数据库文件的压缩率是很高的。例如:本文的示例代码生成的 Northwind.sdf 文件是 788 KB,经过 ZIP 标准压缩后生成的 Northwind.zip 文件是 228 KB。为了减少数据传输流量,同时也是为了节约时间,我们还可以对生成的数据库文件进行压缩后,再提供给客户端下载,客户端再对文件进行解压缩。
示例代码下载:sqlce_data_import5.rar
作者:黎波
博客:http://upto.cnblogs.com/
日期:2008年3月5日

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Fujifilm fans were recently very excited at the prospect of the X-T50, since it presented a relaunch of the budget-oriented Fujifilm X-T30 II that had become quite popular in the sub-$1,000 APS-C category. Unfortunately, as the Fujifilm X-T50's launc

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

The Fujifilm X-M5 has shown itself in a handful of rumours that suggested that the compact APS-C camera would launch as an affordable alternative to the X100VI sometime in late 2024. Now, a new rumour out of Fujirumours reveals Fujifilm's film simula

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

New SOTA for multimodal document understanding capabilities! Alibaba's mPLUG team released the latest open source work mPLUG-DocOwl1.5, which proposed a series of solutions to address the four major challenges of high-resolution image text recognition, general document structure understanding, instruction following, and introduction of external knowledge. Without further ado, let’s look at the effects first. One-click recognition and conversion of charts with complex structures into Markdown format: Charts of different styles are available: More detailed text recognition and positioning can also be easily handled: Detailed explanations of document understanding can also be given: You know, "Document Understanding" is currently An important scenario for the implementation of large language models. There are many products on the market to assist document reading. Some of them mainly use OCR systems for text recognition and cooperate with LLM for text processing.
