tableadapters issue with SelectCommandTimeout property c#
I want to increase the time to ret开发者_如何学Pythonrieve data from my tableadapter. How do i set it? I tried using this:
http://www.codeproject.com/KB/database/TableAdaptrCommandTimeout.aspx
However, the _commandCollection.Length is set to null therefore i am unable to set the CommandTimeout
Any ideas?
You have to call the GetData() Method on your tableAdapter before you can set the timeout, othewise the SelectCommand will not have been initialized.
protected void setAdapterTimeout(SqlDataAdapter da, int timeOut = 120)
{
if (da.SelectCommand != null)
da.SelectCommand.CommandTimeout = timeOut;
}
Then call it like this:
//Replacing AccessoryTableAdapter with your table Adapter
AccessoryTableAdapter ata = new AccessoryTableAdapter();
setAdapterTimeout(ata.Adapter);
EDIT: Extension Methods are cool!
public static class Extensions
{
public static void setAdapterTimeout(this SqlDataAdapter da, int timeOut = 120)
{
if (da.SelectCommand != null)
da.SelectCommand.CommandTimeout = timeOut;
if (da.InsertCommand != null)
da.InsertCommand.CommandTimeout = timeOut;
}
}
then call it:
AccessoryTableAdapter ata = new AccessoryTableAdapter();
ata.Adapter.setAdapterTimeout(120);
In my case this work correctly. The only think is adding this row of code at the original codeproject solution:
if ((this._commandCollection == null)) this.InitCommandCollection();
So, the SelectCommandTimeout propery become:
// Add this check before the assign step...
if ((this._commandCollection == null)) this.InitCommandCollection();
for (int i = 0; i < this._commandCollection.Length; i++)
{
if ((this._commandCollection[i] != null))
{
((System.Data.SqlClient.SqlCommand)
(this._commandCollection[i])).CommandTimeout = value;
}
}
}
}
精彩评论