简体   繁体   中英

Cannot resolve symbol 'parameters' when using SQLCommandBuilder

I'm trying to fetch a query from the database and I've done the following:

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;
                }
            }

However I'm receiving an error on the following lines:

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

That reads:

Cannot resolve symbol 'Parameters'

Where have I gone wrong here? I know the mistake is in where I've added the parameters - but I don't understand enough to know how to correct it.

The Parameters collection isn't accessible directly from SqlCommandBuilder .

Try replacing the two lines in question with these:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM