System.Data.OleDb.OleDbException:'没有给出一个或多个必需参数的值。'

时间:2017-12-12 15:25:20

标签: vb.net ms-access oledb

  

System.Data.OleDb.OleDbException:'没有给出一个或多个必需参数的值。'

我在这段代码中有这个错误怎么解决呢?

Imports System.Data.OleDb

Public Class yenikayit
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim isimT As String = ""
    Dim soyisimT As String = ""
    Dim tcNoT As String = ""
    Dim yasT As String = ""
    Dim cinsiyetT As String = ""
    Dim perNoT As String = ""
    Dim egDurT As String = ""
    Dim meslekT As String = ""
    Dim telNoT As String = ""
    Dim emailT As String = ""

    isimT = TextBox1.Text
    soyisimT = TextBox2.Text
    tcNoT = TextBox3.Text
    yasT = TextBox6.Text
    cinsiyetT = ComboBox1.Text
    perNoT = TextBox4.Text
    egDurT = ComboBox2.Text
    telNoT = TextBox5.Text
    meslekT = TextBox10.Text
    emailT = TextBox9.Text


    Dim query As String = "INSERT INTO Personel (isim,soyisim,Yas,Cinsiyet,Alan,Egitim,TC,personel_id,tel_no,email) VALUES(isimT,soyisimT,yasT,cinsiyetT,meslekT,egDurT,tcNoT,perNoT,telNoT,emailT)"
    Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MONSTER\Desktop\AGKS\AGKS\Database5.accdb"
    Dim conn = New OleDbConnection(dbsource)
    Dim cmd As New OleDbCommand(query, conn)
    conn.Open()
    Dim sonuc As Boolean
    sonuc = cmd.ExecuteNonQuery()
    If (sonuc = 1) Then
        MsgBox("Girdiğiniz veriler kayıt olmuştur")
        conn.Close()
    End If
End Sub
End Class

this is the part of the code that give us error

2 个答案:

答案 0 :(得分:1)

目前,您的代码不会向命令添加任何参数值,因此会出现“No Values Given”错误。该命令无法识别这些名称并仅因为您已将它们声明为上述变量而为其分配值。您应该清楚这些是sql查询中的参数,然后在执行查询之前设置每个参数的值:

  

请注意,OleDbType.VarChar的所有实例都需要替换为列的正确数据类型。我显然无法知道这些列是什么类型,所以我刚刚将它们全部变为VarChar,但你应该调整它们以匹配你的表模式。

Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MONSTER\Desktop\AGKS\AGKS\Database5.accdb"
Dim query As String = "INSERT INTO Personel (isim,soyisim,Yas,Cinsiyet,Alan,Egitim,TC,personel_id,tel_no,email) " & _
                       "VALUES (@isimT,@soyisimT,@yasT,@cinsiyetT,@meslekT,@egDurT,@tcNoT,@perNoT,@telNoT,@emailT)"
Using conn As OleDbConnection = New OleDbConnection(dbsource)
    Dim cmd As New OleDbCommand(query, conn)
    With cmd
        .Parameters.Add("@isimT", OleDbType.VarChar).Value = TextBox1.Text
        .Parameters.Add("@soyisimT", OleDbType.VarChar).Value = TextBox2.Text
        .Parameters.Add("@yasT", OleDbType.VarChar).Value = TextBox6.Text
        .Parameters.Add("@cinsiyetT", OleDbType.VarChar).Value = ComboBox1.Text
        .Parameters.Add("@meslekT", OleDbType.VarChar).Value = TextBox10.Text
        .Parameters.Add("@egDurT", OleDbType.VarChar).Value = ComboBox2.Text
        .Parameters.Add("@tcNoT", OleDbType.VarChar).Value = TextBox3.Text
        .Parameters.Add("@perNoT", OleDbType.VarChar).Value = TextBox4.Text
        .Parameters.Add("@telNoT", OleDbType.VarChar).Value = TextBox5.Text
        .Parameters.Add("@emailT", OleDbType.VarChar).Value = TextBox9.Text
    End With
    conn.Open()
    Dim sonuc As Boolean
    sonuc = cmd.ExecuteNonQuery()
    If (sonuc = 1) Then
        MsgBox("Girdiğiniz veriler kayıt olmuştur")
    End If
    conn.Close()
End Using

答案 1 :(得分:0)

好的,尝试这样的事情:

Dim query As String = "INSERT INTO Personel (isim,soyisim,Yas,Cinsiyet,Alan,Egitim,TC,personel_id,tel_no,email) "
query +="VALUES(@isimT,@soyisimT,@yasT,@cinsiyetT,@meslekT,@egDurT,@tcNoT,@perNoT,@telNoT,@emailT)"
Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MONSTER\Desktop\AGKS\AGKS\Database5.accdb"
Dim conn = New OleDbConnection(dbsource)
Dim cmd As New OleDbCommand(query, conn)
 query.Parameters.AddWithValue("@isimT", TextBox1.Text)
 cmd.Parameters.AddWithValue("@soyisimT", TextBox2.Text)   
 cmd.Parameters.AddWithValue("@tcNoT", TextBox3.Text)
 cmd.Parameters.AddWithValue("@yasT", TextBox6.Text)
 cmd.Parameters.AddWithValue("@cinsiyetT", ComboBox1.Text)
 cmd.Parameters.AddWithValue("@perNoT", TextBox4.Text)
 cmd.Parameters.AddWithValue("@egDurT", ComboBox2.Text)
 cmd.Parameters.AddWithValue("@telNoT", TextBox5.Text)
 cmd.Parameters.AddWithValue("@meslekT", TextBox10.Text)
 cmd.Parameters.AddWithValue("@emailT", TextBox9.Text)
 conn.Open()
 Dim sonuc As Boolean
    sonuc = cmd.ExecuteNonQuery()
    If (sonuc = 1) Then
        MsgBox("Girdiğiniz veriler kayıt olmuştur")
        conn.Close()
    End If

这应该可以胜任。这应该可以解决您的异常。