在继续往下看之前,首先一定要保证你的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下面启动)

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.


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

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

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