【问题标题】:Add SQL Query And Variable to SharePoint ASPX Page将 SQL 查询和变量添加到 SharePoint ASPX 页面
【发布时间】:2015-08-15 19:05:28
【问题描述】:

我在 sharepoint 2013 网站上创建了一个 aspx 页面。我已经完全按照我的需要自定义了页面 css、按钮和布局。现在我的目标是向我们网络上的数据库添加一个 SQL 连接,以根据当前登录的用户提取数据。我编写的 SQL 查询基本上是“从数据库中选择“字段”,其中 SharePointUserloggedinname 像 'db.table.field'。我已经在 sql 管理工作室测试了这个查询,它运行良好。我的问题是如何添加将此 sql 查询部分放入我的 aspx 页面,将查询结果设置为变量,然后显示查询结果(变量)以通过文本字段/段落标记/或其他显示。

我已将命名空间和下面提供的 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();
    }
}
 %>

任何想法或想法将不胜感激。

【问题讨论】:

  • 您确定您已经测试了该查询吗?因为它看起来很奇怪[...] like 'persons name') = @variable1。没有括号,变量看起来放错了位置。
  • 我已经编辑了 sql 查询本身以在公共网站上显示,并且在编辑之前它确实通过 Visual Studio 和 sql mgmt Studio 正常工作。至于变量创建,该部分是通过我提到的文章找到的,并且不确定将其放置在 asp 页面中的哪个位置。
  • 我明白了,但是按照现在的写法,它会抛出一个错误。也许你的意思是:"SELECT [totalHours] FROM [DB].[dbo].[TABLE] Where [DB].[dbo].[TABLE].[column] like @variable1"
  • 是的,正确,但我的问题是我在我的 aspx 页面上方的哪里放置这个 sql 连接,以及稍后如何在我的页面的一部分中调用它

标签: c# sql asp.net visual-studio sharepoint


【解决方案1】:

您不应将 C# 代码直接放入页面中。

相反,您可以按照本指南制作用户控件: https://msdn.microsoft.com/en-us/library/ee231548.aspx

然后在你的页面中你必须为控件注册一个标签

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

然后你就可以使用你的控件进入页面了:

<MyControls:MyUserControl runat="server"/>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多