2010-12-09 61 views
0

J'ai une fonction de type de retour datatableLes contrôles ne sont pas héritées dans WinForms

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

Et je l'appelle comme ça ...

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

Dans le IMFDBAnalyserPlugin j'ai

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

et sur le fond du projet principal j'ai

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

Mais quand je debug, l'objet de réponse est de retour que le datatable vide que je suis incapable de donner la source de données dans la première fonction, car il ne hérite pas de contrôle là ...

Espérer la question est en partie clair pour vous les gars .. Il devrait donner la liste de toutes les tables lors du débogage, mais il ne prend pas la grille de données dgResultView là pour lui donner une source de données ...

+2

Je ne vois pas où l'héritage de contrôle est pertinent ici du tout. Si vous placez des points d'arrêt dans la méthode RunAnalysis et que vous effectuez un débogage, que se passe-t-il? –

Répondre

1

Cela n'a rien à voir avec l'héritage; il semble que vous êtes en train d'attraper et de journaliser des 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); 
    } 

Je commencerais par regarder là; plus probablement quelque chose est de lancer, et vous dire pourquoi ... Si je devais deviner, il semble que peut-être vous manque:

sConnection.Open(); 

Le fait que quoi que ce soit est retourné me dit qu'il est juste une exception.

Personnellement j'aurais simplement laisser un non prévu bulle d'exception à l'interface utilisateur, et que l'interface utilisateur réagit en vous connectant et montrant la page #fail appropriée.

De plus, lorsque cela est possible, CAST le plug-in à l'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

Mais alors aussi .. la réponse DataTable ne donne pas la liste de toutes les tables – Srivastava

+0

que la fonction GetAllPrimaryKeyTables() génère – Srivastava

+0

@Srivastava - no; parce qu'il lance une exception. Mettez un point d'arrêt dans cette méthode et tracez-le. –