C# 如何更改表适配器的命令超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1192171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the table adapter's command timeout
提问by cagin
I'm using Visual Studio 2008 with C#.
我在 C# 中使用 Visual Studio 2008。
I have a .xsd file and it has a table adapter. I want to change the table adapter's command timeout.
我有一个 .xsd 文件,它有一个表适配器。我想更改表适配器的命令超时。
Thanks for your help.
谢谢你的帮助。
采纳答案by timh
I have investigated this issue a bit today and come up with the following solution based on a few sources. The idea is to create a base class for the table adapter too inherit which increases the timeout for all commands in the table adapter without having to rewrite too much existing code. It has to use reflection since the generated table adapters don't inherit anything useful. It exposes a public function to alter the timeout if you want to delete what i used in the constructor and use that.
我今天调查了这个问题,并根据一些来源提出了以下解决方案。这个想法是为表适配器创建一个基类也继承,这增加了表适配器中所有命令的超时,而不必重写太多现有代码。它必须使用反射,因为生成的表适配器不继承任何有用的东西。如果您想删除我在构造函数中使用的内容并使用它,它会公开一个公共函数来更改超时。
using System;
using System.Data.SqlClient;
using System.Reflection;
namespace CSP
{
public class TableAdapterBase : System.ComponentModel.Component
{
public TableAdapterBase()
{
SetCommandTimeout(GetConnection().ConnectionTimeout);
}
public void SetCommandTimeout(int Timeout)
{
foreach (var c in SelectCommand())
c.CommandTimeout = Timeout;
}
private System.Data.SqlClient.SqlConnection GetConnection()
{
return GetProperty("Connection") as System.Data.SqlClient.SqlConnection;
}
private SqlCommand[] SelectCommand()
{
return GetProperty("CommandCollection") as SqlCommand[];
}
private Object GetProperty(String s)
{
return this.GetType().GetProperty(s, BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance).GetValue(this, null);
}
}
}
回答by Julian de Wit
Say your dataset is called MySET.
There is one table called MyTable
假设您的数据集名为 MySET。
有一张表叫做 MyTable
MySETTableAdapters.MyTableTableAdapter fAdapter =
new MySETTableAdapters.MyTableTableAdapter();
fAdapter.Adapter.SelectCommand.CommandTimeout = <fill inyour value here>;
回答by csl
In some cases you cannot access members like Adapterin your class, since they are defined as privateto the class.
在某些情况下,您无法访问类中的Adapter 等成员,因为它们被定义为类的私有成员。
Fortunately, the wizard will generate partial classes, which means you can extend them. As described in [this thread by Piebald][1], you can write your own property to set the timeout on select-commands.
幸运的是,向导会生成部分类,这意味着您可以扩展它们。如 [Piebald 的这个线程][1] 中所述,您可以编写自己的属性来设置选择命令的超时时间。
Generally, you would do this:
一般来说,你会这样做:
partial class FooTableAdapter
{
/**
* <summary>
* Set timeout in seconds for Select statements.
* </summary>
*/
public int SelectCommandTimeout
{
set
{
for ( int n=0; n < _commandCollection.Length; ++n )
if ( _commandCollection[n] != null )
((System.Data.SqlClient.SqlCommand)_commandCollection[n])
.commandTimeout = value;
}
}
}
Note that I have not actually tried this myself, but it seems like a viable solution.
请注意,我自己实际上并没有尝试过,但这似乎是一个可行的解决方案。
回答by Mitchell Gilman
With some small modifications csl's idea works great.
通过一些小的修改,csl 的想法效果很好。
partial class FooTableAdapter
{
/**
* <summary>
* Set timeout in seconds for Select statements.
* </summary>
*/
public int SelectCommandTimeout
{
set
{
for (int i = 0; i < this.CommandCollection.Length; i++)
if (this.CommandCollection[i] != null)
this.CommandCollection[i].CommandTimeout = value;
}
}
}
To use it, just set this.FooTableAdapter.CommandTimeout = 60; somewhere before the this.FooTableAdapter.Fill();
要使用它,只需设置 this.FooTableAdapter.CommandTimeout = 60; 在 this.FooTableAdapter.Fill() 之前的某个地方;
If you need to change the timeout on a lot of table adapters, you could create a generic extension method and have it use reflection to change the timeout.
如果您需要更改许多表适配器的超时,您可以创建一个通用扩展方法并让它使用反射来更改超时。
/// <summary>
/// Set the Select command timeout for a Table Adapter
/// </summary>
public static void TableAdapterCommandTimeout<T>(this T TableAdapter, int CommandTimeout) where T : global::System.ComponentModel.Component
{
foreach (var c in typeof(T).GetProperty("CommandCollection", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance).GetValue(TableAdapter, null) as System.Data.SqlClient.SqlCommand[])
c.CommandTimeout = CommandTimeout;
}
Usage:
用法:
this.FooTableAdapter.TableAdapterCommandTimeout(60);
this.FooTableAdapter.Fill(...);
This is a little slower. And there is the possibility of an error if you use it on the wrong type of object. (As far as I know, there is no "TableAdapter" class that you could limit it to.)
这有点慢。如果您在错误类型的对象上使用它,则可能会出错。(据我所知,没有您可以将其限制为的“TableAdapter”类。)
回答by Alex Russell
If you use a partial class, make you have the right namespace. Probably [your data set's name] + "TableAdapters'. Example:
如果您使用分部类,请确保您拥有正确的命名空间。可能是 [您的数据集名称] + "TableAdapters"。示例:
namespace MyProject.DataSet1TableAdapters
命名空间 MyProject.DataSet1TableAdapters
回答by user1365680
I had a couple of issues with using Mitchell Gilman's solution that I was eventually able to workaround.
我在使用 Mitchell Gilman 的解决方案时遇到了一些问题,我最终能够解决这些问题。
First of all, I needed to make sure to use the right namespace. It took me a while to figure out that Designer file for the xsd data set actually contains two namespaces, one for the data set in general and one for the table adapters. So the first thing is to note is that the namespace for the table adapter should be used, not for the data set in general.
首先,我需要确保使用正确的命名空间。我花了一段时间才发现 xsd 数据集的 Designer 文件实际上包含两个命名空间,一个用于一般数据集,另一个用于表适配器。所以首先要注意的是,应该使用表适配器的命名空间,而不是一般的数据集。
Secondly, the commandcollection may not always be initialized when the timeout command is used for the first time. To work around this, I called the InitCommandCollection command if this was the case.
其次,当第一次使用 timeout 命令时,commandcollection 可能并不总是被初始化。为了解决这个问题,如果是这种情况,我调用了 InitCommandCollection 命令。
So the adapted solution I used was
所以我使用的适应解决方案是
namespace xxx.xxxTableAdapters
partial class FooTableAdapter
{
/**
* <summary>
* Set timeout in seconds for Select statements.
* </summary>
*/
public int SelectCommandTimeout
{
set
{
if (this.CommandCollection == null)
this.InitCommandCollection();
for (int i = 0; i < this.CommandCollection.Length; i++)
if (this.CommandCollection[i] != null)
this.CommandCollection[i].CommandTimeout = value;
}
}
}
Hope that's helpful to people!
希望对大家有帮助!
回答by Chris
You can open up the Properties folder, open Settings.settings and alter the Timeout property of your connection string.
您可以打开 Properties 文件夹,打开 Settings.settings 并更改连接字符串的 Timeout 属性。
回答by atromgame
I do like this ; Right click Fill()
or GetX()
function and click Goto Defination
from menu.
我喜欢这个;右键单击Fill()
或GetX()
功能并Goto Defination
从菜单中单击。
You will see Source code of DATATABLE. And find ;
您将看到 DATATABLE 的源代码。并找到;
private global::System.Data.SqlClient.SqlCommand[] _commandCollection;
command line from your dataadapter class. And Change the private to public .
来自 dataadapter 类的命令行。并将 private 更改为 public 。
Now you can access the _commandCollection and you can change all attributes.
现在您可以访问 _commandCollection 并且您可以更改所有属性。
But be careful when you add or change any Filed form DESIGNER , the public will be private again by autogenerate system.
但是当您添加或更改任何 Filed 表单 DESIGNER 时要小心,自动生成系统将再次公开公共信息。
And also , when you finish to call Fill or Get Function you must reset _commandColleciton
calling this function ( InitCommandCollection()
)
而且,当您完成调用 Fill 或 Get Function 时,您必须重置_commandColleciton
调用此函数 ( InitCommandCollection()
)
public void InitCommandCollection() {}
This function is also private by autogen, you must change to public also!
这个功能也是autogen私有的,你也必须改成public!
Example:
例子:
dsIslemlerTableAdapters.tblIslemlerTableAdapter _t = new dsIslemlerTableAdapters.tblIslemlerTableAdapter();
dsIslemler.tblIslemlerDataTable _m = new dsIslemler.tblIslemlerDataTable();
_t._commandCollection[0].CommandText = "Select * From tblIslemler Where IslemTarihi>='' And IslemTarihi<=''";
_m = _t.GetData();
_t.InitCommandCollection();
回答by AniPol
Call ChangeTimeout Function by providing the TableAdapter and Time in seconds.
通过提供 TableAdapter 和以秒为单位的时间来调用 ChangeTimeout 函数。
this.ChangeTimeout(this.taTest, 500);
Function :
功能 :
private void ChangeTimeout(Component component, int timeout)
{
if (!component.GetType().FullName.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;
}
Interaction.command(0).CommandTimeout = timeout;
}
回答by Flo
Here's some example code from MSDN, using VB.NET:
这是来自MSDN的一些示例代码,使用 VB.NET:
Imports System.Data.SqlClient
Namespace MyDataSetTableAdapters
Partial Class CustomersTableAdapter
Public Sub SetCommandTimeOut(ByVal timeOut As Integer)
For Each command As SqlCommand In Me.CommandCollection
command.CommandTimeout = timeOut
Next
End Sub
End Class
End Namespace
When it comes time to call a long query, just call the SetCommandTimeOut method before the query:
当需要调用长查询时,只需在查询前调用 SetCommandTimeOut 方法:
Dim ds As New MyDataSet
Dim customersTA As New MyDataSetTableAdapters.CustomersTableAdapter
' Increase time-out to 60 seconds
customersTA.SetCommandTimeOut(60000)
' Do the slow query
customersTA.FillSlowQuery(ds.Customers)