Function for inserting/updating MySql DB returns wrong quot;affected rowsquot; number(用于插入/更新 MySql DB 的函数返回错误的“受影响的行;数字)
问题描述
我正在使用下面的代码使用 DataTable
I'm using the code below to insert or update a MySql table using values from a DataTable
Function DB_Multi_Ins_Or_Upd_From_DT(ByVal dt As DataTable, ByVal DestTbl$) As Integer
Using SQLConnection As New MySqlConnection(CnStr)
Using sqlCommand As New MySqlCommand()
Dim CmdTxt$ = "INSERT INTO " & DestTbl & " ("
For Each Col As DataColumn In dt.Columns
CmdTxt &= Col.ColumnName & ", "
Next
CmdTxt = CmdTxt.Substring(0, CmdTxt.Length - 2) & ") VALUES ("
For x As Integer = 0 To dt.Rows.Count - 1
For xx As Integer = 0 To dt.Columns.Count - 1
CmdTxt &= "@" & "R" & x.ToString & "C" & xx.ToString & ", "
sqlCommand.Parameters.AddWithValue _
("@" & "R" & x.ToString & "C" & xx.ToString, dt.Rows(x)(xx))
Next xx
CmdTxt = CmdTxt.Substring(0, CmdTxt.Length - 2) & "), ("
Next x
CmdTxt = CmdTxt.Substring(0, CmdTxt.Length - 3)
CmdTxt &= " ON DUPLICATE KEY UPDATE "
For Each Col As DataColumn In dt.Columns
CmdTxt &= Col.ColumnName & "=VALUES(" & Col.ColumnName & "), "
Next
CmdTxt = CmdTxt.Substring(0, CmdTxt.Length - 2)
With sqlCommand
.CommandText = CmdTxt
.Connection = SQLConnection
.CommandType = CommandType.Text
End With
Try
SQLConnection.Open()
Dim AffRows% = sqlCommand.ExecuteNonQuery()
Return AffRows
Catch ex As MySqlException
Return -1
Finally
SQLConnection.Close()
End Try
End Using
End Using
End Function
碰巧代码更新了 1 行但返回了 2.
It happens that the code updates 1 row but returns 2.
似乎仅在 update 上返回错误结果,而在 insert 上返回正确数量的受影响行
It seems that the wrong result is returned only on update while on insert is returned the right number of affected rows
怎么了?如何修复并获取受影响行的正确数量?
What's wrong? How can I fix and get rigth number of affected rows?
要测试我的代码,它需要:
To test my code it needs:
1) 导入 MySql.Data.MySqlClient
1) Imports MySql.Data.MySqlClient
2) 给 cnstr 赋值,比如"datasource=" + Server_Name + ";username=" + UserDB + ";password="+ 密码 + ";database=" + Database_Name + ""
2) give a value to cnstr like
"datasource=" + Server_Name + ";username= " + UserDB + ";password="
+ Password + ";database=" + Database_Name + ""
3) 创建一个 DataTable,其列名类似于数据库表的字段
3) create a DataTable who has column names like fields of a DB table
4) 调用传递DataTable和表名的函数在数据库上
4) call the function passing the DataTable and the name of the table on the DB
推荐答案
使用 ON DUPLICATE KEY UPDATE,每行的受影响行值为 1,如果该行作为新行插入,如果更新现有行,则为 2,并且0 如果现有行设置为其当前值
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values
dev.mysql.com
这篇关于用于插入/更新 MySql DB 的函数返回错误的“受影响的行";数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用于插入/更新 MySql DB 的函数返回错误的“受影响
基础教程推荐
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
