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

Posted

技术标签:

【中文标题】MS Management Studio 中的存储过程返回数据。使用 C# 调用时不返回任何数据【英文标题】:Store Procedure Return Data in MS Managment Studio .Not return Any Data When Call Using C# 【发布时间】:2012-10-23 23:46:09 【问题描述】:

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

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# 中没有 Dara 返回..

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

我想你想说 SqlCommand = new SqlCommand("SerachTickets",sqlCon);...不,这不行【参考方案2】:

我不知道代码出了什么问题,但我做了一些更改,但它不工作SQL 管理工作室。

现在运行良好。

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;

【讨论】:

以上是关于MS Management Studio 中的存储过程返回数据。使用 C# 调用时不返回任何数据的主要内容,如果未能解决你的问题,请参考以下文章

如何从 SQL Server Management Studio 中的表生成 CRUD 存储过程

在 SQL 2008 Management Studio 中调试时查询存储过程中的临时表

在 MS SQL Server Management Studio 中处理事务的最佳方式

无法使用 Liferay 6.2 连接 MS SQL Server management studio 2008 r2

您会考虑使用MS SQL Server Management Studio的替代方案吗? [关闭]

在 SQL Server Management Studio 中调试存储过程