在SQL变量上设置表单字段值

时间:2012-09-19 08:15:07

标签: c# asp.net sql

我在SQL 2012存储过程中有以下内容:

V.VoterID IN (SELECT VoterID FROM vts_tbVoterAnswers WHERE AnswerID=@Seed)

@Seed是存储过程中的一个整数变量,它应该等于从名为:ddlSeed的ASP表单字段给出的值。

如何将SQL变量与表单字段的值连接?

编辑:你是对的@HeavenCore,我的问题似乎太模糊了,道歉。好的,这是表单代码:

<%@ Page Language="c#" MasterPageFile="MsterPageTabs.master" AutoEventWireup="false"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"><table summary="maintable" class="TableLayoutContainer">
<tr>
<td class="contentCell" valign="top">
<table summary="exporttable" class="innerText">
<tr>
<td>
<table summary="exporttypetable" class="innerText">
<asp:PlaceHolder ID="CSVOptionPlaceHolder" runat="server">
</asp:PlaceHolder>
<tr>
<td width="155">
<br /><strong>
<asp:Literal ID="SeedLabel" runat="server" EnableViewState="False">Seed : </asp:Literal></strong>
</td>
<td>
<asp:DropDownList ID="ddlSeed" runat="server">
<asp:ListItem Value=""> All </asp:ListItem>
<asp:ListItem Value="32"> Yes </asp:ListItem>
<asp:ListItem Value="31"> No </asp:ListItem>
</asp:DropDownList></td></tr></table>
</td></tr></table>
<asp:Button ID="ExportDataButton" runat="server" Text="Export CSV"></asp:Button></td></tr>
</table>
</asp:Content>

它的基本功能是允许用户从下拉菜单中进行选择并提交。下拉列表中的每个选项都包含一个值:

[空]为所有人 32是的 31为否

提交后,下拉列表的值应该成为以下存储过程中SQL变量 @Seed 的值:

USE [SurveyDB20652]
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
/// <summary>
/// Return the data needed to export a CSV  file
/// </summary>
*/
ALTER PROCEDURE [dbo].[vts_spVoterExportCSVData]
                @SurveyID int,
                @StartDate datetime,
                @EndDate datetime,
                @Seed int
AS

SELECT  SUBSTRING(Q.QuestionText,1,20) as QuestionText,Q.QuestionId,
 AnswerID,SelectionModeId,AnswerTypeId, 
SUBSTRING(Q.QuestionText,1,20)+'...'+' | '+ AnswerText   as ColumnHeader ,
AnswerText,
Q.DisplayOrder QuestionDisplayOrder,
Q.QuestionId,
Q.Alias QuestionAlias,
Q.QuestionIdText QuestionIdText,
A.DisplayOrder AnswerDisplayOrder,
A.AnswerId ,
A.AnswerAlias,Q.ParentQuestionid,
    case when q.parentQuestionId is null then null
    else (select count(*)+1 from vts_tbquestion q1 
             where q1.parentquestionid=q.parentquestionid
             and   q1.questionid<q.questionid
             ) 
    end as roworder,
    case when q.parentQuestionId is null then null
    else (select QuestionText from vts_tbquestion q1 
             where q1.questionid=q.parentquestionid
             ) 
    end as ParentQuestiontext,
    case when q.parentQuestionId is null then null
    else (select QuestionIdText from vts_tbquestion q1 
             where q1.questionid=q.parentquestionid
             ) 
    end as ParentQuestionIdtext,
    case when q.parentQuestionId is null then null
    else (select ALIAS from vts_tbquestion q1 
             where q1.questionid=q.parentquestionid
             ) 
    end as ParentQuestionAliastext,
A.AnswerIDText AnswerIdText
 FROM vts_tbQuestion Q
INNER JOIN vts_tbAnswer A
    ON A.QuestionID = Q.QuestionID
WHERE 
    SurveyID = @SurveyID  
ORDER BY Q.DisplayOrder, Q.QuestionID, A.DisplayOrder

SELECT
    V.VoterID,
    V.VoteDate,
    V.StartDate,
    V.IPSource,
    V.ContextUserName as username,
    (SELECT sum(ScorePoint) FROM vts_tbVoter 
        INNER JOIN vts_tbVoterAnswers
            ON vts_tbVoterAnswers.VoterID = vts_tbVoter.VoterID
        INNER JOIN vts_tbAnswer
            ON vts_tbAnswer.AnswerID = vts_tbVoterAnswers.AnswerID
        WHERE vts_tbVoter.VoterID = V.VoterID) AS Score
    FROM vts_tbVoter V
    WHERE 
        V.SurveyID = @SurveyID AND
        V.Validated <> 0 AND
        DATEDIFF (d,@startDate,V.VoteDate) >= 0 AND DATEDIFF (d,@endDate,V.VoteDate) <= 0 AND
        V.VoterID IN (SELECT VoterID FROM vts_tbVoterAnswers WHERE AnswerID=@Seed)
    ORDER BY V.VoterID DESC

SELECT
    V.VoterID,
    VA.AnswerID,
    SectionNumber,
    VA.AnswerText,
    AnswerTypeId,
    SelectionModeId,
    Q.QuestionId,
    A.AnswerText AnswerAnswerText,
    A.DisplayOrder AnswerDisplayOrder,
A.AnswerAlias,
A.AnswerIDText AnswerIdAlias
FROM vts_tbVoterAnswers VA
INNER JOIN vts_tbVoter V
    ON V.VoterID = VA.VoterID
INNER JOIN vts_tbAnswer A
    ON VA.AnswerId=A.AnswerId
INNER JOIN vts_tbQuestion Q
     ON A.QuestionId=Q.QuestionId
WHERE 
    V.SurveyID = @SurveyID AND
    V.Validated <> 0 AND
    DATEDIFF (d,@startDate,V.VoteDate) >= 0 AND DATEDIFF (d,@endDate,V.VoteDate) <= 0 AND
    V.VoterID IN (SELECT VoterID FROM vts_tbVoterAnswers WHERE AnswerID=@Seed)


ORDER BY V.VoterID DESC

上述程序基本上可以作为过滤器使用。它过滤掉了vts_tbVoterAnswers表中没有VoterID条目的所有数据库条目,该表的相应AnswerID条目等于@Seed的值(可以是''或32或31)

所以如果在下拉列表中: 我选择All,然后@ Seed ='':将选择表中的所有条目vts_tbVoterAnswers 如果我选择是,则@ Seed ='32':将过滤掉VoterID 120及其所有条目 如果我选择No,则@ Seed ='31':将过滤掉VoterID 109及其所有条目

vts_tbVoterAnswers表如下所示:

VoterID | AnswerID | SectionNumber | AnswerText

109     | 3        | 0             | 12/03/2000

109     | 23       | 0             | NULL

109     | 32       | 0             | NULL

120     | 3        | 0             | 26/11/1979

120     | 23       | 0             | NULL

120     | 31       | 0             | NULL

所以我的问题是如何将@Seer SQL变量与下拉列表给出的值相连接..:)

我希望这个解释比我之前的解释更好。 :)

1 个答案:

答案 0 :(得分:0)

你的问题很模糊,

假设@Seed是你的触发器的参数。 假设你的proc被称为usp_GetVotes

您可以从ASP调用proc将表单值传递给参数:

'#### Make sure you have adovbs.inc

'#### Ready objects
Set db = CreateObject("ADODB.Connection") 
set dbc = CreateObject("ADODB.Command") 

'#### Connect to SQL
db.Open "YourConnectionString"

'#### Build & Execute Command
With dbc
    .ActiveConnection = db
    .CommandText = "vts_spVoterExportCSVData" 
    .CommandType = adCmdStoredProc
    .Parameters.Append .CreateParameter("@Seed", adInt, adParamInput, , request.form("ddlSeed"))
    set rs = .Execute 
End With

或者,如果你想要非常快速和肮脏:

set rs = db.execute("EXEC vts_spVoterExportCSVData @Seed = " & replace(request.form("ddlSeed"),"'","''"))

为您澄清的问题编辑1:C#版本:

SqlConnection db = new SqlConnection(YourConnectionString);
SqlCommand dbc = new SqlCommand("vts_spVoterExportCSVData", db);
dbc.CommandType = CommandType.StoredProcedure;

SqlParameter Seed = dbc.Parameters.Add("@Seed", SqlDbType.Int);
Seed.Direction = ParameterDirection.Input;
Seed.Value = request.form("ddlSeed");
db.Open();

SqlDataReader Results = dbc.ExecuteReader();

while (Results.Read())  {
    //Results["VoterID"].ToString();
    //Results["AnswerID"].ToString();
    //Results["SectionNumber"].ToString();
    //Results["AnswerText"].ToString();
};

Results.Close();