【问题标题】:Controls are not inherited in winforms控件在winforms中不被继承
【发布时间】:2010-12-09 06:24:40
【问题描述】:

我有一个返回类型数据表的函数

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;
}

我这样称呼它......

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

在 IMFDBAnalyserPlugin 我有

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

在我的主要项目背景上

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);

但是当我调试时,响应对象只返回空数据表,因为我无法在第一个函数中提供数据源,因为它没有继承那里的控件...

希望问题对你们来说是部分清楚的。它应该在调试时给出所有表的列表,但它没有将数据网格 dgResultView 带到那里给它一个数据源...

【问题讨论】:

  • 我看不出控制继承在哪里是相关的。如果在 RunAnalysis 方法中设置断点并通过它进行调试,会发生什么?

标签: c# visual-studio winforms debugging datatable


【解决方案1】:

这与继承无关;看起来您正在捕获并记录异常:

    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);
    }

我会先看看那里;很可能有东西在扔,并告诉你为什么......如果我不得不猜测,看起来你可能失踪了:

sConnection.Open();

返回 anything 的事实告诉我这只是一个例外。

个人我只会让一个未预料到的异常冒泡到 UI,然后让 UI 通过记录它并显示适当的#fail 页面来做出反应。

此外,如果可能,将插件强制转换为接口:

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);

【讨论】:

  • 但是也.. DataTable 响应没有给出所有列表
  • GetAllPrimaryKeyTables() 函数正在生成的表
  • @Srivastava - 不;因为它抛出异常。在此方法中放置一个断点并跟踪它。
  • @Srivastava - 它也可能和密码错误一样愚蠢,但在您检查您登录的异常之前,您不会知道
  • 输入字符串格式不正确是它在事件查看器中抛出的异常。
猜你喜欢
  • 1970-01-01
  • 2010-11-05
  • 2023-03-25
  • 2017-08-13
  • 2012-11-16
  • 2019-12-01
  • 2011-10-05
  • 2014-01-17
相关资源
最近更新 更多