问题描述
我有一个QueriesTableAdapter一个DataSet。为了控制SqlCommand.CommandTimeout我也添加了QueriesTableAdapter一个名为ChangeTimeout的公共方法分部类。
I have a DataSet with a QueriesTableAdapter. In order to control the SqlCommand.CommandTimeout I've added a partial class called QueriesTableAdapter with a public method called ChangeTimeout.
partial class QueriesTableAdapter
{
public void ChangeTimeout(int timeout)
{
foreach (System.Data.SqlClient.SqlCommand cmd in CommandCollection)
{
cmd.CommandTimeout = timeout;
}
}
}
对于每一个数据集我有有一个QueriesTableAdapter,我可以将CommandTimeout执行之前设置。
For every DataSet I have that has a QueriesTableAdapter, I can set the CommandTimeout prior to executing.
using (NameSpace.DataSet.DataSetTableAdapters.QueriesTableAdapter ta =
new NameSpace.DataSet.DataSetTableAdapters.QueriesTableAdapter())
{
ta.ChangeTimeout(3600);
ta.DoSomething();
}
这工作得很好是大多数情况下,因为QueriesTableAdapter被命名为您DataSet设计器。我遇到的问题是,是唯一命名的TableAdapter。举例来说,如果我有一个名为Person的DataTable和一个名为PersonTableAdapter TableAdaper,我必须写一个PersonTableAdapter局部类以同样的方式我写了QueriesTableAdaper类。我有数百个数据表具有独特的TableAdapter的名称。我并不想创建一个部分类为每个的。我怎样才能得到一个局部类的底层的SqlCommand对象在一个全球性的方式?
This works well is most cases because the "QueriesTableAdapter" is named for you in the DataSet designer. The problem I'm running into is the TableAdapters that are uniquely named. For example, if I have a DataTable called Person and a TableAdaper called PersonTableAdapter, I have to write a PersonTableAdapter partial class in the same way I wrote the QueriesTableAdaper class. I have hundreds of DataTables with unique TableAdapter names. I don't want to create a partial class for each of those. How can I get to the underlying SqlCommand objects of a partial class in a global way?
推荐答案
由于某种原因,我的适配器。 SelectCommand中为空,所以我最后不得不去通过CommandCollection对象,而不是,所以我想我会后我的基于上述以前的答案小的变化。
for some reason, my adapter's .selectcommand was null so i ended up having to go through the CommandCollection object instead, so i thought i'd post my small change based on the previous answer above.
包括:
using System.ComponentModel;
using System.Reflection;
代码:
code:
private void ChangeTimeout(Component component, int timeout)
{
if (!component.GetType().Name.Contains("TableAdapter"))
{
return;
}
PropertyInfo adapterProp = component.GetType().GetProperty("CommandCollection", BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance);
if (adapterProp == null)
{
return;
}
SqlCommand[] command = adapterProp.GetValue(component, null) as SqlCommand[];
if (command == null)
{
return;
}
command[0].CommandTimeout = timeout;
}
这篇关于控制的TableAdapter命令超时全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!