如何设置"应用程序名称"在ADODB连接字符串 [英] How to set "Application Name" in ADODB connection string

查看:195
本文介绍了如何设置"应用程序名称"在ADODB连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中我只是使用应用程序名= MyApp的连接字符串中,但是使用通过VBA ADO连接时,SQL Server Management Studio中的活动监视器总是显示的Microsoft Office 2010 进程应用列不管是什么我的名字在VBA代码设置。





<预类=朗动prettyprint-覆盖> conn.ConnectionString =UID =&放大器; UID和放大器; ; PWD =&放大器; PWD&安培; DSN =&放大器; DSN&安培; _
;应用程序名称= MyApp的

如何监控设置应用程序的名称目的是什么?


解决方案

啊我看到VBA连接字符串不支持的 应用程序名 的属性。它只是内VBA中使用时不被识别。我能想到它是从一个COM C#库返回 ADODB.Connection 对象的瞬间解决这个的唯一途径。



您自己的COM库将返回一个ADODB.Connection对象,这似乎在.NET中工作预定义的连接字符串。你会被连接到使用VBA ADODB.Connection对象的数据库,但与取代的对象引用。而不是



设置CN =新ADODB.Connection 你会使用 GetConection() 方法,通过自己的库暴露出来。

 暗淡作为CN ADODB.Connection 
将CN = yourCOMlibrary.GetConnection



这里的步骤



下载并安装的的Visual Studio Express Windows版(的免费的)



打开它作为管理员并创建一个新项目。选择的 的Visual C# 的那么的 类库 的并将其重命名为的 MyConnection的





解决方案管理器的,重命名的 的Class1.cs 的到的 ServerConnection.cs





右键点击您的 MyConnection的 的项目在解决方案资源管理器的选择的 添加引用



键入的ActiveX 在搜索框中,并勾选 Microsoft ActiveX数据对象6.1库





复制下面的代码粘贴到的 ServerConnection.cs 的完全替换无论是文件的​​

 使用系统;使用System.Runtime.InteropServices 
;
:使用System.IO;
使用ADODB;

命名空间MyConnection的
{
[InterfaceType(ComInterfaceType.InterfaceIsDual),
的Guid(32A5A235-DA9F-47F0-B02C-9243315F55FD)]
公共接口INetConnection
{
连接的getConnection();
无效的Dispose();
}

[ClassInterface(ClassInterfaceType.None)
[的Guid(4E7C6DA2-2606-4100-97BB-AB11D85E54A3)]
公共类ServerConnection:INetConnection ,IDisposable的
{
私人连接CN;

私人字符串cnStr =供应商= SQLOLEDB;数据源= SERVER\\DB;​​初始目录= default_catalog;用户ID =用户名;密码=密码;应用程序名称= MyNetConnection;

公共连接的getConnection()
{
CN =新的Connection();
cn.ConnectionString = cnStr;
返回CN;
}

公共无效的Dispose()
{
CN = NULL;
GC.Collect的();
}
}
}



找到<在代码code> cnStr 变量和更新您的连接字符串的详细信息



请注意:如果您不确定你应该使用请参见所有连接字符串



连接字符串

点击工具Visual Studio和创建GUID



用自己替换的GUID和删除花括号让他们在相同的格式那些你现在看到从复制的代码





右键单击的 MyConnection的 的在解决方案资源管理器的并选择属性。



点击 应用 的标签左侧一面,那么的 大会信息 的并勾选使大会COM可见





点击 *构建* 从左侧菜单,勾选注册为COM Interop





请注意:如果要为64位Office开发然后确保你改变的 目标平台 的上构建的菜单 64 !这是强制性的64位Office COM库,以避免任何ActiveX相关的错误。






右键单击的 MyConnection的 的在解决方案资源管理器的选择的 构建 的从菜单中。



如果一切正常,然后你的 MyConnection.dll MyConnection的。 TLB 应成功生成。进入这条道路现在



  C:\Users\username\desktop\ 

或你救了他们



,你应该看到你的文件。








现在打开Excel,转到VBE。点击工具并选择参考



点击<大骨节病>浏览按钮并导航到 MyConnection.tlb



此外,添加引用< 。code>微软ActiveX对象6.1库 - 这是所以你可以使用ADODB库





现在右键单击在 Project Explorer中<随时随地/ EM>窗口的插入的一个新的 模块



复制和粘贴下面的代码把它

 选项显式

子的Main()

昏暗myNetConnection作为ServerConnection
将myNetConnection =新ServerConnection

昏暗的CN作为ADODB.Connection
将CN = myNetConnection.GetConnection

CN 。开

Application.Wait(现在+ TIMEVALUE(00:00:10))

cn.Close
将CN =什么

myNetConnection.Dispose

端子






打开SQL Server Management Studio中,右键单击该服务器,并选择活动监视器



< IMG SRC =htt​​p://i.stack.imgur.com/RDhoU.pngALT =在这里输入的形象描述>



不要关闭此窗口






返回到Excel,并按<大骨节病> F5 或打绿的播放的色带上的按钮。





现在转回到SSMS(SQL Server Management Studio中)



和等待您的自定义连接名字出现在! :)



在这里,我们走!这很容易,不是吗? :)








这是正在发生的事情。



您正在返回一个ADODB连接从你对象C#COM库使用 myNetConnection.GetConnection 函数

 暗淡myNetConnection作为ServerConnection 
将myNetConnection =新ServerConnection

昏暗的CN作为ADODB.Connection
将CN = myNetConnection.GetConnection

这几乎就像在说设置CN =新ADODB.Connection 但你在你的C#没有预定义的连接字符串代码。



您可以使用 CN 对象像中VBA现在正常ADODB.Connection对象。



记住,总是 .Close()的ADODB.Connection。一个好的程序员的做法是打开始终贴近东西 - 流,连接等。



您可以依靠垃圾收集器免费引用/内存,但我也写了的Dispose()方法你,让你可以强制GC运行。你可以做,立即摆脱连接,所以它不会的挂断的作为打开SSMS。



记住使用 myNetConnection.Dispose cn.Close ,你会没事的。


$ b $沿b

请注意:



这是如果任何人认为这是错误的或需要更新(作为是不稳定或不安全我会怎么做的),请发表评论。






好了,我希望这将有助于在未来的人:)


In .NET I simply use Application Name = MyApp inside the connection string, but when using ADO connection through VBA the Activity Monitor of the SQL Server Management Studio always shows Microsoft Office 2010 in Processes on the Application column no matter what name I set on the VBA code.

conn.ConnectionString = "UID=" & UID & ";PWD=" & PWD & ";DSN=" & DSN & _
    ";Application Name = MyApp"

How can I set the application name for monitoring purposes?

解决方案

Ahh I see VBA connection string doesn't support the Application Name attribute. It simply isn't being recognized when used within VBA. The only way I can think of solving this at the moment it's to return an ADODB.Connection object from a COM C# library.

Your own COM library would return an ADODB.Connection object with a predefined connection string which seem to work in .NET. You will be connecting to the database using a VBA ADODB.Connection object but with a substituted object reference. Instead of

Set cn = new ADODB.Connection you will use a GetConection() method exposed by your own library.

Dim cn as ADODB.Connection
Set cn = yourCOMlibrary.GetConnection

here are the steps

Download and install Visual Studio Express for Windows (FREE)

Open it as Administrator and create a New Project. Select Visual C# then Class Library and rename it to MyConnection

In the Solution Explorer, rename Class1.cs to ServerConnection.cs

Right click your MyConnection project in the Solution Explorer and select Add Reference

Type activeX in the search box and tick the Microsoft ActiveX Data Objects 6.1 Library

Copy and paste the below code into the ServerConnection.cs completely replacing whatever is in the file.

using System;
using System.Runtime.InteropServices;
using System.IO;
using ADODB;

namespace MyConnection
{
    [InterfaceType(ComInterfaceType.InterfaceIsDual),
    Guid("32A5A235-DA9F-47F0-B02C-9243315F55FD")]
    public interface INetConnection
    {
        Connection GetConnection();
        void Dispose();
    }

    [ClassInterface(ClassInterfaceType.None)]
    [Guid("4E7C6DA2-2606-4100-97BB-AB11D85E54A3")]
    public class ServerConnection : INetConnection, IDisposable
    {
        private Connection cn;

        private string cnStr = "Provider=SQLOLEDB; Data Source=SERVER\\DB; Initial Catalog=default_catalog; User ID=username; Password=password;Application Name=MyNetConnection";

        public Connection GetConnection()
        {
            cn = new Connection();
            cn.ConnectionString = cnStr;
            return cn;
        }

        public void Dispose()
        {
            cn = null;
            GC.Collect();
        }
    }
}

Locate the cnStr variable in the code and UPDATE your connection string details.

Note: if you are unsure about the connection string you should use see ALL CONNECTION STRINGS

Click on TOOLs in Visual Studio and CREATE GUID

Replace the GUIDs with your own and remove the curly braces so they are in the same format as the ones you see now from the copied code

Right click MyConnection in the Solution Explorer and select Properties.

Click the Application tab on the left side, then Assembly Info and tick Make Assembly COM-Visible

Click the *Build* from the menu on the left and tick Register For COM Interop

Note: If you are developing for 64-bit Office then make sure you change the Platform Target on the Build menu to x64! This is mandatory for 64-bit Office COM libraries to avoid any ActiveX related errors.


Right click MyConnection in the Solution Explorer and select Build from the menu.

If everything went OK then your MyConnection.dll and MyConnection.tlb should be successfully generated. Go to this path now

C:\Users\username\desktop\

or wherever you saved them

and you should see your files.


Now open Excel and go to VBE. Click Tools and select References.

Click the Browse button and navigate to the MyConnection.tlb.

Also, add references to Microsoft ActiveX Object 6.1 Library - this is so you can use ADODB library.

Now right click anywhere in the Project Explorer window and Insert a new Module

copy and paste the below code to it

Option Explicit

Sub Main()

    Dim myNetConnection As ServerConnection
    Set myNetConnection = New ServerConnection

    Dim cn As ADODB.Connection
    Set cn = myNetConnection.GetConnection

    cn.Open

    Application.Wait (Now + TimeValue("0:00:10"))

    cn.Close
    Set cn = Nothing

    myNetConnection.Dispose

End Sub


Open SQL Server Management Studio, right click the server and select Activity Monitor

dont close this window


Go back to Excel and hit F5 or hit the green play button on the ribbon.

now switch back to SSMS ( SQL Server Management Studio )

and wait for your custom connection name to appear! :)

Here we go! That was easy, wasn't it? :)


This is what is happening.

You are returning an ADODB Connection object from you C# COM library by using myNetConnection.GetConnection function

Dim myNetConnection As ServerConnection
Set myNetConnection = New ServerConnection

Dim cn As ADODB.Connection
Set cn = myNetConnection.GetConnection

It's almost like saying Set cn = new ADODB.Connection but with predefined connection string which you did in your C# code.

You can use the cn object like a normal ADODB.Connection object within VBA now.

Remember to always .Close() the ADODB.Connection. A good programmers practice is to always close anything you open - streams, connections, etc.

You can rely on the Garbage Collector to free references/ memory but I also wrote a Dispose() method for you so you can force the GC to run. You can do that to immediately get rid of the Connection so it does not hang in the SSMS as opened.

Remember to use myNetConnection.Dispose along with the cn.Close and you'll be fine.

Note:

This is how I would do it if any one thinks this is wrong or needs to be updates (as being unstable or unsafe) please leave a comment.


Well, I hope this will be helpful to anyone in the future :)

这篇关于如何设置&QUOT;应用程序名称&QUOT;在ADODB连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆