[ad_1]
<pre> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try 'CheckedListBox1.Show() DateTimePicker1.Value = Date.Now 'Dim cmd As New OleDbCommand Dim cmd As OleDbCommand = New OleDbCommand("select * from dossier_ass where pieces_fourni =@pieces_fourni and pieces_manquant = @pieces_manquant", cn) Dim da As New OleDbDataAdapter(cmd) Dim dt As New DataTable() Dim pieces_fourni As String = "" Dim pieces_manquant As String = "" cmd.Parameters.AddWithValue("@pieces_fourni", pieces_fourni) cmd.Parameters.AddWithValue("@pieces_manquant", pieces_manquant) cn.Open() cmd = New OleDbCommand("Insert into tab_asso (id_dossier,denomination,date_recep,nom_prenom,adresse,wilaya,code_wilaya,region,pieces_fourni,pieces_manquant) VALUES(@id_dossier,@denomination,@date_recep,@nom_prenom,@adresse,@wilaya,@code_wilaya,@region,@pieces_fourni,@pieces_manquant) ", cn) cmd.Parameters.AddWithValue("@id_dossier", TextBox1.Text) cmd.Parameters.AddWithValue("@denomination", TextBox2.Text) cmd.Parameters.AddWithValue("@date_recep", DateTimePicker1.Text) cmd.Parameters.AddWithValue("@nom_prenom", TextBox6.Text) cmd.Parameters.AddWithValue("@adresse", TextBox7.Text) cmd.Parameters.AddWithValue("@wilaya", ComboBox1.Text) cmd.Parameters.AddWithValue("@code_wilaya", TextBox4.Text) cmd.Parameters.AddWithValue("@Region", TextBox5.Text) For Each dr As DataRow In dt.Rows Dim selectedIndex As Integer = CheckedListBox1.SelectedIndex If selectedIndex <> -1 Then 'Loop through items to get their check states. For i As Integer = 0 To CheckedListBox1.Items.Count - 1 Dim checkstate As CheckState checkstate = CheckedListBox1.GetItemCheckState(i) If (checkstate = checkstate.Checked) Then cmd.Parameters.AddWithValue("@pieces_fourni", CheckedListBox1.Items(i)).ToString() 'true Else cmd.Parameters.AddWithValue("@pieces_manquant", CheckedListBox1.Items(i)).ToString() 'false End If Next End If Next cmd.ExecuteNonQuery() Catch ex As Exception MessageBox.Show("Erreur lors de l'insertion de l'enregistrement dans la table..." & ex.Message, "Insértion d'Enregistrement") Finally cn.Close() refraiche() End Try MessageBox.Show("Données insérées avec succès") End Sub
我尝试过的:
大家好
我有一个 CheckedListBox,它指定要提供的文件文档 我希望将选中的框存储在访问表中名为pieces_fourni 的单个字段中,并将未选中的框存储在另一个名为pieces_manquant 的单个字段中 我已经尝试了几次但没有成功,任何人都可以帮助我这是代码
解决方案1
数据库中的一列仅保存一个值:保存 muyltip[le values you would have to do one of two things:
1) Convert your “Checked” items to a comma delimited string, and your “Unchecked” items to another. Then store the strings in a single column each.
The problem with this approach is that it’s difficult to break the string up in a way that allows you to refer back to the actual items you are checking. You have to split the string, then convert them to numeric values to identify which control to check or uncheck.
2) Remember that you are using a relational database which is very good at combining data from multiple tables: create a second table which uses the originals row ID to refer back to the appropriate row, an index into an array of controls on your form, and a Checked/Unchecked status.
A simple loop then sets them as appropriate
[ad_2]
コメント