-3

任务:

用必要的数据替换 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();

4

1 回答 1

6

原因是您无法参数化您尝试从中选择的数据库/表。你不能这样做:select * from @SomeTable你已经在 C# 中这样做了,只需这样做:

(为清楚起见剪断):

string sql = string.Format("select isnull(COUNT(*),0) from {0}..APSetup)...", CurrentBranch.AppDB);
于 2013-01-28T18:04:16.107 回答