2010-12-07 6 views
0

J'ai défini une fonction dans la classe suivante comme celui-cierreur Namespace

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Configuration; 
using MFDBAnalyser; 
using System.Data; 
using System.Data.SqlClient; 
using System.Diagnostics; 
using System.IO; 

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

     /// <summary> 
     /// This function populates the tables with primary keys in a datagrid dgResultView. 
     /// </summary> 
     /// <param name="localServer"></param> 
     /// <param name="userName"></param> 
     /// <param name="password"></param> 
     /// <param name="selectedDatabase"></param> 
     /// <returns></returns> 
     public string GetAllPrimaryKeyTables(string ConnectionString) 
     { 
      string result = string.Empty; 

      // 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); 
        DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames"); 

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

Mais quand j'appelle cela comme ce

protected void GetPrimaryKeyTables() 
{ 
    DataTable dtPrimaryKeys = new PrimaryKeyChecker().GetAllPrimaryKeyTables(txtHost.Text, txtUsername.Text, txtPassword.Text, Convert.ToString(cmbDatabases.SelectedValue)); 
    dgResultView.DataSource = dtPrimaryKeys; 
} 

Ensuite, il jette des erreurs comme

erreur 1 Le type ou le nom de l'espace de noms 'PrimaryKeyChecker' est introuvable (Manque rective ou un ensemble référence) D: \ Projects \ Mindfire \ GoalPlan \ MFDBAnalyser \ MFDBAnalyser \ MFDBAnalyser.cs 340 43 MFDBAnalyser

+1

Apprenez à code de format ici. Pas si difficile. 'Ctrl + K' autour d'un bloc de code fera, si vous ne pouvez pas repérer le bouton 010101 sur l'éditeur. – Oded

+0

ok merci fera – Srivastava

Répondre

2

Vous ne présentez pas ce que les déclarations à l'aide sont en vigueur pour GetPrimaryKeyTables() mais vous pouvez toujours utiliser le nom complet:

DataTable dtPrimaryKeys = 
     new MFDBAnalyser.PrimaryKeyChecker().GetAllPrimaryKeyTables(...)); 

Je suppose que vous avez peut-être mal orthographié une instance de méthode MFDBAnalyser

1

Si la classe qui définit GetPrimaryKeyTables() ne sont pas dans l'espace de noms MFDBAnalyser, vous devrez en Clude using en haut de ce fichier, comme si ...

using MFDBAnalyser; 

Sinon, vous pouvez instancier PrimaryKeyChecker en utilisant son nom complet, comme si ...

DataTable dtPrimaryKeys = new PrimaryKeyChecker().GetAllPrimaryKeyTables(...); 
+0

mais j'ai fait la même chose !!! – Srivastava

+0

PrimaryKeyChecker est-il dans un projet différent? Si VS.Net n'a pas pu compiler ce projet, ou si le projet est désynchronisé, vous pouvez obtenir cette erreur. Stab dans l'obscurité. –