C#:ExecuteNonQuery()异常

时间:2017-01-31 03:34:15

标签: c# asp.net-mvc asp.net-mvc-4 c#-4.0

实际上我有一个页面来更新个人资料信息。我有这样的代码

控制器

      if (ModelState.IsValid)
        {
            string UpdateClinicName = null;
            string UpdateDrname = null;
            string UpdateClinicFullAddress = null;
            string UpdateClinicFaxNo = null;
            string UpdateClinicPhone = null;
            string UpdateMOHRegistration = null;
            string UpdateClinicEmail = null;
            string UpdateContactID = null;
            string UpdateHospitalRegistrationNo = null;

                // Getting GroupID
            string GroupIDdrinin = null;
            GroupIDdrinin = Profiledatainfo.GroupID;

                // Getting ContactID
            UpdateContactID = Profiledatainfo.CContactID;

               // Getting ClinicName 
            UpdateClinicName = Profiledatainfo.Clinicname;

               // Getting Dr name 
            UpdateDrname = Profiledatainfo.Doctorname;

              // Getting ClinicFullAddress
            UpdateClinicFullAddress = Profiledatainfo.ClinicFullAddress;

              // Getting ClinicPhone 
            UpdateClinicPhone = Profiledatainfo.ClinicPhone;

              // Getting MOHRegistrationNo
            UpdateMOHRegistration = Profiledatainfo.MOHRegistrationNo;


              // Getting FaxNo
            UpdateClinicFaxNo = Profiledatainfo.ClinicFaxNo;

              // Getting Clinic Mail 
            UpdateClinicEmail = Profiledatainfo.ClinicBusinessEmail;

              // Getting HospitalRegistrationNo

            UpdateHospitalRegistrationNo = Profiledatainfo.RegistrationNo;

            ClinicProfileTimingModelDAL UpdateProfileInfo = new ClinicProfileTimingModelDAL();

              // Updating ProfileInformation 
            int Upstatus = UpdateProfileInfo.UpdateClinicProfile(UpdateContactID, UpdateClinicPhone, UpdateClinicFaxNo, UpdateClinicFullAddress, UpdateClinicEmail);
            int Uregistatus = UpdateProfileInfo.UpdateClinicRegistration(GroupIDdrinin, UpdateMOHRegistration, UpdateHospitalRegistrationNo);

        }

模型

  public class ClinicProfileTimingModelDAL
{

    public int UpdateClinicProfile(string ContactID, string ClinicPhone, string ClinicFaxNo, string ClinicAddress, string ClinicBusiessMail)
    {

        SqlConnection myConnection = new SqlConnection("DataBase Conn details");
        myConnection.Open();
        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "Execute SpClinicDetAmendMVC " + ContactID + ",'" + ClinicPhone + "','" + ClinicFaxNo + "','" + ClinicAddress + "','" + ClinicBusiessMail + "'";
        //sqlCmd.CommandText = "Select ConfirmVisitID,MemberID,GroupID,RecStatus,CreateUserID,VisitDate from tblConfirmVisit where Cast(VisitDate as Date)='" + Vstdate + "' and CreateUserID="+createuserid+"";


        sqlCmd.Connection = myConnection;
        int Msg = 0;
        Msg = sqlCmd.ExecuteNonQuery();
        myConnection.Close();
        return Msg;

    }

    public int UpdateClinicRegistration(string GroupID, string HospitalResitrationno, string MOHRegistrationNo)
    {

        SqlConnection myConnection = new SqlConnection("DataBase Conn details");
        myConnection.Open();
        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "Execute spupdateProfileInfoMVC '" + GroupID + "'," + HospitalResitrationno + "," + MOHRegistrationNo + "";
        sqlCmd.Connection = myConnection;
        int Msg = 0;
        Msg = sqlCmd.ExecuteNonQuery();
        myConnection.Close();
        return Msg;

    }

}

因此,当我尝试更新配置文件信息时,我有一个例外“我收到了.ExecuteNonQuery()错误。用户代码未处理SqlException //在','附近语法错误。

所以我在模型部分

尝试了替代方式
     sqlCmd.CommandText = "Execute spupdateProfileInfoMVC(@Cid, @Cp, @Cfn, @Ca, @Cbm) "
    sqlCmd.CommandText.Parameters.AddWithValue("@Cid", ContactID );
    sqlCmd.CommandText.Parameters.AddWithValue("@Cp", ClinicPhone  );
    sqlCmd.CommandText.Parameters.AddWithValue("@Cfn", ClinicFaxNo);
    sqlCmd.CommandText.Parameters.AddWithValue("@Ca", ClinicAddress  );
    sqlCmd.CommandText.Parameters.AddWithValue("@Cbm", ClinicBusinessMail);
    sqlCmd.CommandText.ExecuteNonQuery();

这种替代方式也给出了同样的例外。那么如何解决这个问题呢。任何帮助表示感谢,提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个:

qlCmd.CommandText = "Execute spupdateProfileInfoMVC '" + GroupID +
 "','" + HospitalResitrationno + "','" + MOHRegistrationNo + "'";

同样:

 sqlCmd.CommandText = "Execute SpClinicDetAmendMVC '" + ContactID + "','" + ClinicPhone + "','" + ClinicFaxNo + "','" + ClinicAddress + "','" + ClinicBusiessMail + "'";