请原谅我的英语不好,这是我的阅读障碍,写作有困难。
这就是我应该在我这边使用一些值的地方,当我尝试它时我会收集一些信息只会下载epayId并且它不会接收其他两个,
我做错了什么,因为它只会检索三个中的一个?
public string HentEpayIdFraPriser(int prisId)
{
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd1 = conn1.CreateCommand();
cmd1.Connection = conn1;
cmd1.CommandText = @"SELECT priser, epayId, mdr from Priser WHERE id = @id";
cmd1.Parameters.AddWithValue("@id", prisId);
conn1.Open();
SqlDataReader readerBrugerA = cmd1.ExecuteReader();
var epayId = "0";
var priser = "0";
var mdr = "0";
if (readerBrugerA.Read())
{
priser = readerBrugerA["priser"].ToString();
mdr = readerBrugerA["mdr"].ToString();
epayId = readerBrugerA["epayId"].ToString();
}
conn1.Close();
return epayId;
}
t可能是因为我没有回复其他两个值?
答案 0 :(得分:1)
我创建了一个可以传递的数据传输对象。实现可能看起来像这样。
public class PriserMdrEpayIdDto
{
public PriserMdrEpayIdDto(string priser, string mdr, string epayId)
{
Priser = priser;
Mdr = mdr;
EpayId = EpayId;
}
public string Priser { get; set; }
public string Mdr { get; set; }
public string EpayId { get; set; }
}
public PriserMdrEpayIdDto HentEpayIdFraPriser(int prisId)
{
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd1 = conn1.CreateCommand();
cmd1.Connection = conn1;
cmd1.CommandText = @"SELECT priser, epayId, mdr from Priser WHERE id = @id";
cmd1.Parameters.AddWithValue("@id", prisId);
conn1.Open();
SqlDataReader readerBrugerA = cmd1.ExecuteReader();
var epayId = "0";
var priser = "0";
var mdr = "0";
if (readerBrugerA.Read())
{
priser = readerBrugerA["priser"].ToString();
mdr = readerBrugerA["mdr"].ToString();
epayId = readerBrugerA["epayId"].ToString();
var result = new PriserMdrEpayIdDto(priser, mdr, epayId);
conn1.Close();
return result;
}
return null;
}