以下,目标是,若相同产品今年出成率,与去年同月的进行比较,若去年同月的没有,则从去年12月里面取,若还没有,则为空,以下方法是为了取12月的出成率数据,循环是为了将取得的数据,填入到数据表的对应字段并显示出来,但在实际运行中,取不到去年12月的数据,请给分析指导一下,哪里有问题呢,谢谢
private decimal GetRate(string proName) //此方法,是用来获得12月份产品的出成率;
{
string Conn = "server=192.168.100.247;database=WHMesInfo;user=sa;password=whyy@2021";
decimal DeceRate = 0;
string sqlstring = @"select rate from w_YirateMom where proName = @proName and dateInfo = '2023年12月'";
using (SqlConnection Connect = new SqlConnection(Conn))
using (SqlCommand Cmd = new SqlCommand(sqlstring, Connect))
{
Cmd.Parameters.AddWithValue("@proName", proName);
try
{
Connect.Open();
object result = Cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
DeceRate = Convert.ToDecimal(result);
}
}
catch (Exception ex)
{
MessageBox.Show("Error retrieving rate: " + ex.Message);
}
}
return DeceRate;
}
省略查询SQL的代码
foreach (DataRow row in dt.Rows)
{
decimal Lastrate = row["rate"] != DBNull.Value ? Convert.ToDecimal(row["rate"]) : 0;
if ((Lastrate == null) || (Lastrate == 0))
{
string proName = Convert.ToString(row["proName"]);
decimal lastDecerate = GetRate(proName);
if (lastDecerate != 0)
{
Lastrate = lastDecerate;
}
}
}