タスク:
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));
SQLクエリ:
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();です。