Pages

12 March, 2012

How to access Sitecore Database in Code Behind (Sitecore v6.5) - 2

There are three methods to work with Sitecore Database. To obtain it, you have to use Factory class Sitecore.Configuration.Factory 

GetDatabase("Database Name"): Get Sitecore Database by Name 
Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase("DBName");
 You need to pass Database name as a parameter and it will return Sitecore Database Object.
 Note: “DBName” should match with ID of any /Configuration/Sitecore/Databases/Database and it is case sensitive. 

GetDatabaseNames(): Get all the Sitecore Database names 
string[] databases = Sitecore.Configuration.Factory.GetDatabaseNames(); 
GetDatabaseNames method returns a string array with list of Database Names added in /Configuration/Sitecore/Databases/Database. 

GetDatabases(): Get all the Sitecore Databases Object in a List
List<Sitecore.Data.Database> databases = Sitecore.Configuration.Factory.GetDatabases();
GetDatabases will return a list with all the Sitecore Database Objects configured in /Configuration/Sitecore/Databases/Database.
Note: Add Sitecore.Kernel.dll reference to project.

No comments:

Post a Comment

blockquote { margin: 0; } blockquote p { padding: 15px; background: #eee; border-radius: 5px; } blockquote p::before { content: '\201C'; } blockquote p::after { content: '\201D'; }