本文介绍了通过控制台应用程序使用Kentico 7 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过控制台应用程序使用Kentico API 7进行以下操作:

I have the following, using Kentico API 7 via a console application:

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);

The connection is open. I get error Console.WriteLine("CurrentSiteID " + CMS.CMSHelper.CMSContext.CurrentSiteID);

表示连接未初始化.得到帮助了吗?

that says connection is not initialized. Got help?

推荐答案

当然可以在Kentico本身之外使用Kentico API.最近,我发布了有关该主题的文章.但是,本文证明了在更新版本的Kentico上的可能性.但是回到您的问题...

It certainly is possible to use Kentico API outside of Kentico itself. Recently, I published an article on this topic. However, the article demonstrates the possibility on a newer version of Kentico. But back to your problem...

CMS.CMSHelper.CMSContext.Init();方法期望app.config或web.config中存在一个名为"CMSConnectionString"的连接字符串.

The CMS.CMSHelper.CMSContext.Init(); method expects a connection string named "CMSConnectionString" to exist in your app.config or web.config.

文档也说

,因此在调用CMSContext.Init()之前,请勿触摸CMS.DataEngine或任何其他CMS.*名称空间.调用该方法后,您可以开始使用无参数重载ConnectionHelper.GetConnection(),但我建议您利用信息提供者模式(由Kentico提供),而不是通过CMS.DataEngine使用直接的数据库访问.

so you should not be touching CMS.DataEngine or any other CMS.* namespace before you call CMSContext.Init(). Once you call that method you can start using the parameter-less overload ConnectionHelper.GetConnection() but I would advise you to take advantage of the Info-Provider pattern that Kentico offers instead of using the direct DB access through CMS.DataEngine.

例如,这就是您删除用户:

// Get the user
UserInfo deleteUser = UserInfoProvider.GetUserInfo("MyNewUser");

// Delete the user
UserInfoProvider.DeleteUser(deleteUser);

这篇关于通过控制台应用程序使用Kentico 7 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!