Search code examples
sqlarraysconverterssid

How to retrieve the sql server SID in string format?


Hello I break my head since this morning trying to retrieve the SID of my computer with SQl query. I can only retrieve the SID in Byte array!

Is it possible to retrieve a string with a cast? I tried but impossible.

Here is my sql query

SqlDataSource data = new SqlDataSource(
System.Configuration.ConfigurationManager.ConnectionStrings["myDbConnection"].ToString(), 
"Select sid FROM sys.server_principals where Name='##MS_SQLAuthenticatorCertificate##'");

DataView viewData = data.Select(DataSourceSelectArguments.Empty) as DataView;

//Get S.I.D in byte
byte[] tabSid = (byte[]) viewData[0][0] ;
//Get S.I.D in String format (not working)
string varSid = viewData[0][0].toString() ;

Thank's a lot


Solution

  • If you do this:

    SELECT master.dbo.fn_varbintohexstr(sid)  AS 'SID'
     FROM sys.server_principals where Name='##MS_SQLAuthenticatorCertificate##'
    

    Then the .ToString() will work correctly