使用MultipleActiveResultSets复用Sql Server 2008数据库连接

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tjvictor/article/details/4202535

 MultipleActiveResultSets可以使数据库连接复用。这样就不怕数据库的连接资源被耗尽了。使用方法很简单,只需要把它加到数据的连接字符串中即可。

例如:server=(local);Integrated Security = true;database=AdventureWorks;MultipleActiveResultSets=true;

测试用例:

编译后,打开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数据库。

这就是数据库连接复用的好处了。

关于数据库连接字符串的其它用法,参见:

http://blog.csdn.net/tjvictor/archive/2009/03/19/4004277.aspx

展开阅读全文

SQL Server数据库连接问题

07-30

我做了一个SQL Server数据库连接的DLL,编译通过生成DLL文件,在.exe工程文件调用时出错,大概是溢出,但我直接做成函数调用是可以的,请问是什么意思?错误代码:rnProject Project1.exe raised exception class EAccessViolation with message'Access violation at address 004118cc in module 'DATABASEDLL.DLL'.Read of address FFFFFFFF'.Process stopped.Use Step or Run to continue.rnDLL文件:rn#include rn#include rn#include "FormUnit.h"//Form2窗口rn#pragma hdrstoprnextern "C" __declspec(dllexport) void LinkDatabase(WideString Pwd,WideString UserID,WideString Database,WideString Server); //数据库连接rn#pragma argsusedrnint WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)rnrn return 1;rnrn//---------------------------------------------------------------------------rn/*rnUserID:登录SQL Server用户名, Database:SQL Server数据库名rnServer:SQL Server数据库所在的服务器名称 Pwd:登录数据库密码rn*/rnvoid LinkDatabase(WideString Pwd,WideString UserID,WideString Database,WideString Server)rn WideString Link="Provider=SQLOLEDB.1;Password="+Pwd+";Persist Security Info=False;User ID="+UserID+";Initial Catalog="+Database+";Data Source="+Server;rn if(Form2->ADOConnection1->Connected==false) //如果没有连接到SQL Serverrn tryrn rn Form2->ADOConnection1->ConnectionString=Link;rn Form2->ADOConnection1->LoginPrompt=true;rn Form2->ADOConnection1->Connected=true;rn rn catch(...)rn rn ShowMessage("数据库连接失败,请联系管理员");rn rn rnrn//--------------------------------------------------------------------------------rn静态调用:rnextern "C" __declspec(dllexport) void LinkDatabase(WideString Pwd,WideString UserID,WideString Database,WideString Server);//声明rnvoid __fastcall TForm1::FormCreate(TObject *Sender)rnrn WideString Pwd="",UserID="sa",Database="test",Server="my1";rn LinkDatabase(Pwd,UserID,Database,Server);rnrn//---------------------------------------------------------------------------rn 论坛

SQL Server数据库连接的问题

11-07

我现在装的WINDOWS XP,前一段时间我在网上自动升级时系统显示了一个 SQL server数据库系统java的连接 现在我用java的jdbc不能连接SQL Server数据库了,这是是为什么呢rn错误信息:rn创建数据库连接失败!! url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=web ,username=web ,password=web-错误信息:[Microsoft][SQLServer JDBC Driver]Error establishing socket.rnrnjava.sql.SQLException: [Microsoft][SQLServer JDBC Driver]Error establishing socket.rnrn at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)rnrn at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)rnrn at com.microsoft.jdbc.sqlserver.tds.TDSConnection.(Unknown Source)rnrn at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)rnrn at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)rnrn at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)rnrn at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)rnrn at java.sql.DriverManager.getConnection(DriverManager.java:512)rnrn at java.sql.DriverManager.getConnection(DriverManager.java:171)rnrn at com.ld.dataset.ConnPool.createConnection(ConnPool.java:118)rnrn at com.ld.dataset.ConnPool.getConnection(ConnPool.java:144)rnrn at com.ld.dataset.ConnPool.getConnection(ConnPool.java:164)rnrn at com.ld.dataset.ResultSet.(ResultSet.java:35)rnrn at webdeveloper.dataset.SResultSetEx.(SResultSetEx.java:24)rnrn at webdeveloper.appframe.TreeInfo.(TreeInfo.java:21)rnrn at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)rnrn at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)rnrn at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)rnrn at java.lang.reflect.Constructor.newInstance(Constructor.java:274)rnrn at java.lang.Class.newInstance0(Class.java:308)rnrn at java.lang.Class.newInstance(Class.java:261)rnrn at java.beans.Beans.instantiate(Beans.java:204)rnrn at java.beans.Beans.instantiate(Beans.java:48)rnrn at org.apache.jsp.CreateAppFrame_jsp._jspService(CreateAppFrame_jsp.java:87)rnrn at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)rnrn at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rnrn at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)rnrn at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)rnrn at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)rnrn at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rnrn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)rnrn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)rnrn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)rnrn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)rnrn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)rnrn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)rnrn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rnrn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)rnrn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)rnrn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)rnrn at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)rnrn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)rnrn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)rnrn at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)rnrn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)rnrn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)rnrn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)rnrn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)rnrn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)rnrn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)rnrn at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)rnrn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)rnrn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)rnrn at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)rnrn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)rnrn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)rnrn at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)rnrn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)rnrn at java.lang.Thread.run(Thread.java:534)rnrn 论坛

sql server 2005 数据库连接……

03-03

我用的是VS2005,运行下面的代码: rnrnImports System rnImports System.Data rnImports System.Data.SqlClient rnImports System.Configuration rnrnPublic Class frmDisplayAuthor rnrnPublic Sub New() rnrn' 此调用是 Windows 窗体设计器所必需的。 rnInitializeComponent() rnrn' 在 InitializeComponent() 调用之后添加任何初始化。 rnLoadAuthors() rnEnd Sub rnrnPrivate Sub LoadAuthors() rnDim sConnectionString As String = "Data Source=ramanujan;" & "Initial Catalog=Pubs;" & "User ID=lixing;password=lixing" rnDim connPubs As New SqlConnection(sConnectionString) rnDim cmdAuthors As New SqlCommand("Select au_fname + ' ' + au_lname as Name from Authors", connPubs) rnDim daAuthors As New SqlDataAdapter(cmdAuthors) rnDim dsAuthors As New DataSet("Authors") rnrnconnPubs.Open() rndaAuthors.Fill(dsAuthors, "Authors") rnrnlbAuthors.DataSource = dsAuthors.Tables(0).DefaultView rnlbAuthors.DisplayMember = "Name" rnEnd Sub rnEnd Class rnrn出现如下错误: rnrn未处理 System.InvalidOperationException rnMessage="创建窗体时出错。有关详细信息,请参阅 Exception.InnerException。错误为: 在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)" rnSource="DisplayAuthor" rnStackTrace: rn在 DisplayAuthor.My.MyProject.MyForms.Create__Instance__[T](T Instance) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 190 rn在 DisplayAuthor.My.MyProject.MyForms.get_frmDisplayAuthor() rn在 DisplayAuthor.My.MyApplication.OnCreateMainForm() 位置 E:\lixing\Sams Teach Yourself Visual Basic.NET in 21 Days\myexercise\chapter19\CommonDemo\DisplayAuthor\My Project\Application.Designer.vb:行号 35 rn在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() rn在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() rn在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) rn在 DisplayAuthor.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81 rn在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args) rn在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) rn在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() rn在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) rn在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) rn在 System.Threading.ThreadHelper.ThreadStart() rnrn是什么原因?谢谢!急!!! 论坛

没有更多推荐了,返回首页