问题描述
你好朋友,
我有下一个问题.
我有一个项目(DBConnecting.DataObjects.DataObjects),该项目是Windows窗体应用程序与Sql Server 2008数据库之间的通信.
在我的第二个项目中:一个Windows窗体(引用DBConnecting.DataObjects.DataObjects),该窗体调用Async表方法以异步加载数据表对象.
Hello Friends,
I have the next problem.
I have a project (DBConnecting.DataObjects.DataObjects) that is the communication between my windows form app and my Sql Server 2008 Database.
In my 2nd project: a Windows form (that references DBConnecting.DataObjects.DataObjects) that calls the Async table method to load a datatable object asynchronous.
string query = "select * from [dbo].[FactInternetSales]";
DBConnecting.DataObjects.DataObjects.AsyncDataTableStart(query, null, CommandType.Text, null);
在DBConnecting项目中,将调用AsyncDataTableStart,此方法将调用方法GetAsyncDataTableEnd
In the DBConnecting project the AsyncDataTableStart gets called and this method calls the method GetAsyncDataTableEnd
public static IAsyncResult AsyncDataTableStart(String Query, List<sqlparameter> sqlParameters, CommandType sqlCommandType, String ConnectionString)
{
SqlCommand command = Open(Query, sqlParameters, sqlCommandType, ConnectionString);
return command.BeginExecuteReader(GetAsyncDataTableEnd, command, CommandBehavior.CloseConnection);
}
public static void GetAsyncDataTableEnd(IAsyncResult myResult)
{
SqlCommand command = null;
SqlDataReader dr = null;
DateTime nu = DateTime.Now;
DataTable myTable = new DataTable();
try
{
command = (SqlCommand)myResult.AsyncState;
dr = command.EndExecuteReader(myResult);
myTable.Load(dr);
}
catch
{
}
finally
{
if (dr != null)
dr.Close();
if (command != null && command.Connection.State != ConnectionState.Closed)
command.Connection.Close();
}
}
</sqlparameter>
因此,我有一个装载了数据的DataTable对象,但是如何将该表返回到Windowsform?
在DBConnecting.DataObjects.DataObjects类中,我希望有一个委托,当Windows窗体实现此委托时,将返回数据表,以便可以将其设置为DataGridView的数据源.
请通过一些示例帮助我.
So I have a DataTable object loaded with data, but How do I return this table to the windowsform?
In the DBConnecting.DataObjects.DataObjects class I would like to have a delegate that when the windows form implements this delegate the datatable is returned so it can be set as DataSource for a DataGridView.
Please help me with some examples
推荐答案
public class DataTableEventArgs : System.EventArgs {
DataTable dataTable;
public DataTable DataTableInfo {
get {
return dataTable;
}
}
public DataTableEventArgs(DataTable dataTable) {
this.dataTable= dataTable;
}
}
在源项目中声明委托
Declare the delegate in your source project
public delegate void DataTableEventHandler(object sender, DataTableEventArgs e);
在返回DataTable的类中声明事件
Declare the event in the class that returns the DataTable
public event DataTableEventHandler ReturnDataTable;
引发您要发送DataTable的事件
Raise the event where you want to send the DataTable
if (ReturnDataTable != null) {
DataTable dataTable = BuildDataTable();
ReturnDataTable(this, dataTable);
}
现在,在您的接收器类中,该类必须接收DataTable
Now, in your receiver class that must receive the DataTable
private void Init() {
//Bind your method to the event
MyDataSource src = new MyDataSource();
src.ReturnDataTable += new DataTableEventHandler(HandleDataTable);
}
private void HandleDataTable(object sender, DataTableEventArgs e) {
DataTable dataTable = e.DataTableInfo;
//Use dataTable here
}
这篇关于1个解决方案中的2个项目的委托问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!