如何将数据从aspx页面转移到存储过程?

时间:2011-05-17 05:26:40

标签: asp.net sql sql-server criteria user-defined-functions

我必须在asp.net中创建一个RDLC报告。我正在使用不同的标量函数编写存储过程。 我有一个GUI,我必须根据该标准选择一个标准并收集数据。 GUI在这里: enter image description here 我已经实现了所有案例,如“所有员工”,“所有服务”等,但问题来自于我必须从列表框中选择特定记录并根据该数据提供数据。例如,我从列表中选择特定的员工记录,我的报告应显示仅包含所选记录的记录。 我怎么处理这个?我的意思是当我从列表框中选择一些记录时,如何将这些记录带到我的存储过程的where子句以及如何在那里使用它?

我正在使用SQL SERVER 2008。 我的存储过程是:

USE [PC_Rewrite]

GO SET ANSI_NULLS ON 走 SET QUOTED_IDENTIFIER ON 走 ALTER PROCEDURE [dbo]。[spGetClients] (     @orderBy varchar(50) )

AS 开始      - 添加SET NOCOUNT ON以防止出现额外的结果集      - 干扰SELECT语句。     SET NOCOUNT ON;

-- Insert statements for procedure here
IF(@orderBy = 'Consumer Name')
BEGIN
    SELECT c.Id, c.LastName, c.FirstName, c.MiddleInit, 
    c.DateOfBirth, dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, c.Sex, cs.Status, ca.Address, co.Phone, 
    dbo.GetEthnicity(c.Id) AS Ethnicity, dbo.GetDevelopmentalDisabilities(c.Id) AS Disabilities, 
    dbo.GetClientStaffContacts(c.Id) AS Staff, dbo.GetClientContacts(c.Id) AS Contact, 
    dbo.GetClientInsuranceProviders(c.Id) AS InsuranceProvider FROM Client c 
    LEFT OUTER JOIN ClientStatus cs ON cs.Id = c.StatusId  
    LEFT OUTER JOIN(        
        SELECT ca.ParentEntityId, ca.Address
        FROM ContactAddress ca
        INNER JOIN EntityName en ON en.Id = ca.EntityNameId AND en.Name = 'Client' 
        INNER JOIN GeneralLookup gl ON ca.glAddressTypeId = gl.Id AND gl.LookupItem = 'Primary'    
    ) ca ON c.Id = ca.ParentEntityId 
    LEFT OUTER JOIN(        
        SELECT co.ParentEntityId, co.ContactData Phone
        FROM ContactOther co
        INNER JOIN EntityName en ON en.Id = co.EntityNameId AND en.Name = 'Client' 
        INNER JOIN GeneralLookup gl ON co.glContactTypeId = gl.Id AND gl.LookupItem = 'Home'    
    ) co ON c.Id = co.ParentEntityId 
    ORDER BY c.LastName, c.FirstName, c.MiddleInit
END

ELSE IF(@orderBy = 'Consumer Address')
BEGIN
    SELECT c.Id, c.LastName, c.FirstName, c.MiddleInit, 
    c.DateOfBirth, dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, c.Sex, cs.Status, ca.Address, co.Phone, 
    dbo.GetEthnicity(c.Id) AS Ethnicity, dbo.GetDevelopmentalDisabilities(c.Id) AS Disabilities, 
    dbo.GetClientStaffContacts(c.Id) AS Staff, dbo.GetClientContacts(c.Id) AS Contact, 
    dbo.GetClientInsuranceProviders(c.Id) AS InsuranceProvider FROM Client c 
    LEFT OUTER JOIN ClientStatus cs ON cs.Id = c.StatusId  
    LEFT OUTER JOIN(        
        SELECT ca.ParentEntityId, ca.Address
        FROM ContactAddress ca
        INNER JOIN EntityName en ON en.Id = ca.EntityNameId AND en.Name = 'Client' 
        INNER JOIN GeneralLookup gl ON ca.glAddressTypeId = gl.Id AND gl.LookupItem = 'Primary'    
    ) ca on c.Id = ca.ParentEntityId 
    LEFT OUTER JOIN(        
        SELECT co.ParentEntityId, co.ContactData Phone
        FROM ContactOther co
        INNER JOIN EntityName en ON en.Id = co.EntityNameId AND en.Name = 'Client' 
        INNER JOIN GeneralLookup gl ON co.glContactTypeId = gl.Id AND gl.LookupItem = 'Home'    
    ) co ON c.Id = co.ParentEntityId 
    ORDER BY ca.Address
END

END

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:1)

如果您的存储过程如下所示:

Create PROCEDURE [dbo].[sp]
@ID int
AS
BEGIN
    Select ID, Display, GroupID           
    From SystemTypes
    Where ID = @ID
END

然后您可以像这样设置ID参数:

SqlCommand command = new SqlCommand("sp",connection);
command.CommandType = CommandType.StoredProcedure;
Command.Parameters.Add("@ID, SqlDbType.Int).Value = **VALUE**;
conn.open();

SqlDataReader reader = command.ExecuteReader();

如果你想使用逗号分隔值字符串之类的东西..我找到了这个链接:

http://www.outsightinteractive.com/blog/comma_delimited_list_as_stored_procedure_parameter

http://www.4guysfromrolla.com/webtech/031004-1.shtml

Passing List<> to SQL Stored Procedure

答案 1 :(得分:0)

您的存储过程是否已存在?如果是这样,你必须使用参数吗?您可以根据需要随意更改存储过程吗?

有许多关于如何调用存储过程的选项。这里有两个:您可以创建一个SqlCommand对象并将其设置为使用存储过程,然后为where子句中的各个子句创建SqlParameters。例如,显示屏幕上每个选项的参数。对于列表框,您可以传入逗号分隔字符串,但这取决于您的数据库模式。

另一种方法是使用Linq To Sql并将sproc拖放到设计器上并让它为您创建一个方法,然后使用生成的方法参数调用它。

很抱歉不再具体,但受存储过程的影响。

答案 2 :(得分:0)

我想您想将搜索条件传递给您的商店程序吗? 使用这样的东西..

//在代码背后

CommandType.StoredProcedure;

//为您的条件传递每个参数,如下所示 Command.Parameters.Add(“@ ID,SqlDbType.string).Value = staff;
Command.Parameters.Add(“@ ID,SqlDbType.string).Value = service;

//现在在商店程序中 将sql声明为varchar(200) set sql =“select * from tablename”

//检查参数是空白还是有值 if(@ paraname1!=“”)     set sql = sql +“where ... parameter1”; 结束如果

if(@ paraname2!=“”)     set sql = sql +“where ... parameter2”; 结束如果


这是你在找什么?

答案 3 :(得分:0)

set @query ='select Resume.ResumeID,Resume.Name,Resume.DepartmentID,Department.DepartmentName,         Resume.CurrentCompany,Resume.CurrentLocation,Resume.Phone,Resume.Email,         Resume.KeySkills,Resume.TotalExpYears,Resume.TotalExpMonths,Resume.CandidateTextResume,Resume.ResumeStatus,         Resume.IsActive来自[Resume]内部联接[部门] [简历] .DepartmentID = [部门] .DepartmentID其中[简历] .IsActive = 1'

if(@DepartmentName != '')
begin 
        select  @query = @query + ' And [Department].DepartmentName like ''%'  + @DepartmentName + '%'''
end  

if(@TotalExpYears != '')
begin 
        select  @query = @query + ' And TotalExpYears like ''%'  + @TotalExpYears + '%'''
end  

if(@TotalExpMonths != '')
begin 
        select  @query = @query + ' And TotalExpMonths like ''%'  + @TotalExpMonths + '%'''
end

if(@KeySkills != '')
begin 
        select  @query = @query + ' And KeySkills like ''%'  + @KeySkills + '%'''
end

if(@CurrentLocation != '')
begin 
        select  @query = @query + ' And CurrentLocation like ''%'  + @CurrentLocation + '%'''
end

if(@CandidateTextResume != '')
begin 
        select  @query = @query + ' And CandidateTextResume like ''%'  + @CandidateTextResume + '%'''
end

if(@CurrentCompany != '')
begin 
        select  @query = @query + ' And CurrentCompany like ''%'  + @CurrentCompany + '%'''
end
if(@ResumeStatus != '')
begin
    select  @query = @query + ' And ResumeStatus like ''%'  + @ResumeStatus + '%'''
end

============== @strId as varchar(100)// strId是逗号分隔的ID 'select * FROM tblSchool where intSchoolId IN('+ @strId +')'

================= 现在好吗?