在我的winform应用程序,我有以下情形:
In my winform application I have the following scenario:
我想在一个事件的多个表。返回所有表为数据集
在单个服务器周期,
或得到一个表的时间,并使用单独的服务器周期为每个表
哪一个更好?有什么好处一个比另一个?
I want to get multiple tables on a single event. Returning all tables as dataset
in single server cycle,
or getting one table at time and using separate server cycle for each table
which one is better? What are the advantages one over another?
通常的方式是让所有的一次。
The normal way is to get all at once.
只是构建你的 SELECT
的,你将有一个数据集
摆满了桌子。
just construct your SELECT
's and you will have a DataSet
filled with all tables.
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(myConnString))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
{
cmd.CommandText = "myMultipleTablesSP";
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
conn.Close();
}
}
例如,如果你回到2表中的SP,如:
if for example you return 2 tables in your SP, like:
SELECT * FROM [TableA];
SELECT * FROM [TableB];
您会访问该表为:
DataTable tableA = ds.Tables[0];
DataTable tableB = ds.Tables[1];