Control table

I have a DataSet with a QueriesTableAdapter. To control SqlCommand.CommandTimeout, I added a partial class called QueriesTableAdapter with the public ChangeTimeout method.

partial class QueriesTableAdapter { public void ChangeTimeout(int timeout) { foreach (System.Data.SqlClient.SqlCommand cmd in CommandCollection) { cmd.CommandTimeout = timeout; } } } 

For each DataSet, I have a QueriesTableAdapter package, I can set CommandTimeout before execution.

 using (NameSpace.DataSet.DataSetTableAdapters.QueriesTableAdapter ta = new NameSpace.DataSet.DataSetTableAdapters.QueriesTableAdapter()) { ta.ChangeTimeout(3600); ta.DoSomething(); } 

This works well in most cases because the "QueriesTableAdapter" is named for you in the DataSet designer. The problem I am facing is TableAdapters, which are uniquely named. For example, if I have a DataTable called Person and a TableAdaper called PersonTableAdapter, I have to write a partial PersonTableAdapter class in the same way as I wrote the QueriesTableAdaper class. I have hundreds of DataTables with unique TableAdapter names. I do not want to create a partial class for each of them. How can I get to the base objects of the SqlCommand partial class in a global way?

+4
source share
4 answers

for some reason my .selectcommand adapter was null, so I had to go through the CommandCollection object, so I thought I would post a small change based on the previous answer above.

includes:

 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; } 
+11
source

All created TableAdapters inherit from Component. Therefore, you can write a method that uses reflection to retrieve the adapter property:

  private void ChangeTimeout(Component component, int timeout) { if (!component.GetType().Name.Contains("TableAdapter")) { return; } PropertyInfo adapterProp = component.GetType().GetProperty("Adapter", BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance); if (adapterProp == null) { return; } SqlDataAdapter adapter = adapterProp.GetValue(component, null) as SqlDataAdapter; if (adapter == null) { return; } adapter.SelectCommand.CommandTimeout = timeout; } 

Then you can call it like this:

 MyTableAdapter ta = new MyTableAdapter(); this.ChangeTimeout(ta,1000); 

I assume that since you are using a typed DataSet, you are still in .NET 2.0, so I did not do this with an extension method.

+3
source

BFree and similar solutions work well with reflection. The following is a small clarification, which I think gives a cleaner code.

You can also change the base class that uses the TableAdapter in the DataSet designer. You can change the base class of the TableAdapter to MyTableAdapterBaseClass or similarly to provide the necessary functionality. You can quickly make this change on all of your table adapters by running Find in Files and replacing them with DataSets .xsd files.

Instead of the BFree method for the caller with the signature:

 private void ChangeTimeout(Component component, int timeout) 

you can then create a method in the base class TableAdapter of the called party with a signature:

 public void ChangeTimeout(int timeout) 
+1
source

I tried both options and gave some problems. In the 1st answer, which namespace should be imported / used for the CommandCollection object on the 2ns.SelectCommand response adapter returns a null value

0
source

Source: https://habr.com/ru/post/1286017/


All Articles