2010-12-07 135 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(...);