简体   繁体   English

从C#App连接到SQL数据库时,我收到消息“超时过期..”

[英]I get message “Timeout expired ..” when connect to SQL database from C# App

I wrote a sale management software. 我写了一个销售管理软件。 It is working fine with almost functions (it means connection string is right) except only one function "Production plan". 除了只有一个功能“生产计划”以外,几乎所有功能(表示连接字符串正确)都可以正常工作。 When I click button to perform this function I get a error message "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding". 当我单击按钮来执行此功能时,我收到一条错误消息“超时到期。在操作完成之前超时时间已过,或者服务器没有响应”。

In this function, I call a stored procedure and I tested it in SQL Management Studio, it works fine and fast (0.2 second). 在此函数中,我调用了一个存储过程,并在SQL Management Studio中对其进行了测试,它可以正常且快速地运行(0.2秒)。

Please help me to find out a solve. 请帮助我找出解决方案。 Thanks in advanced! 提前致谢! Here is my stored procedure: 这是我的存储过程:

CREATE proc [dbo].[SPChiTietKeHoachGiao] (@makhg nvarchar(15))  
as  
begin  
declare @ngay datetime, @thang int, @nam int,  
  @ngay1 datetime, @thang1 int, @nam1 int,  
  @ngay2 datetime, @thang2 int, @nam2 int,  
  @ngay3 datetime, @thang3 int, @nam3 int,  
  @ngay4 datetime, @thang4 int, @nam4 int  
-- Lay tinh hinh tieu thu cua thang truoc  
select top 1 
           @ngay=dateadd(month, -1,Thang) 
from KeHoachGiao 
where MaKeHoachGiao = @makhg   
set @thang = month(@ngay)  
set @nam = year(@ngay)  
set @ngay1=dateadd(MONTH,-1,@ngay)  
set @ngay2=dateadd(MONTH,-1,@ngay1)  
set @ngay3=dateadd(MONTH,-1,@ngay2)  
set @ngay4=dateadd(MONTH,-1,@ngay3)  
set @thang1=month(@ngay1)  
set @nam1=year(@ngay1)  
set @thang2=month(@ngay2)  
set @nam2=year(@ngay2)  
set @thang3=month(@ngay3)  
set @nam3=year(@ngay3)  
set @thang4=month(@ngay4)  
set @nam4=year(@ngay4)  

select ct.MaSP, 
         TenSP,
         xuatkho4.TieuThuTrongThang4,
         xuatkho3.TieuThuTrongThang3,
         xuatkho2.TieuThuTrongThang2,
         xuatkho1.TieuThuTrongThang1,  
  (   
    ISNULL ( TieuThuTrongThang4, 0 )
   +ISNULL ( TieuThuTrongThang3, 0 )
   +ISNULL ( TieuThuTrongThang2, 0 )
   +ISNULL ( TieuThuTrongThang1, 0 ) ) / 4 TTBQ,  

        TonDauKy,
        NhapKhoTrongThang,
        xuatkho.TieuThuTrongThang,
        TonCuoiKy,  

   case KeHoachSanXuat 
        when 0 then null else KeHoachSanXuat end KeHoachSanXuat,  

        KeHoachSanXuat * DinhMuc / 1000000 SanLuong,  
    case SoLuongDaNhap 
         when 0 then null else SoLuongDaNhap end SoLuongDaNhap,  

    case isnull( KeHoachSanXuat, 0 ) 
         when 0 then null 
         else 100 * SoLuongDaNhap / isnull ( KeHoachSanXuat, 100) end TiLeHoanThanh  

from ( 
       select * from ChiTietKeHoachGiao where makehoachgiao = @makhg) ct 

       inner join  
       (
         select masp
              , tensp 
              , stt
              , MaLoai
              , DinhMuc 
         from SanPham
       ) sp 

       ON ct.MaSP = sp.MaSP  

       left join  
       (  
          select ctpx.MaSP
              , SUM( ctpx.SoLuong ) TieuThuTrongThang  
          from   
          (
             select * from PhieuXuatKho 
             where MONTH(NgayXuat)= @thang 
             and YEAR(NgayXuat) = @nam 
           ) pxk  

          inner join ChiTietXuatKho ctpx 
          on pxk.MaPhieuXuat = ctpx.MaPhieuXuat  
          group by ctpx.MaSP  
       ) xuatkho 
      on sp.MaSP = xuatkho.MaSP  


      left join  
      (  
        select ctpx.MaSP
             , sum(ctpx.SoLuong) TieuThuTrongThang1  
        from   
           ( select * from PhieuXuatKho 
             where MONTH(NgayXuat) = @thang1 
             and YEAR ( NgayXuat) = @nam1 

            ) pxk  

             inner join ChiTietXuatKho ctpx 
             on pxk.MaPhieuXuat = ctpx.MaPhieuXuat  
             group by ctpx.MaSP  

      ) xuatkho1 

      on sp.MaSP = xuatkho1.MaSP  

      left join  
      (  
         select ctpx.MaSP
              , sum ( ctpx.SoLuong ) TieuThuTrongThang2  
         from   
            (
              select * from PhieuXuatKho 
              where MONTH ( NgayXuat) = @thang2 
              and YEAR ( NgayXuat)= @nam2

             ) pxk

             inner join ChiTietXuatKho ctpx 
             on pxk.MaPhieuXuat = ctpx.MaPhieuXuat  
             group by ctpx.MaSP  

      ) xuatkho2 
      on sp.MaSP = xuatkho2.MaSP  

      left join  
      (  
        select ctpx.MaSP
             , sum ( ctpx.SoLuong ) TieuThuTrongThang3  
        from   
            (
               select * from PhieuXuatKho 
               where MONTH ( NgayXuat ) = @thang3 
               and YEAR ( NgayXuat ) = @nam3 

              ) pxk  

              inner join ChiTietXuatKho ctpx 
              on pxk.MaPhieuXuat = ctpx.MaPhieuXuat  
              group by ctpx.MaSP  

      ) xuatkho3 
      on sp.MaSP = xuatkho3.MaSP  

      left join  
      (  
        select ctpx.MaSP
             , sum(ctpx.SoLuong) TieuThuTrongThang4  
        from   
            (
              select * from PhieuXuatKho 
              where MONTH(NgayXuat) = @thang4 
              and YEAR(NgayXuat) = @nam4

              ) pxk  

              inner join ChiTietXuatKho ctpx 
              on pxk.MaPhieuXuat = ctpx.MaPhieuXuat  
              group by ctpx.MaSP  

       ) xuatkho4 
       on sp.MaSP = xuatkho4.MaSP  

      inner join 
               (
                 select maloai
                      , tenloai 
                 from loaisanpham

                 ) loai 

        on sp.maloai = loai.maloai  
        order by loai.tenloai
               , sp.STT  
end  

I once had such an issue, the solution was to recompile the SP. 我曾经遇到过这样的问题,解决方案是重新编译SP。 You can do this by adding WITH RECOMPILE between the parameters declarations and the AS keyword or use sp_recompile SP. 可以通过在参数声明和AS关键字之间添加WITH RECOMPILE来实现,或使用sp_recompile SP。 Hope this helps 希望这可以帮助

http://msdn.microsoft.com/en-us/library/ms190439.aspx http://msdn.microsoft.com/en-us/library/ms190439.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从C#连接到SQL数据库? - How do I connect to a SQL database from C#? MySqlConnector 'Connect Timeout Expired' Xamarin Forms 和 c# - MySqlConnector 'Connect Timeout Expired' Xamarin Forms and c# 当我尝试从C#Windows应用程序运行ssis软件包时,我收到一条失败消息。 - I get a failure message when i try to run a ssis package from C# windows app.. C#MySql超时已过期 - C# MySql Timeout expired C#方法超时已过期 - Timeout expired on a C# method 当我尝试使用visual c#连接SQL Server时收到此错误消息 - I am getting this error message when I try to connect SQL Server using visual c# 我无法将我的数据库与我的 c# windows 表单应用程序连接,我收到“未处理的异常”错误 - i cant connect my database with my c# windows form app i get "unhandled exception" error Blazor C# App如何连接到Azure Z9778840A0100CB30C982876741B0数据库? 苹果系统 - How to connect Blazor C# App to Azure SQL Database? MacOS 尝试连接数据库时出现“错误:无法初始化OLE”? C# - I get an “Error: Cannot initialize OLE” when I try to connect to a database? C# 尝试连接数据库时出现“错误:无法初始化OLE”? C# - I get an “Error: Cannot initialize OLE” when I try to connect to a database? C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM