2014-02-13 1 views
0

내 MS 액세스 데이터베이스에 목록보기 항목을 삽입하려고합니다.목록보기에서 일부 데이터를 vb.net의 ms 액세스 데이터베이스에 삽입하십시오.

Public newConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Inbox.Accdb;Persist Security Info=False;" 

Private Sub btnNewSMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewSMS.Click 

    cn.ConnectionString = newConn 
    If cn.State = ConnectionState.Closed Then 
     cn.Open() 

     For x = 0 To ListView2.Items.Count - 1 

      Dim sqlQuery As String = "INSERT INTO InboxTable (Contact_Name,Contact_Number,DateAndTime,Message) Values ('" & ListView2.Items(x).SubItems(0).Text & "', '" & ListView1.Items(x).SubItems(1).Text & "','" & ListView1.Items(x).SubItems(2).Text & "','" & ListView1.Items(x).SubItems(3).Text & ")" 

      Dim cmd As New OleDbCommand 

      With cmd 
       .CommandText = sqlQuery 
       .Connection = cn 
       .ExecuteNonQuery() 


      End With 
      MsgBox("Messages Saved") 
      ListView2.Items.Clear() 

      'End With 

     Next 

    End If 
    cn.Close() 


End Sub 

내 오류는 다음과 같습니다 : 여기

코드 '0'의

값 '인덱스'

내 문제는 값을 삽입에 유효하지 않습니다. ... 모두 감사를 도와주세요 - 크리스

+0

첫 번째 코드가 매우 끔찍하기 때문에 코드가 바뀌 었습니다. 죄송합니다 .. 여기에 새로운 코드와 새로운 오류 ('인덱스'에는 '0'값이 유효하지 않습니다.) – renge

답변

0
'Try this.. 

Public newConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Inbox.Accdb;Persist Security Info=False;" 


Private Sub btnNewSMS_Click(sender As Object, e As EventArgs) Handles btnNewSMS.Click 


    For Each x As ListViewItem In ListView2.Items 
     Dim sqlQuery As String = "INSERT INTO InboxTable (Contact_Name,Contact_Number,DateAndTime,Message) Values ('" & _ 
         x.SubItems(0).Text & "', '" & _ 
         x.SubItems(1).Text & "','" & _ 
         x.SubItems(2).Text & "','" & _ 
         x.SubItems(3).Text & "')" 
     'Add also the last single qoute before the parenthesis ^_^ 
     'to make sure the query works.. 
     Dim cmd As New OleDbCommand 

     With cmd 
      .CommandText = sqlQuery 
      .Connection = cn 
      .ExecuteNonQuery() 
     End With 
     MsgBox("Messages Saved") 
     ListView2.Items.Clear() 

     End With 
    Next 

End Sub 
+0

감사합니다. Diaton .. . 지금 일하고있다. 나는 나의 문제가 작은 따옴표와 listview2다는 것을 나는 안다. (나는 listview1을 값에 넣었습니다.) ... 신의 축복이 ... :) – renge

0

를 사용하여 하나 개 이상의 형태의

DataTransfer.vb

Public Class DataTransfer 

Public Sub ListToData(ByVal strcon As String, ByVal strcmd As String, ByVal listview As ListView) 
Dim i As Integer = 0 
Dim k As Integer = 0 
Dim item As New ListViewItem 
Dim con As New System.Data.OleDb.OleDbConnection(strcon) 
Dim cmd As New System.Data.OleDb.OleDbCommand(strcmd, con) 
Try 
MessageBox.Show(listview.Items.Count) 
    While i < listview.Items.Count 
     item = New ListViewItem() 
     item = listview.Items(i).Clone 
     itemToString(item, strcmd) 
     con.Open() 
     cmd.CommandText = strcmd 
     cmd.ExecuteNonQuery() 
     MessageBox.Show("saved") 
     i += 1 
     End While 
Catch ex As Exception 
MessageBox.Show(ex.ToString) 
End Try 
MessageBox.Show(strcmd) 
End Sub 

Private Sub itemToString(ByVal item As ListViewItem, ByRef strcmd As String) 
Dim k As Integer = 1 
strcmd += "VALUES (" 
strcmd += "'" & item.Text & "'" 
MessageBox.Show("subitems" + item.SubItems.Count.ToString) 
While k < item.SubItems.Count 
strcmd += ",'" & item.SubItems(k).Text & "'" 
k += 1 
End While 
strcmd += ")" 
End Sub 

End Class 

Form1.vb를

이 코드를
Public Class Form1 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

Dim datatransfer As New DataTransfer 
Dim con As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\schoolDB.accdb" 
Dim cmd As String = "INSERT INTO Stu_Info (C_ID, ADM_No, S_Name)" 
datatransfer.ListToData(con, cmd, ListView1) 
End Sub 
End Class 
관련 문제