如何在 C# 中使用存储过程来 return 结果列表?
How to use stored procedure in C# to return a list of results?
这是我的存储过程:
CREATE Proc UpdateChecklist
(
@TemplateId As INT
) as
begin
select MF.CheckListDataId from TemplateModuleMap TM
inner join ModuleField MF
on TM.ModuleId = MF.ModuleId
where TM.TemplateId = @TemplateId and MF.CheckListDataId not in
(select cktm.CheckListDataId from ChecklistTemplateMap cktm
inner join ChecklistData ckd
on cktm.CheckListDataId = ckd.Id
where cktm.TemplateId = @TemplateId)
end
所以我希望这里有一个 returned 的 CheckListDataId
列表。我正在尝试使用 Database.ExecuteSqlCommand()
但尚未成功。我怎样才能 return 列表 CheckListDataId
在这里?我需要修改我的存储过程吗?我是 sql.
的新手
有什么建议吗?这是一个 ASP.NET MVC 5 项目
您的存储过程将 return 为您提供 结果集 并且您可以在 C# 中根据需要进行处理。
我会以这种方式从我的模型 class 内部调用过程:
DataTable loadLogFilterData = SQLHelper.ExecuteProc(STORED_PROCEDURE_NAME, new object[] {
//Parameters to Stored Proc If Any
});
然后我有一个 SQLHelper Class,我在其中创建了 SQL Connection 并具有调用存储过程的委托方法。
public static DataTable ExecuteProc(string procedureName, Object[] parameterList, string SQLConnectionString) // throws SystemException
{
DataTable outputDataTable;
using (SqlConnection sqlConnection = OpenSQLConnection(SQLConnectionString))
{
using (SqlCommand sqlCommand = new SqlCommand(procedureName, sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
if (parameterList != null)
{
for (int i = 0; i < parameterList.Length; i = i + 2)
{
string parameterName = parameterList[i].ToString();
object parameterValue = parameterList[i + 1];
sqlCommand.Parameters.Add(new SqlParameter(parameterName, parameterValue));
}
}
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataSet outputDataSet = new DataSet();
try
{
sqlDataAdapter.Fill(outputDataSet, "resultset");
}
catch (SystemException systemException)
{
// The source table is invalid.
throw systemException; // to be handled as appropriate by calling function
}
outputDataTable = outputDataSet.Tables["resultset"];
}
}
return outputDataTable;
}
您已将存储过程的每个输出都视为结果集,无论它包含什么。然后您需要在您的模型中操作该结果集以填充所需的数据结构和数据类型。
这是我的存储过程:
CREATE Proc UpdateChecklist
(
@TemplateId As INT
) as
begin
select MF.CheckListDataId from TemplateModuleMap TM
inner join ModuleField MF
on TM.ModuleId = MF.ModuleId
where TM.TemplateId = @TemplateId and MF.CheckListDataId not in
(select cktm.CheckListDataId from ChecklistTemplateMap cktm
inner join ChecklistData ckd
on cktm.CheckListDataId = ckd.Id
where cktm.TemplateId = @TemplateId)
end
所以我希望这里有一个 returned 的 CheckListDataId
列表。我正在尝试使用 Database.ExecuteSqlCommand()
但尚未成功。我怎样才能 return 列表 CheckListDataId
在这里?我需要修改我的存储过程吗?我是 sql.
有什么建议吗?这是一个 ASP.NET MVC 5 项目
您的存储过程将 return 为您提供 结果集 并且您可以在 C# 中根据需要进行处理。
我会以这种方式从我的模型 class 内部调用过程:
DataTable loadLogFilterData = SQLHelper.ExecuteProc(STORED_PROCEDURE_NAME, new object[] {
//Parameters to Stored Proc If Any
});
然后我有一个 SQLHelper Class,我在其中创建了 SQL Connection 并具有调用存储过程的委托方法。
public static DataTable ExecuteProc(string procedureName, Object[] parameterList, string SQLConnectionString) // throws SystemException
{
DataTable outputDataTable;
using (SqlConnection sqlConnection = OpenSQLConnection(SQLConnectionString))
{
using (SqlCommand sqlCommand = new SqlCommand(procedureName, sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
if (parameterList != null)
{
for (int i = 0; i < parameterList.Length; i = i + 2)
{
string parameterName = parameterList[i].ToString();
object parameterValue = parameterList[i + 1];
sqlCommand.Parameters.Add(new SqlParameter(parameterName, parameterValue));
}
}
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataSet outputDataSet = new DataSet();
try
{
sqlDataAdapter.Fill(outputDataSet, "resultset");
}
catch (SystemException systemException)
{
// The source table is invalid.
throw systemException; // to be handled as appropriate by calling function
}
outputDataTable = outputDataSet.Tables["resultset"];
}
}
return outputDataTable;
}
您已将存储过程的每个输出都视为结果集,无论它包含什么。然后您需要在您的模型中操作该结果集以填充所需的数据结构和数据类型。