我在winform的应用程序中有这样的函数
public DataTable GetAllPrimaryKeyTables(string ConnectionString)
{
// Create the datatable
DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames");
// 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(sCommand);
EventLog objLog = new EventLog("Check1");
objLog.Source = "MFDBAnalyser";
objLog.WriteEntry("check1 successful: " + selectPrimaryKeyTables);
// 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);
// sw.WriteLine();
// 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 dtListOfPrimaryKeyTables;
}
并称之为
public DataTable RunAnalysis(string ConnectionString)
{
return GetAllPrimaryKeyTables(ConnectionString);
}
关于按钮的调试,请单击此处,我无法获取DataTable ...它只返回一个空的DataTable ...
private void btnStartAnalysis_Click(object sender, EventArgs e)
{
SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
objConnectionString.DataSource = txtHost.Text;
objConnectionString.UserID = txtUsername.Text;
objConnectionString.Password = txtPassword.Text;
string[] arrArgs = { objConnectionString.ConnectionString };
string assemblyName = "PrimaryKeyChecker.dll";
Assembly assembly = Assembly.LoadFrom(assemblyName);
Type local_type = assembly.GetType("PrimaryKeyChecker.PrimaryKeyChecker");
IMFDBAnalyserPlugin analyser = (IMFDBAnalyserPlugin) Activator.CreateInstance(local_type);
DataTable response = analyser.RunAnalysis(objConnectionString.ConnectionString);
在DataTable响应中,我应该得到第一个函数调用的表列表,但为什么我得到一个空的DataTable。
希望有人能帮助我并解决问题所在!!