Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Orchard CMS连接外部SQL Server_Sql Server_Content Management System_Orchardcms_External - Fatal编程技术网

Sql server Orchard CMS连接外部SQL Server

Sql server Orchard CMS连接外部SQL Server,sql-server,content-management-system,orchardcms,external,Sql Server,Content Management System,Orchardcms,External,我计划创建一些页面,显示来自Orchard CMS外部SQL Server的数据。看起来我需要编写一个新模块来实现这个功能。是否有任何示例或想法来实现此要求?是的,您需要编写一个新模块,该模块提供新的内容部分和内容部分驱动程序。该驱动程序将负责从外部SQL Server获取数据,您将外部SQL Server设置为从驱动程序返回的形状的属性。然后形状的视图将显示您的数据 本教程将指导您如何编写自定义内容零件: 执行此操作时,请确保不创建内容部分记录类型,因为您不会从Orchard数据库存储和加载数

我计划创建一些页面,显示来自Orchard CMS外部SQL Server的数据。看起来我需要编写一个新模块来实现这个功能。是否有任何示例或想法来实现此要求?

是的,您需要编写一个新模块,该模块提供新的内容部分和内容部分驱动程序。该驱动程序将负责从外部SQL Server获取数据,您将外部SQL Server设置为从驱动程序返回的形状的属性。然后形状的视图将显示您的数据

本教程将指导您如何编写自定义内容零件:

执行此操作时,请确保创建内容部分记录类型,因为您不会从Orchard数据库存储和加载数据-您希望从外部数据库加载数据。以下是您应该遵循的步骤:

  • 创建一个新模块
  • 创建内容零件类
  • 让您的部件继承自
    ContentPart
    而不是
    ContentPart
    ,因为不会有任何“TRecord”
  • 创建内容部件驱动程序
  • Display
    方法中,通过调用
    ContentShape
    方法返回形状确保在lambda中添加SQL数据访问逻辑。如果您在lambda之外执行此操作,则每次调用使用内容部件的内容项时都会调用该数据访问代码。虽然这听起来似乎正是您想要的,但这里有一个微妙之处,涉及Placement.info,您可以使用它来确定何时实际渲染形状。如果放置逻辑确定不应渲染形状,则您不希望免费访问外部数据
  • 创建Placement.info文件以配置形状的放置(在要渲染的内容项的上下文中)
  • 为步骤3.2中返回的形状创建Razor视图
  • 创建一个Migrations类,该类将定义自定义内容部件,以及要将部件添加到其中的任何内容类型。有关如何创建迁移的更多信息,请参阅
  • 另外,与直接在驱动程序中实现数据访问代码不同,我建议您在单独的类中实现。因为你知道,分离关注点等等。然后,您可以将该服务注入到您的驱动程序中。要在服务容器中注册服务类,请确保为其定义了一个接口,该接口本身派生自
    IDependency

    一些示例伪代码:

    服务代码:

    public interface IMyExternalDataStore : IDependency {
       IList<MyExternalDataRecord> GetMyData();
    }
    
    public class MyExternalDataStore : IMyExternalDataStore {
       public IList<MyExternalDataRecord> GetMyData() {
          // Connect to your SQL Server database, perhaps using EF, load the data and return it. Could of course also be simply a DataSet.
       }
    }
    
    内容部分驱动程序:

    public class MyExternalDataPartDriver : ContentPartDriver<MyExternalContentPart> {
    
       private readonly IMyExternalDataStore _dataStore;
    
       public MyExternalDataPartDriver(IMyExternalDataStore dataStore) {
          _dataStore = dataStore;
       }
    
       protected override DriverResult Display(SlideShowProPart part, string displayType, dynamic shapeHelper) {
    
           return ContentShape("Parts_MyExternalData", () => {
              // Notice that we're performing the data access here within the lambda (the so called "shape factory method").
              var data = _dataStore.GetMyData();
    
              // Notice that I'm creating a property called "MyData"on the shape (which is a dynamic object).
              return shapeHelper.Parts_MyExternalData(MyData: data));
           }
       }
    }
    

    谢谢你,西普。很好的向导,试着跟上你的逻辑。没问题!如果您需要有关特定零件的更多详细信息,请告诉我。
    public class MyExternalDataPartDriver : ContentPartDriver<MyExternalContentPart> {
    
       private readonly IMyExternalDataStore _dataStore;
    
       public MyExternalDataPartDriver(IMyExternalDataStore dataStore) {
          _dataStore = dataStore;
       }
    
       protected override DriverResult Display(SlideShowProPart part, string displayType, dynamic shapeHelper) {
    
           return ContentShape("Parts_MyExternalData", () => {
              // Notice that we're performing the data access here within the lambda (the so called "shape factory method").
              var data = _dataStore.GetMyData();
    
              // Notice that I'm creating a property called "MyData"on the shape (which is a dynamic object).
              return shapeHelper.Parts_MyExternalData(MyData: data));
           }
       }
    }
    
    @{
       var records = (IList<MyExternalDataRecord>)Model.MyData;
    }
    <ul>
    @foreach(var record in records) {
       <li>@record.ToString()</li>
    }
    </ul>
    
    <Placement>
       <Place Parts_MyExternalData="Content:0"/>
    </Placement>
    
    public class Migrations : DataMigrationImpl {
        public int Create() {
    
           // Define your content part so that you can attach it to any content type from the UI.
           ContentDefinitionManager.AlterPartDefinition("MyExternalDataPart", part => part.Attachable());
    
           // Optionally, define a new content type here programmatically or attach it to an existing type.
    
           return 1;
        }
    }