我需要检查用户是否选择更改消息,因为我需要通过传递ID来获取会员的状态和日期。但是我的结果集看起来总是返回空结果实例。如何更新属性? 有人可以告诉我我的代码出了什么问题。
型号:
public class SmsStatus
{
public string Status { get; set; }
public DateTime OptInDate { get; set; }
}
方法:
public static SmsStatusResult GetSmsStatus(string memberNumber)
{
var status = "";
var OptInDate = "";
var smsStatusResult = new SmsStatusResult();
if (!string.IsNullOrEmpty(memberNumber))
{
using (
var connection =
new OracleConnection(
ConfigurationManager.ConnectionStrings["OracleMemberConnectionString"].ConnectionString))
{
connection.Open();
using (
var command =
new OracleCommand(
"SELECT sms_status,sms_opt_in_date FROM member WHERE member_nbr = :memberNumber", connection))
{
command.Parameters.Add(
new OracleParameter(
"memberNumber",
OracleDbType.Varchar2,
memberNumber,
ParameterDirection.Input));
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
status = reader["sms_status"].ToString();
OptInDate = reader["sms_opt_in_date"].ToString();
}
}
}
}
}
return smsStatusResult;
}
答案 0 :(得分:1)
注意:此答案是在OP更改问题以解决此处列出的问题之前写的。
当前有两个问题:
memberNumber
,但是您正在创建名为OracleParameter
的{{1}} 您几乎肯定要执行它,然后使用结果来修改member_nbr
。如果查看smsStatusResult
,则说明您正在创建“空”实例,然后不对其进行任何更改。因此,是的,它将始终返回该“空”实例。
答案 1 :(得分:0)
您的参数名称错误。您必须这样将member_nbr
替换为memberNumber
using (var command = new OracleCommand(
"SELECT sms_status,sms_opt_in_date FROM member WHERE member_nbr = :memberNumber", connection))
{
command.Parameters.Add(
new OracleParameter(
"memberNumber",
OracleDbType.Varchar2,
memberNumber,
ParameterDirection.Input));
}
SmsStatus data;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
data = new SmsStatus();
data.status = reader["sms_status"].ToString();
data.OptInDate = reader["sms_opt_in_date"].ToString();
//add data to an array if there is multi rows in result
//or break if there is only one line
break;
}
}
return data;