How can I change the table adapter's command timeout

后端 未结 15 1467
遇见更好的自我
遇见更好的自我 2020-12-10 02:12

I\'m using Visual Studio 2008 with C#.

I have a .xsd file and it has a table adapter. I want to change the table adapter\'s command timeout.

Thanks for your

15条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 03:00

    This one is a bit old now and suspect this solution is not relevant to everyone, but I've ended up using AniPol's solution to override the ObjectDataSource control as follows:

    public class MyObjectDataSource : ObjectDataSource
    {
        public MyObjectDataSource()
        {
            this.ObjectCreated += this.MyObjectDataSource_ObjectCreated;
        }
    
        private void MyObjectDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
        {
            var objectDataSourceView = sender as ObjectDataSourceView;
            if (objectDataSourceView != null && objectDataSourceView.TypeName.EndsWith("TableAdapter"))
            {
                var adapter = e.ObjectInstance;
    
                PropertyInfo adapterProp = adapter.GetType()
                    .GetProperty(
                        "CommandCollection",
                        BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance);
                if (adapterProp == null)
                {
                    return;
                }
    
                SqlCommand[] commandCollection = adapterProp.GetValue(adapter, null) as SqlCommand[];
    
                if (commandCollection == null)
                {
                    return;
                }
    
                foreach (System.Data.SqlClient.SqlCommand cmd in commandCollection)
                {
                    cmd.CommandTimeout = 120;
                }
            }
        }
    }
    

提交回复
热议问题