简体   繁体   English

在2sxc剃刀模板中运行SQL查询

[英]Running SQL queries in 2sxc razor templates

I use 2sxc for reasearch projects, and statistics is the most important part of the work I do. 我将2sxc用于reasearch项目,而统计是我所做工作中最重要的部分。 With link and visual queries I can do 90% of the work with no hassle at all. 使用链接和可视化查询,我可以轻松完成90%的工作。 However, there are times where I need the optimized database table (with a field - value structure), for example to do a search over multiple columns on a single run (counting the number of "yes" in all questions for example). 但是,有时候我需要优化的数据库表(具有字段-值结构),例如一次运行对多个列进行搜索(例如,计算所有问题中的“是”数)。 With a little fiddle I can get this type of data with the following query: 进行一些操作,我可以通过以下查询获取此类数据:

SELECT dbo.ToSIC_EAV_Attributes.StaticName, dbo.ToSIC_EAV_Values.Value
FROM dbo.ToSIC_EAV_Values
LEFT JOIN dbo.ToSIC_EAV_Attributes
ON dbo.ToSIC_EAV_Values.AttributeID = dbo.ToSIC_EAV_Attributes.AttributeID
LEFT JOIN dbo.ToSIC_EAV_AttributesInSets
ON dbo.ToSIC_EAV_Values.AttributeID = dbo.ToSIC_EAV_AttributesInSets.AttributeID
WHERE dbo.ToSIC_EAV_AttributesInSets.AttributeSetID = <my entity SetID>;

What I don't have is the knowledge to implement this on a view code in 2sxc. 我所没有的是在2sxc中的视图代码上实现此功能的知识。

How can I output the table that this query outputs? 如何输出此查询输出的表?

And if I add a simple SELECT COUNT how can I place the result in ac# var? 如果我添加一个简单的SELECT COUNT,如何将结果放入ac#var?

So the core question is if you want to use visual query or if you want to use c# code in your view. 因此,核心问题是您是否要使用视觉查询,还是要在视图中使用c#代码。 I recommend visual query, but otherwise it's basically standard microsoft DB objects in c# (nothing related to 2sxc). 我建议使用可视查询,但否则它基本上是c#中的标准Microsoft DB对象(与2sxc没有任何关系)。

You can find some examples here https://2sxc.org/en/Docs/Feature/feature/2579 especially the Code Example with DataTable (no DataSource) or the DataReader example. 您可以在这里https://2sxc.org/en/Docs/Feature/feature/2579中找到一些示例,尤其是带有DataTable代码示例(无DataSource)DataReader示例。 It's a bit like this: 有点像这样:

@using System.Configuration

@using System.Data.SqlClient

@functions{

   private SqlDataReader myReader;



   // Official place to provide data preparation. Is automatically called by 2SexyContent

   public override void CustomizeData()

   {

         var conString = ConfigurationManager.ConnectionStrings[Content.ConnectionName].ToString();

         var con = new SqlConnection(conString);

         con.Open();

         var command = new SqlCommand("Select Top 10 * from Files Where PortalId = @PortalId", con);

         command.Parameters.Add("@PortalId", Dnn.Portal.PortalId);

         myReader = command.ExecuteReader();

   }

}

<div class="sc-element">

   @Content.Toolbar

   <h1>Simple Demo using DataReader access</h1>

   <p>This demo accesses the data directly, uses a SQL parameter for the PortalId then shows the first 10 files it finds. More intro-material for direct database access in this <a href="http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C" target="_blank">article on codeplex</a>.</p>

   <h2>The top 10 files found in this portal</h2>

   <ol>

         @while (myReader.Read())

         {

                <li>@myReader["FileName"]</li>

         }

   </ol>

   @{

         myReader.Close();

   }

</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM