使用SQLCommandBuilder时无法解析符号'参数'

时间:2014-07-02 01:41:45

标签: c# sql tsql

我尝试从数据库中获取查询,并且我已完成以下操作:

public DataTable FetchTasks(string questName, string categoryName)
        {
            const string queryString = @"   SELECT  *
                                            FROM    dbo.Task as t
                                                    INNER JOIN dbo.[Quest] as q
                                                        on q.QuestID = t.QuestID
                                                    INNER JOIN dbo.[Category] as c
                                                        on c.CategoryID = t.CategoryID
                                            WHERE   q.[Description] = @questName
                                                    AND c.CategoryTitle = @categoryName";

            using (var dataAdapter = new SqlDataAdapter(queryString, ConnectionString))
            {
                using (var cmd = new SqlCommandBuilder(dataAdapter))
                {
                    cmd.Parameters.AddWithValue("@questName", questName);
                    cmd.Parameters.AddWithValue("@categoryName", categoryName);

                    var table = new DataTable { Locale = System.Globalization.CultureInfo.InvariantCulture };

                    dataAdapter.Fill(table);

                    return table;
                }
            }

但是我在以下几行收到错误:

         cmd.Parameters.AddWithValue("@questName", questName);
         cmd.Parameters.AddWithValue("@categoryName", categoryName);

那就是:

  

无法解析符号'参数'

我在哪里错了?我知道错误在于我添加了参数 - 但我不太了解如何纠正它。

1 个答案:

答案 0 :(得分:1)

Parameters集合无法直接从SqlCommandBuilder访问。

尝试用以下代码替换这两行:

cmd.DataAdapter.SelectCommand.Parameters.AddWithValue("@questName", questName);
cmd.DataAdapter.SelectCommand.Parameters.AddWithValue("@categoryName", categoryName);