Ado.net设计模式
连接类 连接类有固定的使用模式,这是常用的乐观模式: using (var conn = new SqlConnection(connstr)){conn.Open();//执行各种数据库操作} 悲观的、防御性的编程方式,这里在using语句结尾显示关闭了连接,并捕捉整个using语句的数据库异常: try{using (v
连接类
连接类有固定的使用模式,这是常用的乐观模式:
using (var conn = new SqlConnection(connstr)) { conn.Open(); //执行各种数据库操作 }悲观的、防御性的编程方式,这里在using语句结尾显示关闭了连接,并捕捉整个using语句的数据库异常:
try { using (var conn = new SqlConnection(connstr)) { conn.Open(); //执行各种数据库操作 conn.Close(); } } catch (SqlException e) { throw; } 事务当使用多条语句修改数据库是,应将所有这些语句看作一个单元,要么完全成功修改数据库(提交),香港空间,要么完全失败任何一条修改语句都不会被执行(回滚)。要想使用事务,先要引用System.Transactions装配体。事务的代码如下所示:
using (var scope = new TransactionScope()) { using (var conn = new SqlConnection(connstr)) { conn.Open(); // 执行各种数据库操作 } scope.Complete(); }在这里,事务范围使用了默认构造函数,它意味着TransactionScopeOption.Required、IsolationLevel.Serializable、TimeOut=1分钟。
事务通过使用scope.Complete显式标记为完成。缺少这个调用,事务将被回滚。
事务范围内可以有多个连接,都被看作一个整体。
事务的隔离级别:
ReadCommitted 读取已经提交的数据
ReadUncommitted 读取还没有提交的数据
RepeatableRead
Serializable
命令打开数据库连接之后,需要定义要执行的命令。对Sql Server可用的命令类型有两种:文本、和存储过程。
定义文本类型命令的方法如下。因为文本类型的命令是默认值,虚拟主机,所以不需要显式指定命令类型:
var sql = "SELECT ContactName FROM Customers"; var cmd = new SqlCommand(sql, conn);定义存储过程类型的命令:
var cmd = new SqlCommand("CustOrderHist", conn) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.AddWithValue("@CustomerID", "QUICK"); 执行命令定义了命令之后,需要执行它。根据返回结果不同分成三种执行方法。
ExecuteNonQuery方法一般用于UPDATE,INSERT,或DELETE语句。它执行命令,并返回受影响的行数。
打开连接后,添加如下代码:
var cmd = new SqlCommand("CustOrderHist", conn) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.AddWithValue("@CustomerID", "QUICK"); var rows = cmd.ExecuteNonQuery();ExecuteReader方法执行命令,并返回一个数据阅读器对象。在打开连接语句之后,添加如下代码:
var sql = "SELECT ContactName,CompanyName FROM Customers"; var cmd = new SqlCommand(sql, conn); var reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine("Contact: {0,-20} Company: {1}", reader[0], reader[1]); }ExecuteScalar执行返回一个标量值的命令。在打开连接语句之后,添加如下代码:
var sql = "SELECT COUNT(*) FROM Customers"; var cmd = new SqlCommand(sql, conn); var custs = (int)cmd.ExecuteScalar(); Console.WriteLine(custs);ExecuteScalar方法返回一个object对象,需要将其转化为实际的类型。
调用存储过程 调用不返回值的存储过程这里列出两个例子,使用了Northwind数据库的Region表。
更新记录存储过程定义如下:
CREATE PROCEDURE RegionUpdate (@RegionID INTEGER, @RegionDescription NCHAR(50)) AS SET NOCOUNT OFF UPDATE Region SET RegionDescription = @RegionDescription WHERE RegionID = @RegionID GO如前所述,定义一个命令,并添加他的参数和参数值:
var cmd = new SqlCommand("RegionUpdate", conn) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.AddWithValue("@RegionID", 23); cmd.Parameters.AddWithValue("@RegionDescription", "Something"); cmd.ExecuteNonQuery(); 删除记录存储过程定义如下:
CREATE PROCEDURE RegionDelete (@RegionID INTEGER) AS SET NOCOUNT OFF DELETE FROM Region WHERE RegionID = @RegionID GO在命令定义部分,使用SqlParameter构造函数来构造命令参数:
var cmd = new SqlCommand("RegionDelete", conn) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int, 0, "RegionID")); cmd.UpdatedRowSource = UpdateRowSource.None;构造完命令并添加参数定义之后,通过参数名字检索出参数并设置它的值,然后执行命令:
cmd.Parameters["@RegionID"].Value = 999; cmd.ExecuteNonQuery();也可以通过参数位置检索参数。
调用返回输出参数的存储过程下面的例子演示插入一个记录到数据库并且返回该记录的主键给调用者。
CREATE PROCEDURE RegionInsert(@RegionDescription NCHAR(50), @RegionID INTEGER OUTPUT)AS SET NOCOUNT OFF SELECT @RegionID = MAX(RegionID)+ 1 FROM Region INSERT INTO Region(RegionID, RegionDescription) VALUES(@RegionID, @RegionDescription) GO插入过程创造一个新的Region记录。因为主键由数据库自己生成,这值被作为一个输出参数从过程(@RegionID)返回。下面代码显示我们将如何调用RegionInsert存储过程:
var cmd = new SqlCommand("RegionInsert", conn) { CommandType = CommandType.StoredProcedure }; cmd.Parameters.Add(new SqlParameter("@RegionDescription", SqlDbType.NChar, 50, "RegionDescription")); cmd.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int, 0, ParameterDirection.Output, false, 0, 0, "RegionID", DataRowVersion.Default, null)); cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;第二参数,@RegionID,香港服务器,包括它的参数方向,在这个例子中是Output。代码的最后一行UpdateRowSource枚举被用来指明数据将从这个存储过程通过输出参数返回。

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



In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.
