任务:
用必要的数据替换 sql 语句中的某些变量。
问题:
变量没有被替换
C#代码:
SqlCommand cmd = new SqlCommand(@"
select
1 [GL],
(
select
isnull(COUNT(*), 0)
from
@DataDB..APSetup
) [AP],
(
select
isnull(COUNT(*), 0)
from
@DataDB..ARSetup
) [AR],
(
select
isnull(COUNT(*), 0)
from
@DataDB..CASetup
) [CA],
case
when (
select
isnull(COUNT(*), 0)
from
@DataDB..SalesTax
) > 0
then 1 else 0
end [TX],
(
select
isnull(COUNT(*), 0)
from
@DataDB..INSetup
) [IN],
(
select
isnull(COUNT(*), 0)
from
@DataDB..POSetup
) [PO],
(
select
isnull(COUNT(*), 0)
from
@DataDB..SOSetup
) [SO],
(
select
isnull(COUNT(*), 0)
from
@DataDB..RQSetup
) [RQ],
(
select
COUNT(*)
from
@DataDB..pcsetup
Where
Setupid = 'PC' AND PERNBR <> ''
) [PJ]
from
@DataDB..glsetup");
cmd.Parameters.AddWithValue("@DataDB", CurrentBranch.AppDB);
AppConnection.InitialCatalog = CurrentBranch.AppDB;
Modules.AddRange(SqlQueries.DataQuery2(cmd, AppConnection.ConnectionString));
查询:
select 1 [GL], (select isnull(COUNT(*),0) from @DataDB..APSetup) [AP], (select isnull(COUNT(*),0) from @DataDB..ARSetup) [AR], (select isnull(COUNT(*),0) from @DataDB..CASetup) [CA], case when (select isnull(COUNT(*),0) from @DataDB..SalesTax) > 0 then 1 else 0 end [TX], (select isnull(COUNT(*),0) from @DataDB..INSetup) [IN], (select isnull(COUNT(*),0) from @DataDB..POSetup) [PO], (select isnull(COUNT(*),0) from @DataDB..SOSetup) [SO], (select isnull(COUNT(*),0) from @DataDB..RQSetup) [RQ], (select COUNT(*) from @DataDB..pcsetup Where Setupid = 'PC' AND PERNBR <> '') [PJ] from @DataDB..glsetup
问题:
为什么变量替换不起作用?
附加信息:
问题不在于查询,当我手动将 @DataDB 替换为其预期值时,它可以正常工作。
问题是 C# SqlCommand.Parameters.AddWithValue();