通过控制台应用程序使用 Kentico 7 API
Using Kentico 7 API via Console App
我有以下内容,通过控制台应用程序使用 Kentico API 7:
String connectionString = CMS.DataEngine.ConnectionHelper.GetConnectionString("MyConnString");
Console.WriteLine("connectionString ? " + connectionString);
//CMS.DataEngine.GeneralConnection
CMS.DataEngine.GeneralConnection conn = CMS.DataEngine.ConnectionHelper.GetConnection(connectionString);
conn.Open();
Console.WriteLine("connection is open? " + conn.IsOpen());
CMS.CMSHelper.CMSContext.Init();
Console.WriteLine("CurrentSiteID " + CMS.CMSHelper.CMSContext.CurrentSiteID);
连接已打开。我收到错误
Console.WriteLine("CurrentSiteID " + CMS.CMSHelper.CMSContext.CurrentSiteID);
表示连接未初始化。有帮助吗?
除非特别需要创建控制台应用程序来执行您的任务,否则我建议避免使用控制台应用程序,而是创建自定义 scheduled task。
您可以在项目的 App_Code 文件夹中编写您的任务需要执行的代码 - 如果您使用的是 Web 应用程序项目类型,则可以在 CMSAppCode 项目中编写。这样您就不必担心访问数据库或引用您需要使用 Kentico API.
的所有 DLL。
当然可以在 Kentico 本身之外使用 Kentico API。最近,我发表了一篇关于这个主题的文章 article。然而,这篇文章展示了在更新版本的 Kentico 上的可能性。但是回到你的问题...
CMS.CMSHelper.CMSContext.Init();
方法需要一个名为 "CMSConnectionString" 的连接字符串存在于您的 app.config 或 web.config.
中
You can call this method at any point in your application's life cycle, but it must occur before you use any other Kentico CMS API.
因此您在调用 CMSContext.Init()
之前不应接触 CMS.DataEngine
或任何其他 CMS.*
命名空间。
调用该方法后,您可以开始使用 parameter-less 重载 ConnectionHelper.GetConnection()
但我建议您利用 Kentico 提供的 Info-Provider pattern 而不是通过 [=12 使用直接数据库访问=].
例如,这就是你 delete users:
// Get the user
UserInfo deleteUser = UserInfoProvider.GetUserInfo("MyNewUser");
// Delete the user
UserInfoProvider.DeleteUser(deleteUser);
我有以下内容,通过控制台应用程序使用 Kentico API 7:
String connectionString = CMS.DataEngine.ConnectionHelper.GetConnectionString("MyConnString");
Console.WriteLine("connectionString ? " + connectionString);
//CMS.DataEngine.GeneralConnection
CMS.DataEngine.GeneralConnection conn = CMS.DataEngine.ConnectionHelper.GetConnection(connectionString);
conn.Open();
Console.WriteLine("connection is open? " + conn.IsOpen());
CMS.CMSHelper.CMSContext.Init();
Console.WriteLine("CurrentSiteID " + CMS.CMSHelper.CMSContext.CurrentSiteID);
连接已打开。我收到错误
Console.WriteLine("CurrentSiteID " + CMS.CMSHelper.CMSContext.CurrentSiteID);
表示连接未初始化。有帮助吗?
除非特别需要创建控制台应用程序来执行您的任务,否则我建议避免使用控制台应用程序,而是创建自定义 scheduled task。
您可以在项目的 App_Code 文件夹中编写您的任务需要执行的代码 - 如果您使用的是 Web 应用程序项目类型,则可以在 CMSAppCode 项目中编写。这样您就不必担心访问数据库或引用您需要使用 Kentico API.
的所有 DLL。当然可以在 Kentico 本身之外使用 Kentico API。最近,我发表了一篇关于这个主题的文章 article。然而,这篇文章展示了在更新版本的 Kentico 上的可能性。但是回到你的问题...
CMS.CMSHelper.CMSContext.Init();
方法需要一个名为 "CMSConnectionString" 的连接字符串存在于您的 app.config 或 web.config.
You can call this method at any point in your application's life cycle, but it must occur before you use any other Kentico CMS API.
因此您在调用 CMSContext.Init()
之前不应接触 CMS.DataEngine
或任何其他 CMS.*
命名空间。
调用该方法后,您可以开始使用 parameter-less 重载 ConnectionHelper.GetConnection()
但我建议您利用 Kentico 提供的 Info-Provider pattern 而不是通过 [=12 使用直接数据库访问=].
例如,这就是你 delete users:
// Get the user
UserInfo deleteUser = UserInfoProvider.GetUserInfo("MyNewUser");
// Delete the user
UserInfoProvider.DeleteUser(deleteUser);