存储过程在MS Management Studio中返回数据。使用C#调用时不返回任何数据

时间:2012-11-03 14:33:12

标签: asp.net sql-server-2008 c#-4.0 stored-procedures dataset

==========================商店程序=================== ============

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROC [dbo].[SerachTickets]
    @StoreID varchar(70),
    @StatusID varchar(70),
     @CustomerName varchar(400),
         @TicketNo varchar(70),
         @StratDate varchar(100),
         @ESTDate varchar(100),
         @StaffID varchar(100),
         @ServiceItemID varchar(100)
AS 
    SET NOCOUNT ON 
    SET XACT_ABORT ON  
    declare @sql nvarchar(MAX)

    Set @sql='SELECT   Ticket.TicketID,Ticket.StaffID,ServiceItemInfo.ServiceItemID,
    ISNULL(TicketWorkLog.WorkLogDesc,'''') WorkLogDesc,
     Ticket.SerialNo, Convert(varchar(50),Ticket.CreationDate,101) CreationDate,
Admin_ServiceItem.ServiceItem,Convert(varchar(50),Ticket.CompletionDate,101) CompletionDate, Ticket.IsPickedUp, 
Ticket.IsPaid, Ticket.IsVoid, Ticket.IsReadyForPayment, ISNULL(Ticket.TicketDetail,'''') TicketDetail,
Admin_Staff.StaffFirstName + '' ''+ Admin_Staff.StaffLastName As StaffFullName
FROM   Ticket 
LEFT JOIN   Admin_Staff ON Ticket.StaffID = Admin_Staff.StaffID 
LEFT JOIN   TicketWorkLog ON Ticket.TicketID = TicketWorkLog.TicketID
LEFT JOIN   ServiceItemInfo On Ticket.TicketID = ServiceItemInfo.TicketID
LEFT JOIN   Admin_ServiceItem On ServiceItemInfo.ServiceItemID = Admin_ServiceItem.ServiceItemID  
LEFT JOIN   Customer On Ticket.CustomerID = Customer.CustomerID
       WHERE Ticket.StoreID='+@StoreID

       if @StatusID <> '' 
        BEGIN
        Set @sql =@sql + ' AND TicketWorkLog.StoreWorkLogID = '+@StatusID
        END 

       if @CustomerName <> ''
        BEGIN
        Set @sql =@sql + ' AND (Customer.FirstName Like(''%'+@CustomerName+'%'')'
        END

        if @CustomerName <> ''
        BEGIN
        Set @sql =@sql + ' OR Customer.LastName Like(''%'+@CustomerName+'%''))'
        END

       if @TicketNo <> ''
        BEGIN
        Set @sql =@sql + ' AND Ticket.SerialNo Like(''%'+@TicketNo+'%'')'       
        END

       if @StratDate <> '1900-01-01 00:00:00'
        BEGIN
        Set @sql =@sql + ' AND Ticket.CreationDate Between ''' +Convert(varchar(100),Convert(DateTime,@StratDate),101)+ ''' AND '''+Convert(varchar(100),Convert(DateTime,@StratDate),101)+''''     
        END

       if @ESTDate <> '1900-01-01 00:00:00'
        BEGIN
        Set @sql =@sql + ' AND Ticket.CompletionDate Between ''' +Convert(varchar(100),Convert(DateTime,@ESTDate),101)+ ''' AND '''+Convert(varchar(100),Convert(DateTime,@ESTDate),101)+''''       
        END
       if @StaffID <> '0'
        BEGIN
        Set @sql =@sql + ' AND Ticket.StaffID ='+@StaffID       
        END

       if @ServiceItemID <> ''
       BEGIN
        Set @sql =@sql + ' AND ServiceItemInfo.ServiceItemID ='+@ServiceItemID      
        END

--print @sql;
--exec SerachTickets 1,'','','','1900-01-01 00:00:00','1900-01-01 00:00:00',0,''  
Exec (@sql)

============================== C#CODE =============== ===================

try
            {
                using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ToString()))
                {
                    DateTime? dtStart = Convert.ToDateTime("01/01/1900");
                    DateTime? dtEst = Convert.ToDateTime("01/01/1900");
                    int intAssign = 0;

                    if (!string.IsNullOrEmpty(Request.QueryString["sdate"]))
                    {
                        dtStart = Convert.ToDateTime(Request.QueryString["sdate"]);
                    }

                    if (!string.IsNullOrEmpty(Request.QueryString["estdate"]))
                    {
                        dtEst = Convert.ToDateTime(Request.QueryString["estdate"]);
                    }

                    if (!string.IsNullOrEmpty(Request.QueryString["assign"]))
                    {
                        intAssign = Convert.ToInt32(Request.QueryString["assign"]);
                    }


                    using (SqlCommand cmd =sqlCon.CreateCommand())
                    {
                        DataSet dataset = new DataSet();

                        sqlCon.Open();

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "SerachTickets";

                        cmd.Parameters.AddWithValue("@StoreID", Session["StoreID"]);
                        cmd.Parameters.AddWithValue("@StatusID", Request.QueryString["sid"]);
                        cmd.Parameters.AddWithValue("@CustomerName", Request.QueryString["cname"]);
                        cmd.Parameters.AddWithValue("@TicketNo", Request.QueryString["tno"]);
                        cmd.Parameters.AddWithValue("@StratDate", dtStart);
                        cmd.Parameters.AddWithValue("@ESTDate", dtEst);
                        cmd.Parameters.AddWithValue("@StaffID", intAssign);
                        cmd.Parameters.AddWithValue("@ServiceItemID", Request.QueryString["itemid"]);
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {

                            da.Fill(dataset);
                            cmd.Connection.Close();
                            grdTickets.DataSource = dataset.Tables[0];
                            grdTickets.DataBind();

                        }
                    }
                }

            }
            catch (Exception ee)
            {
            }

=============================================== ===============================

我已经使用SQL PROILER检查了哪些值C#发送到SQL Server ..

exec SerachTickets @ StoreID = 1,@ StatusID = N'',@ CustomerName = N'',@ TicketNo = N'',@ StratDate ='1900-01-01 00:00:00',@ ESTDate = '1900-01-01 00:00:00',@ StaffID = 0,@ ServiceItemID = N''

在MS Managment Studio中运行此查询时它返回所需的结果,但在C#No Dara返回..

2 个答案:

答案 0 :(得分:0)

使用SqlCommand cmd =new SqlCommand(sqlCon)代替SqlCommand cmd =sqlCon.CreateCommand()

并在调试期间检查适配器是否填充数据集。

答案 1 :(得分:0)

我不知道代码有什么问题,但是我做了一些更改而且没有工作。我现在改变了命令文本而不是使用过程我正在使用这个查询,当我运行时返回记录SQL Management Studio。

现在它运作良好。

string strCommand =“exec SerachTickets @ StoreID =”+ Session [“StoreID”] +“,@ StatusID = N'”+ Request.QueryString [“sid”] +“',@ CustomerName = N'”+ Request .QueryString [“cname”] +“',@ TicketNo = N'”+ Request.QueryString [“tno”] +“',@ StratDate ='”+ dtStart +“',@ ESTDate ='”+ dtEst +“ ',@ StaffID =“+ intAssign +”,@ ServiceItemID = N'“+ Request.QueryString [”itemid“] +”'“;

cmd.CommandText = strCommand;