问题描述
pre $ public ProjectsModel GetProjectListBySearch(int projectId)
{
try
{
using(_context = new Exo_ADBEntities())
{
var getdetailprojectlist = _context.Database.SqlQuery< ProjectsModel>( exec dbo。[GetProjectListByID] @ProjectID,新的SqlParameter(@ ProjectID,projectId))。FirstOrDefault();
return getdetailprojectlist;
}
}
catch(异常)
{
throw;
}
}
这很好,但是当我尝试做同样的事情有些事情,但传递两个参数我发现语法错误,称为无效参数请帮我做这个。当使用两个参数时,如下所示。
public List< ProjectsModel> GetProjectDetailsBySectorAndSubSector(int sectorid,int subsectorid)
{
try
{
using(_context = new Exo_ADBEntities())
{
var projectbysectorandsubsector = _context。 Database.SqlQuery< ProjectsModel>(exec dbo。[GetProjectDetailsBySectorAndSubSector] @ sectorId,@ subSectorId,新的SqlParameter(@ sectorId,@subSectorId,sectorid,subsectorid))ToList();
return projectbysectorandsubsector;
}
}
catch(异常)
{
throw;
}
}
问题是您尝试将多个参数作为单个对象传递。
如果您,看到这个:
public DbRawSqlQuery< TElement> SqlQuery< TElement>(
string sql,
params Object []参数
)
这太基本了,但是当您在C#函数中看到 params
时,它意味着任意数量的参数。这意味着您可以根据需要传递尽可能多的参数。所以,在你的情况下,这就像这样:
_context.Database.SqlQuery< ProjectsModel>(
exec dbo。[GetProjectDetailsBySectorAndSubSector] @ sectorId,@ subSectorId,
new SqlParameter(@ sectorId,sectorid),
new SqlParameter(@ subSectorId,subsectorid)
).ToList()
请注意,当您发现 params Object []
,你也可以传递数组而不是几个参数,即
_context.Database.SqlQuery< ProjectsModel>(
exec dbo。[GetProjectDetailsBySectorAndSubSector] @ sectorId,@ subSectorId,
new Object [] {new SqlParameter(@ sectorId,sectorid),
new SqlParameter(@ subSectorId,subsectorid)}
).ToList()
根据你的代码如何,这样可以更方便。
I can do it easily when I need to pass a single parameter as follows:
public ProjectsModel GetProjectListBySearch(int projectId)
{
try
{
using (_context = new Exo_ADBEntities())
{
var getdetailprojectlist = _context.Database.SqlQuery<ProjectsModel>("exec dbo.[GetProjectListByID] @ProjectID", new SqlParameter("@ProjectID", projectId)).FirstOrDefault();
return getdetailprojectlist;
}
}
catch (Exception)
{
throw;
}
}
This works nicely but when I try to do the same kind of thing but passing two parameters I find syntax error called invalid parameter Please help me to do this.Code when using two parameters are as follows
public List<ProjectsModel> GetProjectDetailsBySectorAndSubSector(int sectorid,int subsectorid)
{
try
{
using (_context = new Exo_ADBEntities())
{
var projectbysectorandsubsector = _context.Database.SqlQuery<ProjectsModel>("exec dbo.[GetProjectDetailsBySectorAndSubSector] @sectorId,@subSectorId", new SqlParameter("@sectorId, @subSectorId", sectorid, subsectorid)).ToList();
return projectbysectorandsubsector;
}
}
catch (Exception)
{
throw;
}
}
The problem is that you're trying to pass several parameters as a single object.
If you look at the signature of Database.SqlQuery
you'll see this:
public DbRawSqlQuery<TElement> SqlQuery<TElement>(
string sql,
params Object[] parameters
)
This is too basic, but, when you see params
in a C# function it means "any optional number of parameters". What this means is that you can pass as many parameters as needed. So, in your case, it's simply like this:
_context.Database.SqlQuery<ProjectsModel>(
"exec dbo.[GetProjectDetailsBySectorAndSubSector] @sectorId,@subSectorId",
new SqlParameter("@sectorId", sectorid),
new SqlParameter("@subSectorId", subsectorid)
).ToList()
Note that when you find params Object[]
, you can also pass an array instead of several params, i.e.
_context.Database.SqlQuery<ProjectsModel>(
"exec dbo.[GetProjectDetailsBySectorAndSubSector] @sectorId,@subSectorId",
new Object[] { new SqlParameter("@sectorId", sectorid),
new SqlParameter("@subSectorId", subsectorid)}
).ToList()
Depending on how your code looks like this can be more convenient.
这篇关于如何传递两个参数通过c#mvc函数调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!