[英]Stored procedure for Repeater - Select columns from table
I am using a Repeater
in Visual Studio (asp.net and c#). 我在Visual Studio(asp.net和c#)中使用
Repeater
。 The thing is, I am not quite sure how to properly format my stored procedure. 问题是,我不太确定如何正确格式化存储过程。
Here is the error: 这是错误:
My goal is to have a Repeater
display a label and textbox for each column in my table. 我的目标是让
Repeater
为表中的每一列显示一个标签和文本框。 The label must have the DisplayName
from the database. 标签必须具有数据库中的
DisplayName
。
Here is my table, FormField
, that I want to use for my Repeater
. 这是我要用于
Repeater
表FormField
。
So here is my idea for my stored procedure at the moment: 所以这是我目前存储过程的想法:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spGetFormFields]
@TableName nvarchar,
@EventId int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT COLUMN_NAME AS MyColumn
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
AND EventId = @EventId
AND Visible = 1
END
However this doesn't quite work, as I get errors at EventId
and Visible
: 但是,这并不是很有效,因为我在
EventId
和Visible
遇到错误:
Invalid Column Name
无效的列名
So here is what I have in my C#: 这就是我在C#中拥有的东西:
protected void Page_Load(object sender, EventArgs e)
{
//Make label names diff
//Control which columns are shown
//Style the labels and textboxes to css
DataTable table = new DataTable();
table.Columns.Add("MyColumn", typeof(string));
using (SqlConnection sqlConn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Events2"].ConnectionString))
{
sqlConn2.Open();
using (SqlCommand sqlCmd2 = new SqlCommand())
{
sqlCmd2.Connection = sqlConn2;
sqlCmd2.CommandType = System.Data.CommandType.Text;
sqlCmd2.CommandText = string.Format("SELECT COLUMN_NAME AS MyColumn FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName AND EventId = @EventId AND Visible = 1");
sqlCmd2.Parameters.Add("@TableName", SqlDbType.NVarChar).Value = "FormField";
sqlCmd2.Parameters.Add("@EventId", SqlDbType.NVarChar).Value = "1";
sqlCmd2.ExecuteNonQuery();
using (SqlDataReader sqlReader = sqlCmd2.ExecuteReader())
{
while (sqlReader.Read())
{
DataRow tableRow = table.NewRow();
tableRow["MyColumn"] = sqlReader["MyColumn"].ToString();
table.Rows.Add(tableRow);
}
Repeater1.DataSource = table;
Repeater1.DataBind();
}
sqlConn2.Close();
}
}
}
}
My C# code implements the SQL without any stored procedure (still gets the same error about the columns not existing). 我的C#代码无需任何存储过程即可实现SQL(关于不存在的列仍会收到相同的错误)。 Of course I want to add in the stored procedure once I get it to work, so this was for testing purposes.
当然,我要在存储过程开始工作后添加它,因此这是出于测试目的。
And finally, here is the asp.net markup: 最后,这是asp.net标记:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeater.aspx.cs" Inherits="admin_repeater" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server" Text='<%# Eval("MyColumn") %>'></asp:Label>
<asp:TextBox ID="textTest" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
As mentioned in the comment, You are querying INFORMATION_SCHEMA.COLUMNS
table and there is no such column EventId
& Visible
there. 如评论中所述,您正在查询
INFORMATION_SCHEMA.COLUMNS
表,并且那里没有EventId
& Visible
这样的列。 If I have understood you correctly you want to display DisplayName
from your table based on condition then you can directly do it like this:- 如果我正确理解了您想基于条件从表中显示
DisplayName
,那么您可以直接这样做:
SELECT DisplayName AS MyColumn
FROM [Events2].dbo.[FormField]
WHERE EventId = @EventId AND
Visible = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.