简体   繁体   中英

How to query ADO.Net C# Connection Properties

I need to test connecting to an ADO.Net provider in a number of different ways to ensure that the correct connection settings are being set. Is there anyway to query the DBConnection object to ask what the user name of the authenticated user is?

I'm afraid that no generic approach within DbConnection or IDbConnection is powerful enough to achieve this. The closest thing that I can think is by reading the ConnectionString property and parsing it to get the username. For the parsing, you can utilize the DbConnectionStringBuilder , which is similar to a Dictionary, then you can get the value of spesific key (eg "User ID", "user", "uid"). Beware that some dbms using a totally different key for defining user in connection-string.

If you allowed to use dbms spesific class directly, for MS-SQL, you can parse the connection-string using SqlConnectionStringBuilder . Another specific approach is using SqlConnection.Credential that has UserId property.

The following example returns the name of the current user.

SELECT CURRENT_USER;
GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM