0

我在 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)

因此,如果在下拉列表中:我选择全部,则 @Seed='' : 将选择表 vts_tbVoterAnswers 中的所有条目如果我选择是,则 @Seed='32': 将过滤掉 VoterID 120 及其所有条目如果我选择否,则 @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 变量与下拉列表给出的值连接起来.. :)

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

4

1 回答 1

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();
于 2012-09-19T08:48:25.993 回答