get user id from sql connection+C#+window application

Posted on 2011-10-08
Last Modified: 2012-05-12
Hi All,
i get the sql connection from an ocx object.
how i can get the user ID from sql connection.

Question by:MKItani
    LVL 11

    Expert Comment

    If you already have the connection object (SqlConnection) then you can pas the connection string into a SqlConnectionBuilder to get the User ID that is being used to connect
    SqlConnection conn = GetYourConnetion();
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(conn.ConnectionString);
    string userID = builder.UserID;

    Open in new window


    Author Comment

    this is my connectionString:
    <add name="ReportManagerLibrary"
                      connectionString="Data Source=mkitani;Initial Catalog=Test;Integrated Security=false;User ID=testUser;Password=123; TimeOut=20;"
                      providerName="System.Data.SqlClient" />

    but always when i am using "builder.Password" i get an empty string Why????
    LVL 11

    Expert Comment

    This is documented behavior for the Password property.

    If the value has not been set and the developer tries to retrieve the property, the return value is String.Empty.
    This is obviously for security reasons, if you pull a connection string into the builder the password is not visible unless you set it manually.
    LVL 14

    Expert Comment

    by:nishant joshi

    Author Comment

    so in case the integrated Security in the connection string is false always i get an empty string when i used SqlConnectionStringBuilder.Password
    LVL 11

    Accepted Solution

    Nope, you will always get an empty string for SqlConnectionStringBuilder.Password unless you've set it yourself. Check my previous comment above.

    Integrated security is an option to use your Windows account to login instead of providing a password in the connection string.

    I thought you wanted to get the User ID which you can easily get using the connection string builder? If you want to get the password you're going to have to parse the connection string manually by splitting it and looking for 'Password'

    Warranty: You should probably do a lot more checks in this code to make sure it doesn't fall over when it can't find the password, this is just an example.
    using System.Linq;
    string connectionString = "Data Source=mkitani;Initial Catalog=Test;Integrated Security=false;User ID=testUser;Password=123; TimeOut=20;";
    string passwordSection = connectionString.Split(';').First(s => s.Trim().StartsWith("password", StringComparison.OrdinalIgnoreCase));
    string password = password = passwordSection.Split('=')[1];  // Second half of the =

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now