MultipleActiveResultSets=true 数据库连接复用
注:EF连接Mysql时,连接字符串是不能包含MARS的,Mysql不支持这个特性。
ADO.NET 1.n 利用SqlDataReader读取数据,针对每个结果集需要一个独立的连接。
这些独立的链接也要占用相应的内存,在应用程序运行中还会形成高度拥挤的瓶颈效应。
ADO.NET 2.的一个新特征多数据结果集(Multiple Active Result Sets,简称MARS)
它允许在单个连接上执行多重的数据库查询或存储过程。目前只适用于Sql Server 2005
在一个Command对象上同时打开多个DataReader,即可以在sqldatareader里面嵌套sqldatareader,
如果不用MultipleActiveResultSets ,则一般报错为sqldatareader未关闭,即需要关闭了之后才能打开另一个
使用方法:在数据库链接字符串后面加上MultipleActiveResultSets=true
例如:string connstr = "server=(local);database=northwind;integrated security=true;MultipleActiveResultSets=true";
MultipleActiveResultSets可以使数据库连接复用。这样就不怕数据库的连接资源被耗尽了。使用方法很简单,只需要把它加到数据的连接字符串中即可。
例如:server=(local);Integrated Security = true;database=AdventureWorks;MultipleActiveResultSets=true;
using System;
using System.Threading;
using System.Data.SqlClient;
using System.Configuration; namespace ConsoleApplication1
{
public class Example
{
public static void Main()
{
SqlConnection sql1 = new SqlConnection("server=(local);Integrated Security = true;database=AdventureWorks;");
sql1.Open();
SqlCommand comm1 = new SqlCommand();
comm1.CommandText = "select 1";
comm1.CommandType = System.Data.CommandType.Text;
comm1.Connection = sql1;
comm1.ExecuteNonQuery();
sql1.Close();
Console.ReadLine();
}
}
}
编译后,打开bin/debug/ConsoleApplication1.exe。
在SQL Server 2008 Management Studio中打开一个新窗口,输入sp_who
按F5执行,可以发现已经有一个用户连接到AdventureWorks数据库了。
再打开一个ConsoleApplication1.exe,发现又会多一个用户连接到AdventureWorks数据库。
现在把程序的连接字符串改为server=(local);Integrated Security = true;database=AdventureWorks;MultipleActiveResultSets=true;
按上面的顺序执行,发现不管打开多少个ConsoleApplication1.exe,数据库中没有用户或只有一个用户连接着AdventureWorks数据库。
这就是数据库连接复用的好处了。
MultipleActiveResultSets=true 数据库连接复用的更多相关文章
- EntityFramework 连接字符串
1. Microsoft SQL Server 2016 LocalDB <connectionStrings> <add name="DefaultConnection& ...
- 知方可补不足~SqlServer连接的复用MultipleActiveResultSets
回到目录 MultipleActiveResultSets可以使数据库连接复用,但当你上了moebius这种集群工具后,这个选项不能开启(默认是false),当你使用EF等ORM工具时,这个选项会默认 ...
- .net 和 core 数据库连接字符串
Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-xxxx.mdf;Initial Catalog= ...
- Entity Framework Code First数据库连接
1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器->程序包管理器控制台,执行以下语句: PM> Insta ...
- web.config connectionStrings 数据库连接字符串的解释(转载)
先来看一下默认的连接SQL Server数据库配置 1.默认生成 <connectionStrings> <add name="Exa*DB" connectio ...
- EF--Codefirst 加密数据库连接字符串
http://www.tuicool.com/articles/QvYbEn 一.EF,CodeFirst加密SQL连接符 public LifeHelpContext() : base(" ...
- C# 根据ADO.NET数据库连接字符串构建EntityFrame数据库连接字符串
为了保持开发效率,以及保持代码优雅,项目中引用了EntityFrame.但是又因为某些报表功能需要大量计算,所以又要求直接使用ADO.NET,调用存储过程进行计算. 于是乎webconfig文件中就会 ...
- 【EF 译文系列】模型和数据库连接
原文链接:Connections and Models 本篇文章主要包括 Entity Framework 是如何选择数据库进行连接,以及我们如何去改变它的连接.无论是通过 Code First 还 ...
- ASP.NET MVC4 数据库连接(EF6.0)
我的博客原文地址:http://www.star110.com/Note/ReadArticle/60641215331146140041.html 环境:.NET MVC4 + EF6.0 连接数据 ...
随机推荐
- angular.js测试框架protracotr安装所需的node版本
protractor内代码的语法是基于ES6的,比如:里面用到了展开运算符“...”,node.js 6.0以下是不支持该语法特性. 所以,安装protractor是不会报错,但运行webdriver ...
- vim学习日志(7):替换、删除文件中的字符
vim全局替换文件: 语法为 :[addr]s/源字符串/目的字符串/[option] 全局替换命令为::%s/源字符串/目的字符串/g [addr] 表示检索范围,省略时表示当前行. 如:“1,20 ...
- tf.squared_difference
tf.squared_difference squared_difference( x, y, name=None ) 功能说明: 计算张量 x.y 对应元素差平方 参数列表: 参数名 必选 类型 说 ...
- WebSphere ILog JRules 域的介绍和定制
WebSphere ILog JRules 域的介绍和定制 引言 随着企业业务的不断发展,越来越多的企业正经历着以下的情形: 企业需要对于业务系统的频繁变化做出及时的关注和响应,例如,竞争对手或经济环 ...
- JQUERY根据值将input控件选中!
<select>: $('#country').find("option[value = " + data.country + "]").attr( ...
- centosmini7安装GNOME
1.在命令行下输入下面的命令来安装 Gnome 包 #:$sudo yum groupinstall "GNOME Desktop" "Graphical Admini ...
- net-snmp 的配置
http://www.cnblogs.com/oloroso/p/4595123.html
- 【oneday_onepage】—— 美国人的仪容整洁与个人卫生
There used to be an old joke in America that people should take a bath once a week, whether they nee ...
- 实现JTextfield 的右键 复制、剪切、粘贴功能。
http://leagion.iteye.com/blog/572730 ——————————————————————————————————————————————————————————————— ...
- PHP中“==”运算符的安全问题
前言 PHP是一种通用的开源脚本语言,它的语法混合了C,Java,以及Perl等优秀语言的语法.除此之外,它还提供了大量的函数库可供开发人员使用.但是,如果使用不当,PHP也会给应用程序带来非常大的安 ...