gpt4 book ai didi

C# Mysql 创建新行但如果不存在

转载 作者:行者123 更新时间:2023-11-30 22:13:58 26 4
gpt4 key购买 nike

我有一个 mysql 表,我的 C# 程序包含类似登录/注册的内容。

我正在使用这种方法来打开连接、关闭它以及其他一些东西: http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL

public void Insert(string id,string ip)
{
string query = "INSERT INTO skinsdb (id, acces, ip) VALUES('" + id + "', 1 , '" + ip + "')";

//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);

//Execute command
cmd.ExecuteNonQuery();

//close connection
this.CloseConnection();
}
else MessageBox.Show("code 18");
}

我只是将它用于新行但是我想如果“id”已经存在,只需增加他行的 1 个 acces(int) 单元格。谢谢。

最佳答案

如果你在 id 上只有一个唯一索引,那么你可以使用

"INSERT INTO skinsdb (id, acces, ip) VALUES('" + id + "',  1 , '" + ip + "') 
ON DUPLICATE KEY UPDATE acces=acces+1";

关于C# Mysql 创建新行但如果不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39100661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com