我有这段代码可以从数据库中获取数据
public async Task<DataTable> SelectData(string stored_procedure, SqlParameter[] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = stored_procedure;
sqlcmd.Connection = sqlconnection;
if (param != null)
{
sqlcmd.Parameters.AddRange(param);
}
SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
DataTable dt = new DataTable();
await Task.Run(()=> da.Fill(dt));
return dt;
}
并且我使用此代码运行存储过程
public async Task<DataTable> GetOrderManagementManagerEmail()
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DataTable dt = new DataTable();
dt =await DAL.SelectData("GetOrderManagementManagerEmail", null);
DAL.Close();
return dt;
}
然后我在单击按钮时使用此代码
private async void btnValidate_Click(object sender, EventArgs e)
{
int[] selectedRows = gridView2.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
DataRow rowGridView2 = (gridView2.GetRow(selectedRows[i]) as DataRowView).Row;
await stock.ValidateProjectNeed(Convert.ToInt32(rowGridView2["id"]), DateTime.Now);
}
if (XtraMessageBox.Show(Resources.addedSuccessfullyBonBesoinAndSendEmail, Resources.Validate, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
Recipients oRecips = oMsg.Recipients;
oMsg.To =await Task.Run(()=> stock.GetOrderManagementManagerEmail().Rows[0][0].ToString());
oMsg.Subject = "Bon Besoin " ;
oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
oMsg.Display(false); //In order to display it in modal inspector change the argument to true
oMsg.HTMLBody = "Un nouveau bon besoin a été ajouté " +
"<br />" + oMsg.HTMLBody; //Here comes your body;
}
gridControl2.DataSource = stock.GetProjectNeedsForValidate();
}
但是我在这行代码中出错
oMsg.To =await Task.Run(()=> stock.GetOrderManagementManagerEmail().Rows[0][0].ToString());
任务不包含“行”的定义,找不到可以接受的扩展方法“行”,接受类型为“任务”的第一个参数(您是否缺少using指令或程序集引用?)。 在使用异步之前,代码可以正常工作。谢谢。
答案 0 :(得分:7)
您要
var table = await stock.GetOrderManagementManagerEmail();
oMsg.To = table.Rows[0][0].ToString();
不确定await Task.Run()
发生了什么。您将Task
从对GetOrderManagementManagerEmail()
的调用中返回,然后尝试从任务中检索行,并用a ....
简化它。另外,看看是否真的需要将表/行与索引器一起使用。您可以使用:
var oMsg.To = table.AsEnumerable().Select(d => d.Field<string>("To")).FirstOrDefault();