gpt4 book ai didi

c# - 像 DataAdapter 这样的 Dapper 命令

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:52 24 4
gpt4 key购买 nike

拜托,我可以将此方法转换为使用 Dapper 吗?

  • “BuscarUltimasLeituras”是一个程序
  • "@Last"是一个过程参数 (int)

public DataTable GetLeituras ( int ultimas )
{
DataTable listaLeiturasRomaneio = new DataTable();
try
{
_sqlConnection.Open();

_sqlCommand = new SqlCommand();
_sqlCommand.Connection = _sqlConnection;
_sqlCommand.CommandText = "BuscarUltimasLeituras";
_sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
_sqlCommand.Parameters.Add("@Last", SqlDbType.Int).Value = ultimas;
_sqlAdapter = new SqlDataAdapter(_sqlCommand);
_sqlAdapter.Fill(listaLeiturasRomaneio);
}
catch ( SqlException )
{
listaLeiturasRomaneio = null;
}
finally
{
_sqlConnection.Close();
}
return listaLeiturasRomaneio;
}

最佳答案

如果您仍然想要一个DataTable,您可以尝试:

var listaLeiturasRomaneio = new DataTable();
using (var reader = _sqlConnection.ExecuteReader(
"BuscarUltimasLeituras", new { Last = ultimas },
commandType: CommandType.StoredProcedure))
{
listaLeiturasRomaneio.Load(reader);
}

但是,更典型的用法是创建一个来匹配您的数据,然后:

var listaLeiturasRomaneio = _sqlConnection.Query<YourType>(
"BuscarUltimasLeituras", new { Last = ultimas },
commandType: CommandType.StoredProcedure).AsList();

请注意,dapper 还支持动态 用法,但这通常用于随意使用:

var listaLeiturasRomaneio = _sqlConnection.Query(
"BuscarUltimasLeituras", new { Last = ultimas },
commandType: CommandType.StoredProcedure).AsList();

关于c# - 像 DataAdapter 这样的 Dapper 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47772567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com