使用 Dapper 调用 SQL proc
Call a SQL proc with Dapper
我编写了以下方法来调用我的过程并将返回的数据放入数据集中。
public class GetDataFromProc
{
string constr;
public DataSet CallProcToDataSet(string procName)
{
DataSet ds = new DataSet();
constr = ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(procName))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(ds);
}
}
}
return ds;
}
我想要做的是利用 Dapper NuGet 包。通过文档检查,我可以看到示例 proc 调用行如下:
var user = cnn.Query<User>("spGetUser", new { Id = 1 }, commandType: CommandType.StoredProcedure).SingleOrDefault();
但是,我不确定将我的方法转换为上述示例的最佳方法是什么。有更多 Dapper 经验的人可以帮助我吗?
// Declare a model, with a property/field for every column
// that you care about from your Result
public class YourModel {
public int Id {get;set;}
// Whatever other columns your result has...
}
public class GetDataFromProc
{
string constr;
public IEnumerable<YourModel> CallProcToDataSet(string procName)
{
constr = ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
// If you expect only one row
var result = cnn.Query<YourModel>(procName, new { anyParametersYouHave = theirValue }, commandType: CommandType.StoredProcedure).SingleOrDefault();
// If you expect more than one row and want a collection
var results= cnn.Query<YourModel>(procName, new { anyParametersYouHave = theirValue }, commandType: CommandType.StoredProcedure).ToList();
}
}
}
我编写了以下方法来调用我的过程并将返回的数据放入数据集中。
public class GetDataFromProc
{
string constr;
public DataSet CallProcToDataSet(string procName)
{
DataSet ds = new DataSet();
constr = ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(procName))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(ds);
}
}
}
return ds;
}
我想要做的是利用 Dapper NuGet 包。通过文档检查,我可以看到示例 proc 调用行如下:
var user = cnn.Query<User>("spGetUser", new { Id = 1 }, commandType: CommandType.StoredProcedure).SingleOrDefault();
但是,我不确定将我的方法转换为上述示例的最佳方法是什么。有更多 Dapper 经验的人可以帮助我吗?
// Declare a model, with a property/field for every column
// that you care about from your Result
public class YourModel {
public int Id {get;set;}
// Whatever other columns your result has...
}
public class GetDataFromProc
{
string constr;
public IEnumerable<YourModel> CallProcToDataSet(string procName)
{
constr = ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
// If you expect only one row
var result = cnn.Query<YourModel>(procName, new { anyParametersYouHave = theirValue }, commandType: CommandType.StoredProcedure).SingleOrDefault();
// If you expect more than one row and want a collection
var results= cnn.Query<YourModel>(procName, new { anyParametersYouHave = theirValue }, commandType: CommandType.StoredProcedure).ToList();
}
}
}