SQLServer Compact 4.0 中的应用(一),基础配置
在继续往下看之前,首先一定要保证你的IIS运行 于 ASP.Net 4.0 ,这是唯一的 要求 , SQLServer Compact 4.0 只能运行于4.0环境,没有商量的余地。如果做不到,要么改善一下与服务器管理员的人际关系,要么就换服务器。 虽然我也知道有办法可以让 SQLServer
在继续往下看之前,首先一定要保证你的IIS运行于 ASP.Net 4.0 ,这是唯一的要求,SQLServer Compact 4.0只能运行于4.0环境,没有商量的余地。如果做不到,要么改善一下与服务器管理员的人际关系,要么就换服务器。
虽然我也知道有办法可以让 SQLServer Compact 3.5运行于ASP.NET 2.0-3.5,但是,CodeFirst与LINQ一定会有错误发生。
至于那些还在用1.1环境的朋友,我不想说技术问题,只希望你能明白:今年已经是世界末日。
至于桌面应用环境,一样需要.net 4.0环境。
本文的运行环境是ASP.NET Webform 4.0 ,你也可以选择MVC环境。 既然标题说了是 SQLServer Compact 4.0, 那么如果你的服务器还没有安装SQLServer Compact 4.0的话,别紧张, 不需要你安装任何东西。 只需要把下面文件放入Bin文件夹就行了:
http://files.cnblogs.com/kvspas/SQLServerCE4_Bin.rar
里面的东西一定很多(主要是64和32都有),肯定有可以去掉的,但是我没那精力去研究哪些是需要的,哪些是可以去掉的,如果你研究出来了,谢谢汇报一下。
然后是配置EntityFramework 4.1 :我们可以通过Nuget或者手动下载的方式来安装 EntityFramework 4.1 ,这里我推荐后者。
安装文件下载地址: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=8363
如果你没有服务器的配置权限,就把下面的文件放入Bin文件夹就行了:
http://files.cnblogs.com/kvspas/EntityFramework.rar
项目中引用它:
以及,引用System.ComponentModel.DataAnnotations:
接下来就可以开始正题了:
步骤一:创建数据表
不要看那些类似广告的教程吹得天花乱坠,即使是POCO编码,也并非绝对“干净”的,必须至少定义一个特性来标识主键([Key]),要不然的话,我不说,你可以试试看为了保持所谓的“美观”,而把[Key]特性去掉会是什么后果。
public class LiangyichenDB
{
//主键,必须定义
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int index { get; set; }
public string title { get; set; }
public DateTime pubdate { get; set; }
}
步骤二:创建数据库访问对象:
public class LiangyichenDBContext : DbContext
{
public DbSet
}
以上两个类型需要引用命名空间:
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
好了,以上两个步骤通用于任意数据库,和 SQLServer Compact 并没有绝对的关系,接下来是开始 配置 SQLServer Compact :
不需要你去创建实际的数据库文件, 你只需要在 Web.Config 的 connectionStrings 中设置这么一段话:
上面虽然说了不需要你创建实际的数据库文件名,但是需要事先保证 App_Code文件夹已经存在, 不要问我为什么。
谨记: name="LiangyichenDBContext" , name的值必须与数据库访问对象同名,同理,不要问我为什么,也不要浪费精力去探讨或者改变什么,你只需要明白太阳一定是从东边升起的,古往今来的种种惨案告诉我们:知道得太多不是好事。
至于数据库文件名,可以任意。
最后,在页面对应代码中添加:
protected void Page_Load(object sender, EventArgs e)
{
var context = new LiangyichenDBContext();
context.Items.Add(new LiangyichenDB { title = "1", pubdate = DateTime.Now });
context.Items.Add(new LiangyichenDB { title = "2", pubdate = DateTime.Now });
context.Items.Add(new LiangyichenDB { title = "3", pubdate = DateTime.Now });
context.SaveChanges();
foreach (var i in context.Items)
{
output(i.title);
}
}
void output(string str)
{
Response.Write(str+"
");
}
行了,运行一下,你就会发现App_Code文件夹中已经自动生成了Database.sdf 文件。
SQLServer Compact 4.0 的 Code-First 基本配置结束。
下面是项目文件下载地址,在VisualStudio2010中启动成功。
http://files.cnblogs.com/kvspas/Webform-SQLServer-Code-First.rar
(注意我说的是“项目文件”,所以你一定需要在VisualStudio2010中打开sln文件运行,不能直接弄到IIS下面启动)

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

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

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

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
