vb.net使正确的表单插入到访问DB中

时间:2014-07-03 13:35:16

标签: vb.net insert

我在Access 2010中有一个名为Clienti的数据库表,其中包含列:

  • nume(文字)
  • prenume(文字)
  • cnp(号码)
  • varsta(号码)
  • adresa(文字)
  • localitate(文字)
  • data(约会)
  • stare civila(文字)
  • numarcopii(号码)
  • venit(号码)
  • tipcreditales(文字)
  • rezultat(文字)

我使用Visual Studio 2013在VB.NET中创建了一个表单,我希望将数据插入到Clienti表中。

我的问题是:

  1. 对于data我使用日期/时间选择器。有什么办法可以把它转换成日期吗?当我尝试插入它时,VB说日期/时间选择器无法转换为日期。

  2. 对于stare civila我想要一个组合框与3个项目一起使用。如何在字符串中转换这3个项目? (这些项目由我手动介绍)。

  3. 对于tipcreditales我也想使用组合框,因此可以应用与上述相同的问题。

  4. 使用的代码是:

        Convert.ToInt32(cnptxt.Text)
        Convert.ToInt32(numarcopiitxt.Text)
    
        Dim cnpp As Integer
        cnpp = Val(cnptxt.Text)
        Dim nrcopii As Integer
        nrcopii = Val(numarcopiitxt.Text)
    
        Dim nume As String
    
        Dim prenume As String
        Dim cnp As Integer
        Dim varsta As Integer
        Dim adresa As String
        Dim localitate As String
        Dim starecivila As String
        Dim numarcopii As Integer
        Dim data As Date = datatxt.Value.Date
        Dim venit As Integer
        Dim tipcreditales As String
        Dim rezultat As String
    
        nume = numetxt.Text
        prenume = prenumetxt.Text
        cnp = cnpp
        varsta = varstatxt.Text
        adresa = adresatxt.Text
        localitate = localitatetxt.Text
        starecivila = starecivilatxt.Text
        numarcopii = numarcopiitxt.Text
        tipcreditales = ComboBox1
        rezultat = rezultattxt.Text
    
        If Me.ClientiTableAdapter.InsertQueryClienti(nume, prenume, cnp, varsta, adresa, localitate, starecivila, numarcopii, tipcreditales, rezultat) Then
            MsgBox("Adaugat cu succes in baza de date!")
        End If
    

    尝试运行该应用时,它说:

      

    1'System.Windows.Forms.ComboBox'类型的值无法转换为'String'。 D:\ Test \ incercare2 \ WindowsApplication2 \ WindowsApplication2 \ Form3.vb 60 25 WindowsApplication2

         

    2没有为'Public Overridable Overloads函数的参数'rezultat'指定参数InsertQueryClienti(数字为字符串,prenume为字符串,cnp为整数?,varsta为整数?,adresa为字符串,localitate为字符串,数据为日期? ,stare_civila As String,numarcopii As Integer?,venit As String,tipcreditales As String,rezultat As String)As Integer'。 D:\ Test \ incercare2 \ WindowsApplication2 \ WindowsApplication2 \ Form3.vb 62 12 WindowsApplication2

         

    3没有为'Public Overridable Overloads函数InsertQueryClienti的参数'tipcreditales'指定参数(数字为String,prenume为String,cnp为Integer?,varsta为Integer?,adresa为String,localitate As String,data as Date? ,stare_civila As String,numarcopii As Integer?,venit As String,tipcreditales As String,rezultat As String)As Integer'。 D:\ Test \ incercare2 \ WindowsApplication2 \ WindowsApplication2 \ Form3.vb 62 12 WindowsApplication2

    InsertQueryClienti的代码是

    `Public Overridable Overloads Function InsertQueryClienti(
    ByVal nume As String
    ByVal prenume As String 
    ByVal cnp As Global.System.Nullable(Of Integer)
    ByVal varsta As Global.System.Nullable(Of Integer), 
    ByVal adresa As String,
    ByVal localitate As String,
    ByVal data As Global.System.Nullable(Of Date)
    ByVal stare_civila As String,
    ByVal numarcopii As Global.System.Nullable(Of Integer),
    ByVal venit As String, 
    ByVal tipcreditales As String,
    ByVal rezultat As String) As Integer`
    

1 个答案:

答案 0 :(得分:0)

Here's how you get the DateTimePicker value:

Public Sub New()
   ' Create a new DateTimePicker 
   Dim dateTimePicker1 As New DateTimePicker()
   Controls.AddRange(New Control() {dateTimePicker1})
   MessageBox.Show(dateTimePicker1.Value.ToString())

   dateTimePicker1.Value = DateTime.Now.AddDays(1)
   MessageBox.Show(dateTimePicker1.Value.ToString())
End Sub 'New

Here's how you get the ComboBox value:

Private Sub showSelectedButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim selectedIndex As Integer
    selectedIndex = comboBox1.SelectedIndex
    Dim selectedItem As Object
    selectedItem = comboBox1.SelectedItem

    MessageBox.Show("Selected Item Text: " & selectedItem.ToString() & Microsoft.VisualBasic.Constants.vbCrLf & _
                        "Index: " & selectedIndex.ToString())
End Sub