2010-12-07 113 views
0

我在下面的类中定义的函数类似这样的命名空间的错误

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

但是当我调用这个像这样

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

然后抛出错误,如

错误1类型或命名空间名称 'PrimaryKeyChecker'找不到 (您是否错过了使用di rective或 组装 参考)d:\项目\ Mindfire \ GoalPlan \ MFDBAnalyser \ MFDBAnalyser \ MFDBAnalyser.cs 340 43 MFDBAnalyser

+1

学会格式代码在这里。没那么难。如果你不能在编辑器上看到010101按钮,代码块周围的“Ctrl + K”将会执行。 – Oded 2010-12-07 13:44:53

+0

好的,谢谢你会做 – Srivastava 2010-12-07 13:58:21

回答

2

你没有表现出什么using语句实际上是对GetPrimaryKeyTables()但你可以始终使用全名:

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

我怀疑自己可能拼错MFDBAnalyser

的一个实例
1

如果定义GetPrimaryKeyTables()方法不是在MFDBAnalyser命名空间中的类,您将需要在CLUDE using语句在该文件的顶部,这样的...

using MFDBAnalyser; 

或者,那么你可以使用PrimaryKeyChecker它的全名,像这样......

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