简体   繁体   English

使用OLEDB在VB.NET中进行SQL更新

[英]SQL Updating in VB.NET using OLEDB

I am trying to update an Access database from an XML file using VB.NET and OleDB. 我正在尝试使用VB.NET和OleDB从XML文件更新Access数据库。 I am using the following code but cannot get the update to work. 我正在使用以下代码,但无法使更新生效。 I am not getting any errors but no update is happening - it seems to be treating CRSID as a string despite .DbType.Int32. 我没有收到任何错误,但没有更新发生-尽管.DbType.Int32,它似乎将CRSID视为字符串。 Debug output shows all the correct values and a similar method for an Insert SQL works fine. 调试输出显示所有正确的值,并且使用类似的方法插入SQL也可以正常工作。 If I hard code a value in CRSID the db is updated but the data is in the wrong columns LastName contains CRSID and FirstName contains LastName. 如果我对CRSID中的值进行硬编码,则会更新数据库,但数据在错误的列中LastName包含CRSID,而FirstName包含LastName。 Hope someone can help. 希望有人能帮忙。

For Each drCurrent In dt.Rows
            Using cmd As New OleDb.OleDbCommand With
                {
                    .Connection = cn,
                    .CommandText =
                    <SQL>
                        Update NewPMS SET LastName = @LastName, FirstName = @Firstname WHERE CRSID = @CRSID
                    </SQL>.Value
                }
                cmd.Parameters.AddRange(New OleDb.OleDbParameter() _
                    {
                      New OleDb.OleDbParameter With {.ParameterName = "@CRSID", .DbType = DbType.Int32},
                      New OleDb.OleDbParameter With {.ParameterName = "@LastName", .DbType = DbType.String},
                      New OleDb.OleDbParameter With {.ParameterName = "@FirstName", .DbType = DbType.String}
                    }
                )
                cmd.Parameters(0).Value = drCurrent("CRSID")
                cmd.Parameters(1).Value = drCurrent("LastName")
                cmd.Parameters(2).Value = drCurrent("FirstName")
                cn.Open()
                cmd.ExecuteNonQuery()
                cn.Close()
                cmd.Parameters.Clear()
            End Using
        Next    

In OleDb parameters are positional. 在OleDb中,参数是位置。 They are not recognized by their name. 他们的名字无法识别。 (See the Remarks section on OleDbCommand.Parameters collection ) (请参阅OleDbCommand.Parameters集合上的“ Remarks section

The parameter placeholder for @CRSID appears as the last one in your query, but the first OleDbCommand.Parameter is, named for, and filled with the value for @CRSID. @CRSID的参数占位符显示为查询中的最后一个,但第一个OleDbCommand.Parameter被命名,命名并用@CRSID的值填充。

This means that all your parameters are used for the wrong fields or condition and you end up with a where clause totally wrong. 这意味着您所有的参数都用于错误的字段或条件,并且最终出现where子句完全错误的情况。 The command searches the CRSID field with the value of FirstName (and, pretty sure, it cannot find any match). 该命令使用FirstName的值搜索CRSID字段(当然,它找不到任何匹配项)。

The correct code for your sql is 您的sql的正确代码是

Update NewPMS SET 
    LastName = @LastName, 
    FirstName = @Firstname 
WHERE CRSID = @CRSID

....

cmd.Parameters.AddRange(New OleDb.OleDbParameter() _
    {
      New OleDb.OleDbParameter With {.ParameterName = "@LastName", .DbType = DbType.String},
      New OleDb.OleDbParameter With {.ParameterName = "@FirstName", .DbType = DbType.String}
      New OleDb.OleDbParameter With {.ParameterName = "@CRSID", .DbType = DbType.Int32},
    }
)
cmd.Parameters(0).Value = drCurrent("LastName")
cmd.Parameters(1).Value = drCurrent("FirstName")
cmd.Parameters(2).Value = drCurrent("CRSID")

As a side note, you should move the initialization of the OleDbCommand outside the loop, create the empty parameters and open the connection. 附带说明,您应该将OleDbCommand的初始化移到循环外,创建空参数并打开连接。 Inside the loop, just set the values for the parameters from the current row and execute the command. 在循环内部,只需设置当前行中参数的值并执行命令即可。 (Don't close the connection until the end of the loop of course). (在循环结束之前,请不要关闭连接)。 This logic is better and without side effects, but it could give a slight better performance if you have many rows. 这种逻辑更好,没有副作用,但是如果您有很多行,它可能会带来稍微更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM