简体   繁体   中英

SQL Updating in VB.NET using OLEDB

I am trying to update an Access database from an XML file using VB.NET and OleDB. 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. Debug output shows all the correct values and a similar method for an Insert SQL works fine. 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. 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. They are not recognized by their name. (See the Remarks section on OleDbCommand.Parameters collection )

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.

This means that all your parameters are used for the wrong fields or condition and you end up with a where clause totally wrong. The command searches the CRSID field with the value of FirstName (and, pretty sure, it cannot find any match).

The correct code for your sql is

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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