Namespace error
I have defined a function in following class like this
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;
}
}
}
But when I am calling this like this
开发者_如何学编程protected void GetPrimaryKeyTables()
{
DataTable dtPrimaryKeys = new PrimaryKeyChecker().GetAllPrimaryKeyTables(txtHost.Text, txtUsername.Text, txtPassword.Text, Convert.ToString(cmbDatabases.SelectedValue));
dgResultView.DataSource = dtPrimaryKeys;
}
Then it is throwing errors like
Error 1 The type or namespace name 'PrimaryKeyChecker' could not be found (are you missing a using directive or an assembly reference?) D:\Projects\Mindfire\GoalPlan\MFDBAnalyser\MFDBAnalyser\MFDBAnalyser.cs 340 43 MFDBAnalyser
You didn't show what using statements are in effect for GetPrimaryKeyTables()
but you can always use the full name:
DataTable dtPrimaryKeys =
new MFDBAnalyser.PrimaryKeyChecker().GetAllPrimaryKeyTables(...));
I suspect you may have misspelled one instance of MFDBAnalyser
If the class that defines GetPrimaryKeyTables() method is not in the MFDBAnalyser namespace, you will need to include using statement at the top of that file, like so...
using MFDBAnalyser;
Alternatively, you could instantiate PrimaryKeyChecker using its full name, like so...
DataTable dtPrimaryKeys = new PrimaryKeyChecker().GetAllPrimaryKeyTables(...);
精彩评论