C# asynch SQL inside singleton and delegates

前端 未结 1 1532
迷失自我
迷失自我 2021-01-21 11:07

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

相关标签:
1条回答
  • 2021-01-21 11:41
    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+")");
    }
    
    0 讨论(0)
提交回复
热议问题