是否有更好的方法将我的数据发布到mdb,因为我有太多的值要插入(VB 2010访问DB 2003)

时间:2014-03-31 20:04:12

标签: vb.net visual-studio-2010 insert oledb ms-access-2003

因为如下所示,我的' sql'字符串太长了,我该如何组织这个插入字符串? 谢谢!

Try
        conn.Open()
        sql = "SELECT * FROM guests WHERE folio='" & folionum_txtfield.Text & "' AND fname = '" & Fname_txtfield.Text & "'"
        Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql, conn)
        Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()

        If Not sqlRead.HasRows Then
            sql = "INSERT INTO guests values (" & folionum & "," & lname & "," & fname & "," & address & "," & lname & "," & country & "," & company & "," & idtype & "," & otherID & "," & idtype & "," & otherID & "," & idnum & "," & otherID & "," & vehicle & "," & vmodel & "," & pnum & "," & rooomnum & "," & datechckin & "," & datechckout & "," & rmtype & numdays & "," & numadults & "," & numchild & "," & notes)
        End If

    Catch ex As Exception

1 个答案:

答案 0 :(得分:0)

以下是您可以遵循的一些简单步骤,以实现您的需求......

  1. 为ex:GuestDataClass
  2. 创建一个DataClass
  3. 列出一些属性(setter& getters)。
  4. 创建此类的新实例并设置属性。
  5. 创建一个函数并将新实例传递给您的函数。
  6. 然后您可以根据需要使用您的属性保存或更新等。
  7. 这不会让它变得更小但更有条理。

    这是一个简单的例子......

     Public Class GuestDataClass 'This would be your new class for your properties...
    
     #Region "Variables" 'Add your variables here'
       Private strFirstName As String = String.Empty
       Private strLastName As String = String.Empty
     #End Region
    
     #Region "Properties" 'Add some properties'
    
     Public Property FirstName() As String
        Get
            Return strFirstName
        End Get
        Set(ByVal value As String)
            strFirstName = value
        End Set
     End Property
    
     Public Property LastName() As String
        Get
            Return strLastName
        End Get
        Set(ByVal value As String)
            strLastName = value
        End Set
     End Property
    
     #End Region
    
     #Region "Methods"
    
    'Inserts new guest'
     Public Shared Function SaveGuest(ByVal oGuest As GuestDataClass) As Boolean
    
        'Use your properties for the save routine here... just an example...
        'oGuest.FirstName
        'oGuest.LastName 
    
        Return True
     End Function
    
     #End Region
    
     End Class
    

    然后你可以这样使用它......

     Public Class Form1
    
     Private pData As GuestDataClass 'Set a variable in your class you can use..
    
     Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
        'Set this variable of your data class...
        pData = New GuestDataClass()
    
        'Set our properties...
        pData.FirstName = "Bobby"
        pData.LastName = "Walters"
    
        'Save the data...
        If GuestDataClass.SaveGuest(pData) Then
            MessageBox.Show("Saved!")
        Else
            MessageBox.Show("Error!")
        End If
    
    
    End Sub
    End Class