“该名称在当前上下文中不存在”,C#

它说我的返回值在当前上下文中不存在(accessHelper),我不知道问题是什么?


额外的问题,有人能告诉我“new”关键字的作用吗?我看过的所有教程都在那里,但没有解释它的作用。


public AccessHelper GetAccessHelper(int id)

{

    using (SqlConnection sqlCon = new SqlConnection("Data Source = QL01; Initial Catalog = SCAM; Integrated Security = True"))

    {

        var accessHelper = sqlCon.Query<AccessHelper>("getAccessHelper", new { id }, commandType: System.Data.CommandType.StoredProcedure);

    }

    return accessHelper;   

}


叮当猫咪
浏览 248回答 1
1回答

明月笑刀无情

您的变量是在另一个范围内声明的,您无法从上层范围访问它。该new关键字初始化集合与变量id里面,至于new SqlConnection它会调用构造函数创建一个SqlConnection使用给定参数的对象。public AccessHelper GetAccessHelper(int id){&nbsp; &nbsp; using (SqlConnection sqlCon = new SqlConnection("Data Source = QL01; Initial Catalog = SCAM; Integrated Security = True"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var accessHelper = sqlCon.Query<AccessHelper>("getAccessHelper", new { id }, commandType: System.Data.CommandType.StoredProcedure);&nbsp; &nbsp; &nbsp; &nbsp; return accessHelper;&nbsp;&nbsp;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP