控件不会在winforms中继承 [英] Controls are not inherited in winforms
问题描述
我有一个返回类型的功能datatable
I have a function of return type datatable
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(selectPrimaryKeyTables, sConnection);
// 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;
}
我正在这样调用...
And I am calling it like this...
public class PrimaryKeyChecker : IMFDBAnalyserPlugin
{
public DataTable RunAnalysis(string ConnectionString)
{
return GetAllPrimaryKeyTables(ConnectionString);
}
在IMFDBAnalyserPlugin中,我有
In the IMFDBAnalyserPlugin I have
namespace MFDBAnalyser
{
public interface IMFDBAnalyserPlugin
{
DataTable RunAnalysis(string ConnectionString);
}
在主项目背景下我有
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");
MethodInfo objMI = local_type.GetMethod("RunAnalysis");
ConstructorInfo ci = local_type.GetConstructor(Type.EmptyTypes);
object responder = ci.Invoke(null);
object response = objMI.Invoke(responder, arrArgs);
但是当我调试时,响应对象只返回空的datatable,因为我无法给出数据源在第一个函数中,因为它不是继承控件...
But when I debug, the response object is returning only the empty datatable as I am unable to give the datasource in the first function because it is not inheriting controls there...
希望这个问题对你们来说是部分清楚的..它应该给所有表的列表调试,但它没有采取datagrid dgResultView给它一个数据源...
Hope the question is partially clear to you guys.. It should give the list of all tables when debugged but it is not taking the datagrid dgResultView there to give it a datasource...
推荐答案
这与继承无关;看起来你正在捕捉和记录例外:
This has nothing to do with inheritance; it looks like you are catching and logging exceptions:
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);
}
最有可能的是扔,并告诉你为什么...如果我不得不猜测,看起来可能你错过了:
I would start by looking there; most likely something is throwing, and telling you why... If I had to guess, it looks like maybe you are missing:
sConnection.Open();
返回任何的事实告诉我这只是一个例外。
The fact that anything is returned tells me it is just an exception.
个人我只会让一个意外的异常气泡到UI,并让UI响应日志记录它显示适当的#fail页面。
Personally I would have just let an unanticipated exception bubble to the UI, and let the UI react by logging it and showing the appropriate #fail page.
此外,如果可能,将插件投射到界面:
Also, when possible, cast the plugin to the interface:
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);
这篇关于控件不会在winforms中继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!