2011-12-29 47 views
1

我想在WCF服務中異步連接到數據庫。但是,儘管在連接字符串設定爲「異步處理=真」的我越來越BeginExecuteReader WCF服務中的錯誤

System.InvalidOperationException有消息BeginExecuteReader: Connection property has not been initialized.

我使用連接到數據庫的代碼是:

public void Connect() 
{ 
    using (SqlConnection conn = new SqlConnection("Data Source=User12-PC; Initial Catalog = BMS; User Id=sa; Password = pass; Asynchronous Processing=true")) 
    { 
     SqlCommand command = new SqlCommand(); 
     command.CommandText = "Select l.currvalue from" + 
          " advt_ctrl_pts as p inner join advt_log_in_ctrl_pts l" + 
          " on p.registerid = l.regid and p.ddcid = l.ddcid" + 
          " where p.pointid = 5156102" + 
          " order by datetime"; 

     command.CommandType = CommandType.Text; 

     conn.Open(); 
     IAsyncResult result = command.BeginExecuteReader(); //This part is returning exception 
     if (result.IsCompleted) 
     { 
     timer = new Timer(new TimerCallback(onTimerTick), command.EndExecuteReader(result), 5000, 5000); 
     } 
    } 
} 

任何人都可以告訴我我在做什麼錯?

回答

3

你沒有指定連接命令:

SqlCommand command = new SqlCommand(); 
command.Connection = conn; 
+0

也可以用'新的SqlCommand( 「選擇...」,conn);在'等。 – 2013-02-22 08:42:40