C# WCF服务中的BeginExecuteReader错误
我正在尝试在WCF服务中异步连接到数据库。但尽管在连接字符串中设置了“Asynchronous Processing=true”,我还是得到了C# WCF服务中的BeginExecuteReader错误,c#,.net,ado.net,C#,.net,Ado.net,我正在尝试在WCF服务中异步连接到数据库。但尽管在连接字符串中设置了“Asynchronous Processing=true”,我还是得到了 System.InvalidOperationException带有消息BeginExecuteReader: 连接属性尚未初始化。 我用来连接数据库的代码是: public void Connect() { using (SqlConnection conn = new SqlConnection("Data Source=User12-PC;
System.InvalidOperationException
带有消息BeginExecuteReader:
连接属性尚未初始化。
我用来连接数据库的代码是:
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);
}
}
}
有人能告诉我我做错了什么吗?您没有将连接分配给命令:
SqlCommand command = new SqlCommand();
command.Connection = conn;