简体   繁体   English

将SQL查询和变量添加到SharePoint ASPX页面

[英]Add SQL Query And Variable to SharePoint ASPX Page

I have created an aspx page on a sharepoint 2013 site. 我在sharepoint 2013网站上创建了一个aspx页面。 I have customized the pages css, buttons, and layout exactly as I need. 我已根据需要自定义了页面css,按钮和布局。 Now my goal is to add a SQL connection to the database on our network to pull data based on currently logged in user. 现在我的目标是在我们的网络上的数据库中添加一个SQL连接,以根据当前登录的用户提取数据。 I have written the SQL query to basically be "Select "field" From DB Where SharePointUserloggedinname like 'db.table.field' . I have tested this query in sql management studio, and it works perfectly. My question, is how do I add this sql query portion into my aspx page, set the query result to a variable and then display the queries results(variable) to display via a text field/paragraph tag/or other. 我已经编写了SQL查询,基本上是“选择”字段“来自DB,其中SharePointUserloggedinname就像'db.table.field'。我已经在sql management studio中测试了这个查询,它运行得很完美。我的问题是,我该如何添加将此SQL查询部分放入我的aspx页面,将查询结果设置为变量,然后显示查询结果(变量)以通过文本字段/段落标记/或其他显示。

I have added the namespaces, and c# portion provided below into my aspx page but I am unsure how & where to place the c# code to connect to the sql db, set the variable, and then call that variable to display in a field further down the page. 我已将下面提供的名称空间和c#部分添加到我的aspx页面中,但我不确定如何以及在何处放置c#代码以连接到sql db,设置变量,然后调用该变量以显示在字段中这一页。

` `

<%@ Import Namespace="System;"%>
<%@ Import Namespace="System.Collections.Generic;"%>
<%@ Import Namespace="System.ComponentModel;"%>
<%@ Import Namespace="System.Data;"%>
<%@ Import Namespace="System.Drawing;"%>
<%@ Import Namespace="System.Linq;"%>
<%@ Import Namespace="System.Text;"%>
<%@ Import Namespace="System.Threading.Tasks;"%>
<%@ Import Namespace="System.Windows.Forms;"%>
<%@ Import Namespace="System.Data.SqlClient;"%>
<%@ Import Namespace="System.Web.UI.Page" %>


<%@ public partial class _Default : System.Web.UI.Page
{
private SqlDataReader reader = null;
public SqlDataReader Reader { get { return reader; } set { reader = value; } }
    protected void Page_Load(object sender, EventArgs e)
    {
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection connection = new SqlConnection(connectionString);
    connection.Open();

    SqlCommand command = new SqlCommand("SELECT [totalHours] FROM [DB].[dbo].[TABLE] Where [DB].[dbo].[TABLE].[column] like 'persons name') = @variable1", connection);
        command.Parameters.Add(new SqlParameter("variable1", "anonymous"));

        Reader = command.ExecuteReader();
    }
}
 %>

Any ideas or thoughts would be appreciated. 任何想法或想法将不胜感激。

You should not place your C# code into the page directly. 您不应将C#代码直接放入页面中。

Instead you can make a user control following this guide: https://msdn.microsoft.com/en-us/library/ee231548.aspx 相反,您可以按照本指南制作用户控件: https//msdn.microsoft.com/en-us/library/ee231548.aspx

Then in your page you have to register a tag for the control 然后在您的页面中,您必须为控件注册一个标记

<%@ Register Tagprefix="MyControls" 
    Namespace="KM.MyControls.MyControl" 
    Assembly="KM.MyControls, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=<Your token>" %>

and then you can use your control into the page: 然后你可以使用你的控件进入页面:

<MyControls:MyUserControl runat="server"/>

source: https://sharepoint.stackexchange.com/questions/46629/how-to-put-custom-user-control-on-page-layout 来源: https//sharepoint.stackexchange.com/questions/46629/how-to-put-custom-user-control-on-page-layout

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

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