Why both SqlDataAdaptor have to specify Table twice?
I am trying to Fill
my DataSet
with a Query in SqlDataAdaptor
. What I do not understand is why do I have to specifically st开发者_开发问答ate the Table name in Fill
, when I have already given to SqlDataAdapter
which Table to query.
See code below, there is already "FooTable" in the query string, why do I need "FooTable" again in SqlDataAdapter.Fill?
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FooTable", connectionString);
DataSet ds = new DataSet();
da.Fill(ds, "FooTable");
Actually, you don't have to do it in this case, since you have a single resultset you are dealing with. Here's a relevant excerpt from MSDN:
When the query specified returns multiple results, each result set is placed in a separate table. Additional result sets are named by appending integral values to the specified table name (for example, "Table", "Table1", "Table2", and so on). Since no table is created for a query that does not return rows, if you were to process an insert query followed by a select query, the table created for the select query would be named "Table", because it is the first table created. Applications using column and table names should ensure that conflicts with these naming patterns does not occur. The Fill method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. In such situations, Fill performs a case-sensitive comparison to find the corresponding table, and creates a new table if no exact match exists.
The above is taken from this page on MSDN's SqlDataAdaptor.Fill() docs.
So, if your query was a stored procedure or some other type of query yieding multiple result sets, you'd want to specify which resultset the Fill command is supposed to use. That's what the second parameter does. Otherwise, assumptions are made by the Fill command itself.
Usually, if you just do a select from a table, the resulset has the same name as the source table you are selecting from.
精彩评论