由于'oledbcommand'不包含'executenonqueryasync'的定义而没有扩展方法,因此收到错误 [英] Iam getting error as 'oledbcommand' does not contain a definition for 'executenonqueryasync' and no extension method

查看:60
本文介绍了由于'oledbcommand'不包含'executenonqueryasync'的定义而没有扩展方法,因此收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于'OleDbCommand'不包含'ExecuteNonQueryAsync'的定义,并且没有接受第一个类型'OleDbCommand'的扩展方法'ExecuteNonQueryAsync',因此找不到错误(你是否缺少using指令或程序集引用? )



即将推出的ExecuteNonQueryAsyncpre>和OpenAsyn



我尝试过:



<$ p $使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Data.OleDb;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

namespace ExcelElement
{
public class Staff
{
public int CustomerID {get;组; }
public string Name {get;组; }
public string Email {get;组; }
public string Size {get;组; }
public string Address {get;组; }
}
class ExcelDataService
{

OleDbConnection Conn;
OleDbCommand Cmd;


public ExcelDataService()
{
string ExcelFilePath = @C:\ Users \Pulztec-3 \Desktop\\book1.xlsx ;
string excelConnectionString = @Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ ExcelFilePath +; Extended Properties = Excel 12.0; Persist Security Info = True;
Conn = new OleDbConnection(excelConnectionString);
}

///< summary>
///从Excel获取所有记录的方法
///< / summary>
///< returns>< / returns>
///
public async任务< ObservableCollection< Staff>> ReadRecordFromEXCELAsync()


{

ObservableCollection< Staff> Staffs = new ObservableCollection< Staff>();


等待Conn.OpenAsync();
Cmd = new OleDbCommand();
Cmd.Connection = Conn;
Cmd.CommandText =从[Sheet1 $]中选择*;
var Reader = await Cmd.ExecuteReaderAsync();
while(Reader.Read())
{
Staffs.Add(new Staff()
{
CustomerID = Convert.ToInt32(Reader [CustomerID] ),
Name = Reader [Name]。ToString(),
Email = Reader [Email]。ToString(),
Size = Reader [Size]。ToString (),
地址= Reader [地址] .ToString()
});
}
Reader.Close();
Conn.Close();
返回员工;
}

///< summary>
///在Excel
/// S1中插入记录的方法。如果EmpNo = 0,则操作被跳过。
/// S2。如果学生已经存在,则需要更新
///< / summary>
///< param name =Emp>< / param>
公共异步任务< bool> ManageExcelRecordsAsync(工作人员)
{
bool IsSave = false;
if(staff.CustomerID!= 0)
{
await Conn.OpenAsync();
Cmd = new OleDbCommand();
Cmd.Connection = Conn;
Cmd.Parameters.AddWithValue(@ CustomerID,staff.CustomerID);
Cmd.Parameters.AddWithValue(@ Name,staff.Name);
Cmd.Parameters.AddWithValue(@ Email,staff.Email);
Cmd.Parameters.AddWithValue(@ Size,staff.Size);
Cmd.Parameters.AddWithValue(@ Address,staff.Address);

if(!IsStudentRecordExistAsync(staff).Result)
{
Cmd.CommandText =插入[Sheet1 $]值(@ CustomerID,@ Name,@ Email,@尺寸,@地址);
}
其他
{
Cmd.CommandText =更新[Sheet1 $]设置CustomerID = @ CustomerID,Name = @ Name,Email = @ Email,Size = @ Size,地址= @地址,其中CustomerID = @ CustomerID;

}
int result = await Cmd.ExecuteNonQueryAsync();
if(result> 0)
{
IsSave = true;
}
Conn.Close();
}
返回IsSave;

}
///< summary>
///检查记录是否已经可用的方法
///在工作组
///< / summary>
///< param name =emp>< / param>
///< returns>< / returns>
private async Task< bool> IsStudentRecordExistAsync(工作人员)
{
bool IsRecordExist = false;
Cmd.CommandText =从[Sheet1 $]中选择*,其中CustomerID = @ CustomerID;
var Reader = await Cmd.ExecuteReaderAsync();
if(Reader.HasRows)
{
IsRecordExist = true;
}

Reader.Close();
返回IsRecordExist;
}
}
}

解决方案

;
var Reader = await Cmd .ExecuteReaderAsync();
while(Reader.Read())
{
Staffs.Add(new Staff()
{
CustomerID = Convert.ToInt32(Reader [CustomerID]),
Name = Reader [Name]。ToString(),
Email = Reader [Email]。ToString(),
Size = Reader [大小] .ToString(),
地址=读者[地址] .ToString()
});
}
Reader.Close();
Conn.Close();
返回员工;
}

///< summary>
///在Excel中插入记录的方法
/// S1。如果EmpNo = 0,则操作被跳过。
/// S2。如果学生已经存在,那么它被用于U pdate
///< / summary>
///< param name =Emp>< / param>
公共异步任务< bool> ManageExcelRecordsAsync(工作人员)
{
bool IsSave = false;
if(staff.CustomerID!= 0)
{
await Conn.OpenAsync();
Cmd = new OleDbCommand();
Cmd.Connection = Conn;
Cmd.Parameters.AddWithValue(@ CustomerID,staff.CustomerID);
Cmd.Parameters.AddWithValue(@ Name,staff.Name);
Cmd.Parameters.AddWithValue(@ Email,staff.Email);
Cmd.Parameters.AddWithValue(@ Size,staff.Size);
Cmd.Parameters.AddWithValue(@ Address,staff.Address);

if(!IsStudentRecordExistAsync(staff).Result)
{
Cmd.CommandText =插入[Sheet1


值(@客户ID,@姓名,电子邮件@,@大小,@地址);
}
其他
{
Cmd.CommandText =更新[Sheet1


设置CustomerID = @ CustomerID,Name = @ Name,Email = @ Email,Size = @ Size,Address = @ Address其中CustomerID = @ CustomerID;

}
int result = await Cmd.ExecuteNonQueryAsync();
if(result> 0)
{
IsSave = true;
}
Conn.Close();
}
返回IsSave;

}
///< summary>
///检查记录是否已经可用的方法
///在工作组
///< / summary>
///< param name =emp>< / param>
///< returns>< / returns>
private async Task< bool> IsStudentRecordExistAsync(工作人员)
{
bool IsRecordExist = false;
Cmd.CommandText =从[Sheet1

中选择*

Iam getting error as 'OleDbCommand' does not contain a definition for 'ExecuteNonQueryAsync' and no extension method 'ExecuteNonQueryAsync' accepting a first argument of type 'OleDbCommand' could be found (are you missing a using directive or an assembly reference?)

Its coming for both ExecuteNonQueryAsyncpre> and OpenAsyn

What I have tried:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelElement
{
    public class Staff
    {
        public int CustomerID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Size { get; set; }
        public string Address { get; set; }
    }
    class ExcelDataService
    {

        OleDbConnection Conn;
        OleDbCommand Cmd;


        public ExcelDataService()
        {
            string ExcelFilePath = @"C:\Users\Pulztec-3\Desktop\\book1.xlsx";
            string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFilePath + ";Extended Properties=Excel 12.0;Persist Security Info=True";
            Conn = new OleDbConnection(excelConnectionString);
        }

        /// <summary>  
        /// Method to Get All the Records from Excel  
        /// </summary>  
        /// <returns></returns>  
        /// 
        public async Task<ObservableCollection<Staff>> ReadRecordFromEXCELAsync()


        {
            
        ObservableCollection<Staff> Staffs = new ObservableCollection<Staff>();
         

            await Conn.OpenAsync();
            Cmd = new OleDbCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "Select * from [Sheet1$]";
            var Reader = await Cmd.ExecuteReaderAsync();
            while (Reader.Read())
            {
                Staffs.Add(new Staff()
                {
                    CustomerID = Convert.ToInt32(Reader["CustomerID"]),
                    Name = Reader["Name"].ToString(),
                    Email = Reader["Email"].ToString(),
                    Size = Reader["Size"].ToString(),
                    Address = Reader["Address"].ToString()
                });
            }
            Reader.Close();
            Conn.Close();
            return Staffs;
        }

        /// <summary>  
        /// Method to Insert Record in the Excel  
        /// S1. If the EmpNo =0, then the Operation is Skipped.  
        /// S2. If the Student is already exist, then it is taken for Update  
        /// </summary>  
        /// <param name="Emp"></param>  
        public async Task<bool> ManageExcelRecordsAsync(Staff staff)
        {
            bool IsSave = false;
            if (staff.CustomerID != 0)
            {
                await Conn.OpenAsync();
                Cmd = new OleDbCommand();
                Cmd.Connection = Conn;
                Cmd.Parameters.AddWithValue("@CustomerID", staff.CustomerID);
                Cmd.Parameters.AddWithValue("@Name", staff.Name);
                Cmd.Parameters.AddWithValue("@Email", staff.Email);
                Cmd.Parameters.AddWithValue("@Size", staff.Size);
                Cmd.Parameters.AddWithValue("@Address", staff.Address);

                if (!IsStudentRecordExistAsync(staff).Result)
                {
                    Cmd.CommandText = "Insert into [Sheet1$] values (@CustomerID,@Name,@Email,@Size,@Address)";
                }
                else
                {
                    Cmd.CommandText = "Update [Sheet1$] set CustomerID=@CustomerID,Name=@Name,Email=@Email,Size=@Size,Address=@Address where CustomerID=@CustomerID";

                }
                int result = await Cmd.ExecuteNonQueryAsync();
                if (result > 0)
                {
                    IsSave = true;
                }
                Conn.Close();
            }
            return IsSave;

        }
        /// <summary>  
        /// The method to check if the record is already available   
        /// in the workgroup  
        /// </summary>  
        /// <param name="emp"></param>  
        /// <returns></returns>  
        private async Task<bool> IsStudentRecordExistAsync(Staff staff)
        {
            bool IsRecordExist = false;
            Cmd.CommandText = "Select * from [Sheet1$] where CustomerID=@CustomerID";
            var Reader = await Cmd.ExecuteReaderAsync();
            if (Reader.HasRows)
            {
                IsRecordExist = true;
            }

            Reader.Close();
            return IsRecordExist;
        }
    }
}

解决方案

"; var Reader = await Cmd.ExecuteReaderAsync(); while (Reader.Read()) { Staffs.Add(new Staff() { CustomerID = Convert.ToInt32(Reader["CustomerID"]), Name = Reader["Name"].ToString(), Email = Reader["Email"].ToString(), Size = Reader["Size"].ToString(), Address = Reader["Address"].ToString() }); } Reader.Close(); Conn.Close(); return Staffs; } /// <summary> /// Method to Insert Record in the Excel /// S1. If the EmpNo =0, then the Operation is Skipped. /// S2. If the Student is already exist, then it is taken for Update /// </summary> /// <param name="Emp"></param> public async Task<bool> ManageExcelRecordsAsync(Staff staff) { bool IsSave = false; if (staff.CustomerID != 0) { await Conn.OpenAsync(); Cmd = new OleDbCommand(); Cmd.Connection = Conn; Cmd.Parameters.AddWithValue("@CustomerID", staff.CustomerID); Cmd.Parameters.AddWithValue("@Name", staff.Name); Cmd.Parameters.AddWithValue("@Email", staff.Email); Cmd.Parameters.AddWithValue("@Size", staff.Size); Cmd.Parameters.AddWithValue("@Address", staff.Address); if (!IsStudentRecordExistAsync(staff).Result) { Cmd.CommandText = "Insert into [Sheet1


values (@CustomerID,@Name,@Email,@Size,@Address)"; } else { Cmd.CommandText = "Update [Sheet1


set CustomerID=@CustomerID,Name=@Name,Email=@Email,Size=@Size,Address=@Address where CustomerID=@CustomerID"; } int result = await Cmd.ExecuteNonQueryAsync(); if (result > 0) { IsSave = true; } Conn.Close(); } return IsSave; } /// <summary> /// The method to check if the record is already available /// in the workgroup /// </summary> /// <param name="emp"></param> /// <returns></returns> private async Task<bool> IsStudentRecordExistAsync(Staff staff) { bool IsRecordExist = false; Cmd.CommandText = "Select * from [Sheet1


这篇关于由于'oledbcommand'不包含'executenonqueryasync'的定义而没有扩展方法,因此收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆