0

DataSet (XSD ファイル) 内に作成した QueriesTableAdapter 内の各クエリの接続文字列を更新しようとしています。リフレクションを介してこれを実行しようとするメソッドを作成しました (テーブル アダプターで動作する同様のメソッドがあります) が、クエリにアクセスできないようです。これが私が作った方法です:

        /// <summary>
        /// Changes the database all the queries in a queries table adapter connect to
        /// </summary>
        /// <typeparam name="IQA">The type of the queries table adapter</typeparam>
        /// <param name="InstanceQueriesAdapter">The instance of the queries table adapter</param>
        /// <param name="sqlDatabaseName">The name of the database the queries table adapter queries should connect to</param>
        public static void GetInstanceQueriesAdapter<IQA>(ref IQA InstanceQueriesAdapter, string sqlDatabaseName)
        {
            try
            {
                PropertyInfo qAdapterCommandCollection = InstanceQueriesAdapter.GetType().GetProperty("CommandCollection");

                if (qAdapterCommandCollection != null)
                {
                    SqlCommand[] qaCC = (SqlCommand[])qAdapterCommandCollection.GetValue(InstanceQueriesAdapter, null);

                    foreach (SqlCommand singleCommand in qaCC)
                    {
                        SqlConnection newSQLConnection = singleCommand.Connection;

                        SqlConnectionStringBuilder csBulider = new SqlConnectionStringBuilder(newSQLConnection.ConnectionString);

                        csBulider.InitialCatalog = sqlDatabaseName;

                        newSQLConnection.ConnectionString = csBulider.ConnectionString;

                        singleCommand.Connection = newSQLConnection;
                    }

                    qAdapterCommandCollection.SetValue(InstanceQueriesAdapter, qaCC, null);
                }
                else
                {
                    throw new Exception("Could not find command collection.");
                }
            }
            catch (Exception _exception)
            {
                throw new Exception(_exception.ToString());
            }
        }

GetProperty メソッドが null を返すため、失敗しています。ただし、ここでプロパティが存在することがわかります。

1

イミディエイト ウィンドウからの出力は次のとおりです。

? InstanceQueriesAdapter.GetType().GetProperties()
{System.Reflection.PropertyInfo[2]}
    [0]: {System.ComponentModel.ISite Site}
    [1]: {System.ComponentModel.IContainer Container}
? InstanceQueriesAdapter.GetType().GetMethods()
{System.Reflection.MethodInfo[14]}
    [0]: {System.Object ImportProcess(System.Object, System.Object, System.Object, System.Object, System.Object, System.Object, System.Nullable`1[System.Guid], System.String ByRef)}
    [1]: {Void add_Disposed(System.EventHandler)}
    [2]: {Void remove_Disposed(System.EventHandler)}
    [3]: {System.ComponentModel.ISite get_Site()}
    [4]: {Void set_Site(System.ComponentModel.ISite)}
    [5]: {Void Dispose()}
    [6]: {System.ComponentModel.IContainer get_Container()}
    [7]: {System.String ToString()}
    [8]: {System.Object GetLifetimeService()}
    [9]: {System.Object InitializeLifetimeService()}
    [10]: {System.Runtime.Remoting.ObjRef CreateObjRef(System.Type)}
    [11]: {Boolean Equals(System.Object)}
    [12]: {Int32 GetHashCode()}
    [13]: {System.Type GetType()}

接続を動的に変更できるように、この「CommandCollection」にアクセスする方法を知っている人はいますか?

4

2 に答える 2

0

データベースを変更する作業コード。タイムアウトなど、接続のすべての部分をここで変更できることにも注意してください。

/// <summary>
/// Changes the database all the queries in a queries table adapter connect to
/// </summary>
/// <typeparam name="IQA">The type of the queries table adapter</typeparam>
/// <param name="InstanceQueriesAdapter">The instance of the queries table adapter</param>
/// <param name="sqlDatabaseName">The name of the database the queries table adapter queries should connect to</param>
public static void GetInstanceQueriesAdapter<IQA>(ref IQA InstanceQueriesAdapter, string sqlDatabaseName)
{
    try
    {
        FieldInfo qAdapterCommandCollection = InstanceQueriesAdapter.GetType().GetField("_commandCollection", BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic);
        MethodInfo initCC = InstanceQueriesAdapter.GetType().GetMethod("InitCommandCollection", BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic);

        if (qAdapterCommandCollection != null && initCC != null)
        {
            initCC.Invoke(InstanceQueriesAdapter, null);

            IDbCommand[] qaCC = (IDbCommand[])qAdapterCommandCollection.GetValue(InstanceQueriesAdapter);

            foreach (SqlCommand singleCommand in qaCC)
            {
                SqlConnection newSQLConnection = singleCommand.Connection;

                SqlConnectionStringBuilder csBulider = new SqlConnectionStringBuilder(newSQLConnection.ConnectionString);

                csBulider.InitialCatalog = sqlDatabaseName;

                newSQLConnection.ConnectionString = csBulider.ConnectionString;

                singleCommand.Connection = newSQLConnection;
            }

            qAdapterCommandCollection.SetValue(InstanceQueriesAdapter, qaCC);
        }
        else
        {
            throw new Exception("Could not find command collection.");
        }
    }
    catch (Exception _exception)
    {
        throw new Exception(_exception.ToString());
    }
}
于 2013-02-22T10:47:38.747 に答える