内单和委托C#非同步SQL [英] C# asynch SQL inside singleton and delegates
问题描述
我试图建立一个类,将负责对数据库中的所有操作。
从这里使用Singleton模式:<一href=\"http://$c$cbender.denniland.com/a-singleton-base-class-to-implement-the-singleton-pattern-in-c/\" rel=\"nofollow\">http://$c$cbender.denniland.com/a-singleton-base-class-to-implement-the-singleton-pattern-in-c/
我已经建立像这样的类:
类DB:SingletonBase&LT;&DB GT;
{ 公共静态只读字符串SqlConnectionString = @数据源= MYDB;初始目录=产品;集成安全性= TRUE;异步处理=真;; 私人DB()
{
} 公共无效loadData()
{
康涅狄格州的SqlConnection =新的SqlConnection(SqlConnectionString);
的SqlCommand CMD = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText =STATISTICS_1; cmd.Connection =康恩;
conn.Open();
IAsyncResult的结果= cmd.BeginExecuteReader(新的AsyncCallback(HandleCallback),CMD,CommandBehavior.CloseConnection);
} 私人无效HandleCallback(IAsyncResult的结果)
{
SqlDataReader的博士;
的SqlCommand _this =(的SqlCommand)result.AsyncState; 如果(result.IsCompleted)
{
博士= _this.EndExecuteReader(结果);
}
其他
博士= NULL; DataTable的DT =新的DataTable();
dt.Load(DR);
dr.Close();
MessageBox.Show(装);
}
}
在我的主类,我用这个像这样:
私人无效loadStatistics(对象发件人,EventArgs的发送)
{
showStatus(加载数据);
DB.Instance.loadData();
}
但是,这会给我只有我的消息框。
我想这样做是我,将SQL查询返回的东西后调用主类声明的功能。
我认为最好的办法就是用事件,但我不知道该怎么做适当的方式。
我愿做这样的事情在我的主类:
私人无效loadCompleted(string信息)
{
MessageBox.Show(MSG);
} 私人无效loadStatistics(对象发件人,EventArgs的发送)
{
showStatus(加载数据);
DB.Instance.loadData(loadCompleted);
}
所以,我可以指定函数,将SQL调用完成后调用。
我不知道这是不是这样做的最佳方式,因此任何意见,建议和解决方案都欢迎。
我想实现是有负责调用SQL和异步数据传递给将处理它的其它功能类。
公共委托无效NotifyCallback(字符串消息);公共类ClassWithCommandAndCallback
{
公众的SqlCommand的Sql;
公共NotifyCallback回调;
}公共无效loadData(NotifyCallback回调)
{
ClassWithCommandAndCallback AR =新ClassWithCommandAndCallback();
ar.Sql = CMD;
ar.Callback =回调;
IAsyncResult的结果= cmd.BeginExecuteReader(新的AsyncCallback(HandleCallback),AR,CommandBehavior.CloseConnection);
}私人无效HandleCallback(IAsyncResult的结果)
{
ClassWithCommandAndCallback AR =(ClassWithCommandAndCallback)result.AsyncState; ar.Callback(装(SQL是:+ ar.Sql +));
}
I'm trying to build a class that will be responsible for all operations on database.
Using singleton pattern from here: http://codebender.denniland.com/a-singleton-base-class-to-implement-the-singleton-pattern-in-c/
I've build a class like so:
class DB : SingletonBase<DB>
{
public static readonly string SqlConnectionString = @"Data Source=MYDB;Initial Catalog=PRODUCTS;Integrated Security=True;Asynchronous Processing=true;";
private DB()
{
}
public void loadData()
{
SqlConnection conn = new SqlConnection(SqlConnectionString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "STATISTICS_1";
cmd.Connection = conn;
conn.Open();
IAsyncResult result = cmd.BeginExecuteReader(new AsyncCallback(HandleCallback), cmd, CommandBehavior.CloseConnection);
}
private void HandleCallback(IAsyncResult result)
{
SqlDataReader dr;
SqlCommand _this = (SqlCommand)result.AsyncState;
if (result.IsCompleted)
{
dr = _this.EndExecuteReader(result);
}
else
dr = null;
DataTable dt = new DataTable();
dt.Load(dr);
dr.Close();
MessageBox.Show("loaded");
}
}
In my main class I'm using this like so:
private void loadStatistics(object sender, EventArgs e)
{
showStatus("loading data");
DB.Instance.loadData();
}
But this will give me only my message box.
What I would like to do is to declare function in my main class that will be called after SQL query return something.
I think that the best way would by using events, but I don't know how to do that proper way.
I would like to do something like this in my main class:
private void loadCompleted(string msg)
{
MessageBox.Show(msg);
}
private void loadStatistics(object sender, EventArgs e)
{
showStatus("loading data");
DB.Instance.loadData(loadCompleted);
}
So that I can specify function that will be called after SQL call is finished.
I don't know if this is the best way of doing this, so any comments, suggestions and solutions are welcome.
What I would like to achieve is to have one class responsible for calling SQL asynchronously and passing data to other functions that will process it.
public delegate void NotifyCallback(string message);
public class ClassWithCommandAndCallback
{
public SqlCommand Sql;
public NotifyCallback Callback;
}
public void loadData(NotifyCallback callback)
{
ClassWithCommandAndCallback ar = new ClassWithCommandAndCallback();
ar.Sql = cmd;
ar.Callback = callback;
IAsyncResult result = cmd.BeginExecuteReader(new AsyncCallback(HandleCallback), ar, CommandBehavior.CloseConnection);
}
private void HandleCallback(IAsyncResult result)
{
ClassWithCommandAndCallback ar = (ClassWithCommandAndCallback)result.AsyncState;
ar.Callback("loaded (SQL was: "+ar.Sql+")");
}
这篇关于内单和委托C#非同步SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!