我已经在下面这个类中定义了一个函数
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' (你错过了使用指令或 集会 参考?)D:\ Projects \ Mindfire \ GoalPlan \ MFDBAnalyser \ MFDBAnalyser \ MFDBAnalyser.cs 340 43 MFDBAnalyser
答案 0 :(得分:2)
您没有显示哪些使用语句对GetPrimaryKeyTables()
有效,但您始终可以使用全名:
DataTable dtPrimaryKeys =
new MFDBAnalyser.PrimaryKeyChecker().GetAllPrimaryKeyTables(...));
我怀疑你可能拼错了MFDBAnalyser的一个实例
答案 1 :(得分:1)
如果定义GetPrimaryKeyTables()方法的类不在MFDBAnalyser名称空间中,则需要在该文件的顶部包含using语句,如此...
using MFDBAnalyser;
或者,您可以使用其全名来实例化PrimaryKeyChecker,如此......
DataTable dtPrimaryKeys = new PrimaryKeyChecker().GetAllPrimaryKeyTables(...);