使用Visual Basic SQL访问更新 [英] Access Update with Visual Basic SQL

查看:84
本文介绍了使用Visual Basic SQL访问更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,首先.

从技术上讲,我是这个网站的新手,但是几年来我一直在使用它作为资源,这仅仅是因为我感到难过,现在我需要您的帮助.

Technically i am new to this site, however i have been using it as a resource for a few years, and it is only because i am stumped that i now come in need of your aid.

我有一个VB脚本,用于查询Db,用字符串填充预期的框并显示它.

I have a VB script that is Querying the Db, filling the intended boxes with the string and displaying it.

我的问题是,当我尝试使用Update SQL命令时,它完成了,但是当我手动检查时它不会更新数据库.

My issue is that when i try to use the Update SQL command, it completes however it does not update the DB when i manually check.

Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Data.mdb';")
Dim da As New OleDbDataAdapter
Dim dt As New DataTable()
Dim cmd As New OleDbCommand
da.SelectCommand = New OleDbCommand("update FDSL set Host=@Host, Owner=@Owner where ID = @ID", con)


    Dim paramID As New OleDbParameter
    paramID.ParameterName() = "@ID"
    paramID.Value() = Label8.Text
    da.SelectCommand.Parameters.Add(paramID)
    Dim paramHost As New OleDbParameter
    paramHost.ParameterName() = "@Host"
    paramHost.Value() = TextBox1.Text
    da.SelectCommand.Parameters.Add(paramHost)
    Dim paramOwn As New OleDbParameter
    paramOwn.ParameterName() = "@Owner"
    paramOwn.Value() = TextBox4.Text

    da.SelectCommand.ExecuteNonQuery()

    MessageBox.Show("Record Updated! ", "Update Status", MessageBoxButtons.OK, MessageBoxIcon.Information)

    Refresh()
    con.Close()

我也尝试在最后使用.tostring进行参数设置,但这也不起作用.

I have tried the params using the .tostring on the end as well, but this does not work either.

----------已解决----------------

----------Solved----------------

我必须感谢@Gord Thompson的帮助,但是我通过重新编码解决了该问题.

I must thank @Gord Thompson for this assistance, however i solved the issue by recoding it.

        Dim str As String
    str = "update FDSL set Hostname=@Hostname, Owner=@Owner where ID=@id"
    Dim cmd As New OleDbCommand(str, con)
    cmd.Parameters.AddWithValue("@Hostname", TextBox1.Text)
    cmd.Parameters.AddWithValue("@Owner", TextBox2.Text)
    cmd.Parameters.AddWithValue("@ID", textbox6.Text)
    con.Open()
    cmd.ExecuteNonQuery()
    con.Close()

现在可以正常工作了,谢谢您的再次帮助!

This now works correctly thank your for helping again!

推荐答案

Jet.OLEDBACE.OLEDB忽略参数的名称,因此我们需要按照出现的确切顺序指定参数在CommandText中.在您的情况下,您需要更改执行.Parameters.Add()块的顺序,以便依次执行@Host@Owner@ID.

Jet.OLEDB and ACE.OLEDB ignore the names of parameters so we need to specify parameters in the exact order in which they appear in the CommandText. In your case you need to change the order in which you execute your .Parameters.Add() blocks so you do @Host, then@Owner, and then @ID.

这篇关于使用Visual Basic SQL访问更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆