2017-01-01 5 views
0

私はC#でMetroFrameworkを使用していますが、チェックリストはありません。誰かがchecklistboxを使わずにチェックボックスの値をmysqlに挿入する方法を教えてくれますか?C#でchecklistboxを使わずにmysqlにチェックボックスの値を挿入する

これは私が検索したコードの例ですが、動作しません。

CheckBox[] checkboxes = new CheckBox[] { newRIadhd, newRIallergy, newRIanemia, newRIasthma, newRIautism, newRIdepression, newRIdownSyndrome, newRIdyscalculia, newRIDyslexia, newRIepilepsy, newRIheartFailure, newRIhypertension, newRIintDis, newRImigraine, newRIspinaBifida}; 

      List<string> checked = new List<string>(); 
foreach (Checkbox checkbox in checkboxes) { 
    if (checkbox.Checked) { 
    checked.Add(checkbox.Text); 
    } 
} 
string checkedText = String.Join(", ", checked); 

これは、新しいデータを挿入し、それが実行されているため、すべての私のコードです。しかし、保存が必要なチェックボックスがたくさんあるので、「@morRI」にはまった。上記のコードは一部のウェブサイトのコピーに過ぎません。

string insertQuery = "INSERT INTO db_personal(per_image,per_Fname,per_Mname,per_Lname,per_Sname,per_birthDate,per_birthPlace,per_age,per_gender,per_nationality,per_religion,per_ethnicity,per_conNum,per_emailAdd,per_civStatus,per_spouseName,per_spouseOccupation,per_childName,per_childGender,per_childAge,per_city,per_barangay,per_division,per_blockOrStreet,per_houseNumber,per_zipCode) VALUES(@per_image,@per_Fname,@per_Mname,@per_Lname,@per_Sname,@per_birthDate,@per_birthPlace,@per_age,@per_gender,@per_nationality,@per_religion,@per_ethnicity,@per_conNum,@per_emailAdd,@per_civStatus,@per_spouseName,@per_spouseOccupation,@per_childName,@per_childGender,@per_childAge,@per_city,@per_barangay,@per_division,@per_blockOrStreet,@per_houseNumber,@per_zipCode); INSERT INTO db_academic(aca_elementary,aca_elemYearGrad,aca_elemHonor,aca_secondary,aca_secYearGrad,aca_secHonor,aca_seniorHigh,aca_SHyearGrad,aca_SHhonor,aca_ALS,aca_certificateSch,aca_certificateName,aca_tertiary,aca_mermbershipName,aca_membershipPos,aca_probation) VALUES(@aca_elementary,@aca_elemYearGrad,@aca_elemHonor,@aca_secondary,@aca_secYearGrad,@aca_secHonor,@aca_seniorHigh,@aca_SHyearGrad,@aca_SHhonor,@aca_ALS,@aca_certificateSch,@aca_certificateName,@aca_tertiary,@aca_mermbershipName,@aca_membershipPos,@aca_probation); INSERT INTO db_family(fam_parStatus,fam_fatherFN,fam_fatherMN,fam_fatherLN,fam_fatherSN,fam_fatherDec,fam_fatherAdd,fam_fatherEdu,fam_fatherOccu,fam_motherFN,fam_motherMN,fam_motherLN,fam_motherPN,fam_motherDec,fam_motherAdd,fam_motherEdu,fam_motherOccu,fam_guardianName,fam_guardAge,fam_guardOccu,fam_guardAdd,fam_guardRel,fam_guardConNum,fam_siblingName,fam_sibRel,fam_sibAge,fam_sibStat,fam_sibOccu,fam_sibConNum,fam_busKind,fam_busLocation,fam_busIncome) VALUES(@fam_parStatus,@fam_fatherFN,@fam_fatherMN,@fam_fatherLN,@fam_fatherSN,@fam_fatherDec,@fam_fatherAdd,@fam_fatherEdu,@fam_fatherOccu,@fam_motherFN,@fam_motherMN,@fam_motherLN,@fam_motherPN,@fam_motherDec,@fam_motherAdd,@fam_motherEdu,@fam_motherOccu,@fam_guardianName,@fam_guardAge,@fam_guardOccu,@fam_guardAdd,@fam_guardRel,@fam_guardConNum,@fam_siblingName,@fam_sibRel,@fam_sibAge,@fam_sibStat,@fam_sibOccu,@fam_sibConNum,@fam_busKind,@fam_busLocation,@fam_busIncome); INSERT INTO db() VALUES()"; 

      connection.Open(); 

      MySqlCommand cmd = new MySqlCommand(insertQuery, connection); 
      cmd.Parameters.AddWithValue("@per_image", newPicture.Image); 
      cmd.Parameters.AddWithValue("@per_Fname", newFirstName.Text); 
      cmd.Parameters.AddWithValue("@per_Mname", newMiddleName.Text); 
      cmd.Parameters.AddWithValue("@per_Lname", newLastName.Text); 
      cmd.Parameters.AddWithValue("@per_Sname", newSuffixName.Text); 
      cmd.Parameters.AddWithValue("@per_birthDate", newBirthDate.Value); 
      cmd.Parameters.AddWithValue("@per_birthPlace", newBirthPlace.Text); 
      cmd.Parameters.AddWithValue("@per_age", (newAge.Text)); 
      cmd.Parameters.AddWithValue("@per_gender", newGender.Text); 
      cmd.Parameters.AddWithValue("@per_nationality", newNationality.Text); 
      cmd.Parameters.AddWithValue("@per_religion", newReligion.Text); 
      cmd.Parameters.AddWithValue("@per_ethnicity", newEthnicity.Text); 
      cmd.Parameters.AddWithValue("@per_conNum", newContactNumber.Text); 
      cmd.Parameters.AddWithValue("@per_emailAdd", newEmailAddress.Text); 
      cmd.Parameters.AddWithValue("@per_civStatus", newCivilStatus.Text); 
      cmd.Parameters.AddWithValue("@per_spouseName", newSpouseName.Text); 
      cmd.Parameters.AddWithValue("@per_spouseOccupation", newSpouseOccupation.Text); 
      cmd.Parameters.AddWithValue("@per_childName", newChildName.Text); 
      cmd.Parameters.AddWithValue("@per_childGender", newGender.Text); 
      cmd.Parameters.AddWithValue("@per_childAge", newChildAge.Text); 
      cmd.Parameters.AddWithValue("@per_city", newCity.Text); 
      cmd.Parameters.AddWithValue("@per_barangay", newBarangay.Text); 
      cmd.Parameters.AddWithValue("@per_division", newDivision.Text); 
      cmd.Parameters.AddWithValue("@per_blockOrStreet", newBlockOrStreet.Text); 
      cmd.Parameters.AddWithValue("@per_houseNumber", newHouseNumber.Text); 
      cmd.Parameters.AddWithValue("@per_zipCode", newZipCode.Text); 

      cmd.Parameters.AddWithValue("@aca_elementary", newElementary.Text); 
      cmd.Parameters.AddWithValue("@aca_elemYearGrad", newFirstName.Text); 
      cmd.Parameters.AddWithValue("@aca_elemHonor", newElemHonor.Text); 
      cmd.Parameters.AddWithValue("@aca_secondary", newSecondary.Text); 
      cmd.Parameters.AddWithValue("@aca_secYearGrad", newSecYearGrad.Text); 
      cmd.Parameters.AddWithValue("@aca_secHonor", newSecHonor.Text); 
      cmd.Parameters.AddWithValue("@aca_seniorHigh", newSeniorHigh.Text); 
      cmd.Parameters.AddWithValue("@aca_SHyearGrad", newSHyearGrad.Text); 
      cmd.Parameters.AddWithValue("@aca_SHhonor", newSHhonor.Text); 
      cmd.Parameters.AddWithValue("@aca_ALS", newALSschool.Text); 
      cmd.Parameters.AddWithValue("@aca_certificateSch", newCertificateSchool.Text); 
      cmd.Parameters.AddWithValue("@aca_certificateName", newCertificateName.Text); 
      cmd.Parameters.AddWithValue("@aca_tertiary", newTertiary.Text); 
      cmd.Parameters.AddWithValue("@aca_mermbershipName", newMembershipName.Text); 
      cmd.Parameters.AddWithValue("@aca_membershipPos", newMembershipPosition.Text); 
      cmd.Parameters.AddWithValue("@aca_probation", newProbationReason.Text); 

      cmd.Parameters.AddWithValue("@fam_parStatus", newParentsStatus.Text); 
      cmd.Parameters.AddWithValue("@fam_fatherFN", newFatherFN.Text); 
      cmd.Parameters.AddWithValue("@fam_fatherMN", newFatherMN.Text); 
      cmd.Parameters.AddWithValue("@fam_fatherLN", newFatherLN.Text); 
      cmd.Parameters.AddWithValue("@fam_fatherSN", newFatherSN.Text); 
      cmd.Parameters.AddWithValue("@fam_fatherDec", newFatherDeceased.Text); 
      cmd.Parameters.AddWithValue("@fam_fatherAdd", newFatherAddress.Text); 
      cmd.Parameters.AddWithValue("@fam_fatherEdu", newFatherEducation.Text); 
      cmd.Parameters.AddWithValue("@fam_fatherOccu", newFatherOccupation.Text); 
      cmd.Parameters.AddWithValue("@fam_motherFN", newMotherFN.Text); 
      cmd.Parameters.AddWithValue("@fam_motherMN", newMotherMN.Text); 
      cmd.Parameters.AddWithValue("@fam_motherLN", newMotherLN.Text); 
      cmd.Parameters.AddWithValue("@fam_motherPN", newMotherPN.Text); 
      cmd.Parameters.AddWithValue("@fam_motherDec", newMotherDeceased.Text); 
      cmd.Parameters.AddWithValue("@fam_motherAdd", newMotherAddress.Text); 
      cmd.Parameters.AddWithValue("@fam_motherEdu", newMotherEducation.Text); 
      cmd.Parameters.AddWithValue("@fam_motherOccu", newMotherOccupation.Text); 
      cmd.Parameters.AddWithValue("@fam_guardianName", newGuardianName.Text); 
      cmd.Parameters.AddWithValue("@fam_guardAge", newGuardianAge.Text); 
      cmd.Parameters.AddWithValue("@fam_guardOccu", newGuardianOccupation.Text); 
      cmd.Parameters.AddWithValue("@fam_guardAdd", newGuardianAddress.Text); 
      cmd.Parameters.AddWithValue("@fam_guardRel", newGuardianRelation.Text); 
      cmd.Parameters.AddWithValue("@fam_guardConNum", newGuardianConNum.Text); 
      cmd.Parameters.AddWithValue("@fam_siblingName", newSiblingName.Text); 
      cmd.Parameters.AddWithValue("@fam_sibRel", newSiblingRelation.Text); 
      cmd.Parameters.AddWithValue("@fam_sibAge", newSiblingAge.Text); 
      cmd.Parameters.AddWithValue("@fam_sibStat", newSiblingStatus.Text); 
      cmd.Parameters.AddWithValue("@fam_sibOccu", newSiblingOccupation.Text); 
      cmd.Parameters.AddWithValue("@fam_sibConNum", newSiblingConNum.Text); 
      cmd.Parameters.AddWithValue("@fam_busKind", newBusinessKind.Text); 
      cmd.Parameters.AddWithValue("@fam_busLocation", newBusinessLocation.Text); 
      cmd.Parameters.AddWithValue("@fam_busIncome", newBusinessIncome.Text); 

      cmd.Parameters.AddWithValue("@mor_workPlace", newWorkPlace.Text); 
      cmd.Parameters.AddWithValue("@mor_workType", newWorkType.Text); 
      cmd.Parameters.AddWithValue("@mor_workIncDate", newWorkIncDate.Text); 
      cmd.Parameters.AddWithValue("@mor_workIncome", newWorkIncome.Text); 
      cmd.Parameters.AddWithValue("@mor_RI",); 
      cmd.Parameters.AddWithValue("@mor_FTE",); 
      cmd.Parameters.AddWithValue("@mor_lifeOccu",); 
      cmd.Parameters.AddWithValue("@mor_studyPlace",); 
      cmd.Parameters.AddWithValue("@mor_testTaken",); 
      cmd.Parameters.AddWithValue("@mor_verbInt",); 
      cmd.Parameters.AddWithValue("@mor_dateVerify",); 

      try 
      { 
       if (cmd.ExecuteNonQuery() == 1) 
       { 
        MetroFramework.MetroMessageBox.Show(this, "New student information has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       } 
       else 
       { 
        MetroFramework.MetroMessageBox.Show(this, "Incomplete information. Are you sure you want to save?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); 
       } 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 

      connection.Close(); 
+0

あなたはどのようなエラーを受け取ったのですか? – Valkyrie

+0

私はすべての私のコードを見ることができるように私の質問を編集:) – John

答えて

0

データの挿入中にエラーが発生したようです。 MySQL Insert into multiple tables? (Database normalization?)を参照してください。

上記のサンプルコードでは、チェックボックス内の各チェックボックスのチェックマークをコンマで区切った文字列(Textプロパティ)をcheckedTextに割り当てます。チェックボックスがチェックされていない場合、checkedTextは空の文字列です。

文字列をデータベースに書き込むには、それを対応するmysqlパラメータ(@mor_RI)に割り当て、最後のinsert文(パラメータ割り当て付き)を完了する必要があります。

cmd.Parameters.AddWithValue("@mor_RI", checkedText); 
+0

しかし、データの挿入で私のコードが実行されています。また、すべてのチェックボックスをデータベースに挿入する方法の例を挙げることができますか? – John

+0

上記の私の答えのコードを参照してください。これはチェックされたチェックボックスの名前を持つ文字列を作成する独自のコードに基づいています。 – b56

関連する問題