0

我有这个存储过程:

exec sp_Defect_B '2013-05-20 00:00:00','2013-05-25 23:59:59'

哪个有IF..ELSE执行不同的事情取决于给定的代码:

alter proc [dbo].[p_Defect_B] (@dtFrom datetime, @dtTo datetime)            
as            
begin  
DECLARE @Total TABLE 
(
  [No] int, TSampel float
)
-- Total
insert into @Total
select 1 as [No], SUM(H.Total) as TSampel from TrxDBHHDr HH
left join TrxDBHdr H on HH.DBNO=H.DBNO
left join ProductType PT on H.ACd=PT.ACd and PT.GCd=1
where HH.Deleted=0 and HH.DBDate between @dtFrom and @dtTo

DECLARE @Defect TABLE 
(
  DefectCd varchar(15),Name varchar(50), Defect float
)
-- Defect
insert into @Defect
select D.DefectCd,DB.Name,sum(coalesce(D.Qty,0)) as Defect from TrxDBHHDr HH
left join TrxDBDtl D on HH.DBNO=D.DBNO
left join ProductType PT on D.acd=PT.ACd and PT.GCd=1
left join DefectBK DB on DB.DefectCd=D.DefectCd
where HH.Deleted=0 and HH.DBDate between @dtFrom and @dtTo
group by D.DefectCd,DB.Name

DECLARE @SubTotal TABLE 
(
  Name varchar(50), Defect float, TSampel float, PDefect float
)
insert into @SubTotal
select D.Name,D.Defect,T.TSampel,D.Defect*100/T.TSampel as PDefect from @Defect D
left join @Total T on T.[No]=1
order by PDefect desc

DECLARE @TotalD TABLE 
(
  [No] int,Defect float
)

insert into @TotalD
select 1, Sum(D.Defect) as Defect from @Defect D

insert into @SubTotal
select 'Total Defect', D.Defect, T.TSampel, D.Defect*100/T.TSampel as PDefect from @TotalD D
left join @Total T on T.[No]=1

select * from @SubTotal
end

我在 SSMS 中执行代码,它运行良好。但是当我尝试在 C# Windows 应用程序中使用代码时,它没有得到任何价值......这怎么可能?我错过了什么吗?

只有这个存储过程没有返回表值......

我尝试使用临时表,表变量,他们仍然没有返回表值......

这是 C# 代码:

sql= "exec p_Defect_B '2013-05-20 00:00:00','2013-05-25 23:59:59'";

RunQuery qu_data = new RunQuery();
DataTable data = new DataTable();
data = qu_data.OpenAdoQuery(sql,"IP")

这是我将 C# 连接到 SQL Server 的程序的一部分

myCon = new OleDbConnection(strCon);
DataTable myData = new DataTable();

myCon.Open();

OleDbDataAdapter myOleAdapter = new OleDbDataAdapter();
myOleAdapter.SelectCommand = new OleDbCommand(sql,myCon);
myOleAdapter.Fill(myData);
myCon.Close();
  • SMSS 中的所有表都返回值。
  • 所有表变量都在 SMSS 中显示结果。
  • 结果未显示在使用 ADOAdapter 的 C# Windows 应用程序上。
  • 我尝试使用临时表和表变量,但没有用。
  • 我试过不使用IF..ELSE,没有用。
4

1 回答 1

2

你真的应该使用SqlConnectionthen 连接到 SQL Server - 你应该使用标准执行你的存储过程SqlCommand- 不要使用EXEC....代码。

试试这个代码:

// setup connection and command
using (SqlConnection conn = new SqlConnection(-your-connection-string-here-))
using (SqlCommand cmd = new SqlCommand("dbo.p_Defect_B", conn))
{
    // define command as stored procedure
    cmd.CommandType = CommandType.StoredProcedure;

    // define and set parameter values
    cmd.Parameters.Add("@dtFrom", SqlDbType.DateTime).Value = new DateTime(2013, 5, 20);
    cmd.Parameters.Add("@dtFrom", SqlDbType.DateTime).Value = new DateTime(2013, 5, 25, 23, 59, 59);

    // execute your query
    conn.Open();

    // get a data reader to read the values from the result set
    using (SqlDataReader rdr = cmd.ExecuteReader())
    {
        // iterate over the result set
        while (rdr.Read())
        {
            // fetch the values - depends on your result set - YOU NEED TO ADAPT THIS!
            var value1 = rdr.GetInt(0);
            var value2 = rdr.GetString(1);
            ......
        }

        rdr.Close();
    }

    conn.Close();
}
于 2013-06-14T08:39:22.260 回答