Vb.Net 2010-具有自动数字值的SQL插入命令

时间:2018-11-30 00:15:45

标签: sql vb.net

我正在寻找使用SQL添加具有自动数字值的行。

我正在使用Studio VB2010。我有一个非常简单的表,其中包含2个字段: ID自动数字 模型文本字段。

    constr = "INSERT INTO procedures VALUES('" & txtFName.Text & "')"
    cmd = New OleDbCommand(constr, cn)

    cn.Open()
    Me.i = cmd.ExecuteNonQuery

一则消息说缺少参数, 所以我的问题是 如何在SQL命令中添加此自动值? (ID)

我应该得到最后一个ID号和+1吗?我认为必须有一种简单的方法。

谢谢。

更新#1 我现在按照建议尝试参数化查询... 我找到了这个例子,

  Dim cmdText As String = "INSERT INTO procedures VALUES (?,?)"
    Dim cmd As OleDbCommand = New OleDbCommand(cmdText, con)
    cmd.CommandType = CommandType.Text
    With cmd.Parameters
        .Add("@a1", OleDbType.Integer).Value = 0
        .Add("@a2", OleDbType.VarChar).Value = txtFName.Text
    End With
    cmd.ExecuteNonQuery()
    con.Close()

但是,仍然出现语法错误。 有什么想法吗?

谢谢大家。


更新#2 如果我提供下一个ID号,此代码似乎可以工作,但是再次说明,如何自动执行此操作?

Dim cmdText As String = "INSERT INTO procedures VALUES (?,?)"
Dim cmd As OleDbCommand = New OleDbCommand(cmdText, con)
cmd.CommandType = CommandType.Text
    With cmd.Parameters
        .AddWithValue("@a1", OleDbType.Integer).Value = 3
        .AddWithValue("@a2", OleDbType.VarChar).Value = txtFName.Text
    End With
    cmd.ExecuteNonQuery()
    con.Close()

有何评论?再次感谢。

UPDATE#3此代码使我出现语法错误 正如我被告知尝试的那样,我只将我的唯一一列更新,第二个是自动编号列。

 Dim Con As OleDbConnection = New OleDbConnection(dbProvider & dbSource)
 Dim SQL_command As String = "INSERT INTO procedures (procedure) VALUES ('Model')"
Dim cmd As OleDbCommand = New OleDbCommand(SQL_command, Con)

    Try
        Con.Open()
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        Throw ex
    Finally
        Con.Close()
        Con.Dispose()
    End Try

更新#4-解决方案

我将这段代码放在这里,以防有人觉得有用。

    dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;"
    dbSource = "Data Source = c:\gastrica\dabase.accdb"
    Con.ConnectionString = dbProvider & dbSource


Dim Con As OleDbConnection = New OleDbConnection(dbProvider & dbSource)
    Dim SQL_command As String = "INSERT INTO [procedures] ([procedure]) VALUES (?)"
    Dim cmd As OleDbCommand = New OleDbCommand(SQL_command, Con)
    cmd.CommandType = CommandType.Text
    With cmd.Parameters
        .AddWithValue("@procedure", OleDbType.VarChar).Value = txtFName.Text
    End With

    Try
        Con.Open()
        cmd.ExecuteNonQuery()
        Dim varProcedure As String = cmd.Parameters("@procedure").Value.ToString()
        MessageBox.Show("Record inserted successfully. Procedure = " & varProcedure)
    Catch ex As Exception
        Throw ex
    Finally
        Con.Close()
    End Try

感谢您的评论和帮助。

3 个答案:

答案 0 :(得分:0)

您需要指定列:

INSERT INTO procedures (columnname, columnname2) VALUES ('test', 'test');

这是一个显示示例的sql小提琴:

http://sqlfiddle.com/#!9/3cf706/1

答案 1 :(得分:0)

尝试这个:

constr = "INSERT INTO procedures (ID, Model) VALUES (0,'" & txtFName.Text & "')"
'or (not sure)
constr = "INSERT INTO procedures (Model) VALUES ('" & txtFName.Text & "')"

当使用0作为值时,在SQL的自动数字字段中插入下一个数字

答案 2 :(得分:0)

感谢您的回答。

我做不到,这给了我错误。因此,最后我得到一个变量,该变量达到了下一个“自动值”。使用简单的SQL命令。 然后,一切运行良好。

 vNextAuto = GetDB_IntValue("SELECT TOP 1 * FROM procedures ORDER BY ID DESC", "ID") + 1

    Dim SQL_command As String = "INSERT INTO procedures VALUES (?,?)"
    Dim cmd As OleDbCommand = New OleDbCommand(SQL_command, Con)
    cmd.CommandType = CommandType.Text
    With cmd.Parameters
        .AddWithValue("@id", OleDbType.Integer).Value = vNextAuto
        .AddWithValue("@a2", OleDbType.VarChar).Value = txtFName.Text
    End With

    Try
        Con.Open()
        cmd.ExecuteNonQuery()
        'Dim id As String = cmd.Parameters("@id").Value.ToString()
        'MessageBox.Show("Record inserted successfully. ID = " & id)
    Catch ex As Exception
        Throw ex
    Finally
        Con.Close()
        Con.Dispose()
    End Try