【问题标题】:Why is this ignoring the where clause?为什么这忽略了where子句?
【发布时间】:2013-08-20 01:09:22
【问题描述】:

为什么在这段代码中忽略了 where 子句?它似乎忽略了更新中的 where 子句,这意味着每条记录都已被覆盖。我怎样才能解决这个问题?任何帮助将不胜感激。

namespace ResitAssignment2
{
    public partial class HomeCareVisitEddit : Form
    {
        public HomeCareVisitEddit()
        {
            InitializeComponent();
        }

        private void SubmitHCVA_Click(object sender, EventArgs e)
        {
            SqlConnection a = Database.GetConnection();
            a.Open();

            string sqltext;
            sqltext = @"update HomeCareVisit set
             PatientNo=@PatientNo,
             FurtherVisitRequired=@FurtherVisitRequired,
             AdvisoryNotes=@AdvisoryNotes,
             Prescription=@Prescription,
             TreatmentProvided=@TreatmentProvided,
             ActualVisitDateTime=@ActualVisitDateTime,
             Priority=@Priority,
             ScheduledDateTime=@ScheduledDateTime,
             TreatmentInstructions=@TreatmentInstructions,
             MedicalStaffID=@MedicalStaffID
              WHERE
             VisitRefNo=VisitRefNo";

            SqlCommand command = new SqlCommand(sqltext, a);

            try
            {
                using (a)
                {
                    command.Parameters.AddWithValue("@PatientNo", PatientNo.Text);
                    command.Parameters.AddWithValue("@FurtherVisitRequired", FurtherVisitRequired.Text);
                    command.Parameters.AddWithValue("@AdvisoryNotes", AdvisoryNotes.Text);
                    command.Parameters.AddWithValue("@Prescription", Prescription.Text);
                    command.Parameters.AddWithValue("@TreatmentProvided", TreatmentProvided.Text);
                    command.Parameters.AddWithValue("@ActualVisitDateTime",SqlDbType.DateTime );
                    {
                        DateTime.Parse(ActualVisitDateTime.Text);
                    };
                    command.Parameters.AddWithValue("@Priority", Priority.Text);
                    command.Parameters.AddWithValue("@ScheduledDateTime",SqlDbType.DateTime );
                    {
                        DateTime.Parse(ScheduledDateTime.Text);
                    };

                    command.Parameters.AddWithValue("@TreatmentInstructions", TreatmentInstructions.Text);
                    command.Parameters.AddWithValue("@MedicalStaffID", MedicalStaffID.Text);
                    command.Parameters.AddWithValue("@VisitRefNo", VisitRefNo.Text);
                    command.ExecuteNonQuery();

                    MessageBox.Show("Record altered");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            a.Close();
        }

        private void HomeCareVisitEddit_Load(object sender, EventArgs e)
        {
            SqlConnection a = Database.GetConnection();
            a.Open();

            string sqlText = "select * from HomeCareVisit where VisitRefNo =" + VisitRefNo;
            SqlCommand command = new SqlCommand(sqlText, a);
            SqlDataReader HomeCareVisitData = command.ExecuteReader();

            while (HomeCareVisitData.Read())
            {
                //DateTime actual = DateTime.Parse("ActualVisitDateTime");
                //DateTime scheduled = DateTime.Parse("ActualVisitDateTieme");
                PatientNo.Text = HomeCareVisitData["PatientNo"].ToString();
                FurtherVisitRequired.Text = HomeCareVisitData["FurtherVisitRequired"].ToString();
                AdvisoryNotes.Text = HomeCareVisitData["AdvisoryNotes"].ToString();
                Prescription.Text = HomeCareVisitData["Prescription"].ToString();
                TreatmentProvided.Text = HomeCareVisitData["TreatmentProvided"].ToString();
                ActualVisitDateTime.Text = HomeCareVisitData["ActualVisitDateTime"].ToString();
                Priority.Text = HomeCareVisitData["Priority"].ToString();
                ScheduledDateTime.Text = HomeCareVisitData["ScheduledDateTime"].ToString();
                TreatmentInstructions.Text = HomeCareVisitData["TreatmentInstructions"].ToString();
                MedicalStaffID.Text = HomeCareVisitData["MedicalStaffID"].ToString();
             }
             a.Close();
         }
     }
}

【问题讨论】:

  • 建议:你可以在不影响实际问题的情况下剪掉很多字段分配,让你的问题更容易阅读。

标签: c# sql ado.net


【解决方案1】:

WHERE VisitRefNo=VisitRefNo"; 应该是WHERE VisitRefNo=@VisitRefNo";

【讨论】:

    【解决方案2】:
    WHERE VisitRefNo=VisitRefNo
    

    应该是

    WHERE VisitRefNo=@VisitRefNo
    

    【讨论】:

    • 感谢您的帮助。有时,眼睛很容易忽略这样一个小错误,而这会带来很大的不同。
    • 这就是为什么减小示例的大小是一种非常有用的问题解决技术。
    猜你喜欢
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2015-01-31
    • 2014-03-22
    • 1970-01-01
    相关资源
    最近更新 更多