SQL Server > SQL Server Forums > .NET Framework inside SQL Server > Global Scope for Database CLR Variables
Ask a questionAsk a question
 

AnswerGlobal Scope for Database CLR Variables

  • Friday, November 13, 2009 1:35 AMr.muralidhar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I would like to know if we can store variables (from CLR Functions) on global scope; something which is comparable to Application Scope in APS.Net which lets us access variables across processes and calls. I am planning to keep a dictionary object on the global scope. Need to know if this is possible.


    Regards- Muralidhar | If my response helped, please help me by marking my response as the answer and voting as appropriate...

Answers

  • Tuesday, November 17, 2009 6:29 PMBob BeaucheminMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Since you are in a database, why not store your state in a database table? A dictionary object is just a table with primary key.

    You can use static variables, but since SQLCLR frowns on you using your own locking (for reliability's sake), if the static variable is mutable, the assembly must be cataloged as unsafe. And the appdomain may be removed. If you can initialize your static variable in the class constructor (and therefore mark it readonly) you can catalog the assembly as safe.

    Hope this helps,
    Bob Beauchemin
    SQLskills

All Replies

  • Tuesday, November 17, 2009 6:29 PMBob BeaucheminMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Since you are in a database, why not store your state in a database table? A dictionary object is just a table with primary key.

    You can use static variables, but since SQLCLR frowns on you using your own locking (for reliability's sake), if the static variable is mutable, the assembly must be cataloged as unsafe. And the appdomain may be removed. If you can initialize your static variable in the class constructor (and therefore mark it readonly) you can catalog the assembly as safe.

    Hope this helps,
    Bob Beauchemin
    SQLskills