开发者

Controls are not inherited in winforms

开发者 https://www.devze.com 2023-01-29 09:18 出处:网络
I have a function of return type datatable public DataTable GetAllPrimaryKeyTables(string ConnectionString)

I have a function of return type datatable

public DataTable GetAllPrimaryKeyTables(string ConnectionString)
{ 
    // Create the datatable 
    DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames");

    // Query to select primary key tables.
    string selectPrimaryKeyTables = @"SELECT 
                                           TABLE_NAME
                                          AS
                                           TABLES
                                        FROM 
                                           INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                                       WHERE 
                                           CONSTRAINT_TYPE = 'PRIMARY KEY'
                                         AND
                                           TABLE_NAME <> 'dtProperties'
                                    ORDER BY
                                           TABLE_NAME";

    // put your SqlConnection and SqlCommand into using blocks! 
    using(SqlConnection sConnection = new SqlConnection(ConnectionString))
    using(SqlCommand sCommand = new SqlCommand(selectPrimaryKeyTables, sConnection))
    {
        try
        {
            // Create the dataadapter object 
            SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeyTables, sConnection);


            // Fill the datatable - no need to open the connection, the SqlDataAdapter will do that all by itself  
            // (and also close it again after it is done) 
            sDataAdapter.Fill(dtListOfPrimaryKeyTables);
            //using(StringWriter sw = new StringWriter())
            //{
            //    dtListOfPrimaryKeyTables.WriteXml(sw);
            //    sw.WriteLine();
            //    result = sw.ToString();
            //}
        }
        catch(Exception ex)
        {
            //All the exceptions are handled and written in the EventLog. 
            EventLog log = new EventLog("Application");
            log.Source = "MFDBAnalyser";
            log.WriteEntry(ex.Message);
        }
    }

    // return the data table to the caller 
    return dtListOfPrimaryKeyTables;
}

And I am calling it like this...

public class PrimaryKeyChecker : IMFDBAnalyserPlugin
{
    public DataTable RunAnalysis(string ConnectionString)
    {
        return GetAllPrimaryKeyTables(ConnectionString);
    }

In the IMFDBAnalyserPlugin I have

namespace MFDBAnalyser
{
    public interface IMFDBAnalyserPlugin
    {
        DataTable RunAnalysis(string ConnectionString);
    }

and on the main project background I have

private void btnStartAnalysis_Click(object sender, EventArgs e)
{
    SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
    objConnectionString.DataSource = txtHost.Text;
    objConnectionString.UserID = txtUsername.Text;
    objConnectionString.Password = txtPassword.Text;
    string[] arrArgs = {objConnectionString.ConnectionString};

    string assemblyName = "PrimaryKeyChecker.dll";
    Assembly assembly = Assembly.LoadFrom(assemblyName);
    Type local_type = assembly.GetType("PrimaryKeyChecker.PrimaryKeyChecker");
    MethodInfo objMI = local_type.GetMethod("RunAnalysis");
    ConstructorInfo ci = local_type.GetConstructor(Type.EmptyTypes);
    object responder = ci.Invoke(null);
    object response = objMI.Invoke(responder, arrArgs);

But when I debug, the response object is returning only the empty datatable as I am unable to give the datasource in the first function because it is not inheriting controls there...

Hope the que开发者_StackOverflowstion is partially clear to you guys.. It should give the list of all tables when debugged but it is not taking the datagrid dgResultView there to give it a datasource...


This has nothing to do with inheritance; it looks like you are catching and logging exceptions:

    catch(Exception ex)
    {
        //All the exceptions are handled and written in the EventLog. 
        EventLog log = new EventLog("Application");
        log.Source = "MFDBAnalyser";
        log.WriteEntry(ex.Message);
    }

I would start by looking there; most likely something is throwing, and telling you why... If I had to guess, it looks like maybe you are missing:

sConnection.Open();

The fact that anything is returned tells me it is just an exception.

Personally I would have just let an unanticipated exception bubble to the UI, and let the UI react by logging it and showing the appropriate #fail page.

Also, when possible, cast the plugin to the interface:

string assemblyName = "PrimaryKeyChecker.dll";
Assembly assembly = Assembly.LoadFrom(assemblyName);
Type local_type = assembly.GetType("PrimaryKeyChecker.PrimaryKeyChecker");
IMFDBAnalyserPlugin analyser =
   (IMFDBAnalyserPlugin)Activator.CreateInstance(local_type);
DataTable response = analyser.RunAnalysis(objConnectionString.ConnectionString);
0

精彩评论

暂无评论...
验证码 换一张
取 消