期望参数未提供错误。

时间:2017-12-12 02:19:59

标签: c# sql-server

我正在使用此代码将数据添加到数据库中。

SqlConnection connection = MedicalDB.EstConnection();
        string selectStatement
            = "INSERT INTO PATIENT_TBL (PATIENT_ID, LAST_NAME, FIRST_NAME, " +
            "DATE_OF_BIRTH, ADDRESS_STREET, ADDRESS_CITY, ADDRESS_STATE, ADDRESS_ZIP, PHONE_HOME, " +
            "PHONE_MOBILE, PRIMARY_ID) VALUES (@PATIENT_ID, @LAST_NAME, @FIRST_NAME, " +
            "@DATE_OF_BIRTH, @ADDRESS_STREET, @ADDRESS_CITY, @ADDRESS_STATE, @ADDRESS_ZIP, @PHONE_HOME, " +
            "@PHONE_MOBILE, @PRIMARY_ID)";

        SqlCommand selectCommand =
        new SqlCommand(selectStatement, connection);

        selectCommand.Parameters.AddWithValue("@PATIENT_ID", newPatient.patientID);
        selectCommand.Parameters.AddWithValue("@LAST_NAME", newPatient.lastName);
        selectCommand.Parameters.AddWithValue("@FIRST_NAME", newPatient.firstName);
        selectCommand.Parameters.AddWithValue("@DATE_OF_BIRTH", newPatient.DateOfBirth);
        selectCommand.Parameters.AddWithValue("@ADDRESS_STREET", newPatient.addressStreet);
        selectCommand.Parameters.AddWithValue("@ADDRESS_CITY", newPatient.addressCity);
        selectCommand.Parameters.AddWithValue("@ADDRESS_STATE", newPatient.addressState);
        selectCommand.Parameters.AddWithValue("@ADDRESS_ZIP", newPatient.addressZip);
        selectCommand.Parameters.AddWithValue("@PHONE_HOME", newPatient.phoneHome);
        selectCommand.Parameters.AddWithValue("@PHONE_MOBILE", newPatient.phoneMobile);
        selectCommand.Parameters.AddWithValue("@PRIMARY_ID", 1);

        try
        {
            connection.Open();
            SqlDataReader patientReader =
                selectCommand.ExecuteReader(CommandBehavior.SingleRow);
            if (patientReader.Read())
            {
                Patient patient = new Patient();
                patient.patientID = patientReader["PATIENT_ID"].ToString();
                patient.lastName = patientReader["LAST_NAME"].ToString();
                patient.firstName = patientReader["FIRST_NAME"].ToString();
                patient.userName = patientReader["USERNAME"].ToString();
                patient.DateOfBirth = patientReader.GetDateTime(4);
                patient.addressStreet = patientReader["ADDRESS_STREET"].ToString();
                patient.addressCity = patientReader["ADDRESS_CITY"].ToString();
                patient.addressState = patientReader["ADDRESS_STATE"].ToString();
                patient.addressZip = patientReader["ADDRESS_ZIP"].ToString();
                patient.phoneHome = patientReader["PHONE_HOME"].ToString();
                patient.phoneMobile = patientReader["PHONE_MOBILE"].ToString();
                return patient;
            }
            else
            {
                return null;
            }
        }

        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }

当我运行它时,我收到错误说...

  

参数化查询'(@ PATIENT_ID nvarchar(1),@ LAST_NAME nvarchar(5),@ FIRST_NAME nvar'需要参数'@ ADDRESS_STREET',这是未提供的。“}

是否与查询字符串的含量有关?

0 个答案:

没有答案