从SQL数据库中删除1个值c# [英] Remove 1 Value from SQL database c#
问题描述
你好我想删除我从SQL数据库获得的值。
Hello i want remove Value i got from SQL database.
public string Key()
{
var dt = new DataTable();
using (var cn = new MySqlConnection(ConnectionString))
{
using (var cmd = new MySqlCommand() { Connection = cn })
{
#1
cmd.CommandText =" SELECT * FROM sql7253778.Keys" ;;
cn.Open();
dt.Load(cmd.ExecuteReader());
if(dt.Rows.Count> 0)
s = dt.Rows [0] [" key"]。ToString();
else
{
MessageBox.Show("抱歉,没有更多的钥匙:(发送此消息的屏幕截图给我不和谐。");
}
#1 cmd.CommandText = "SELECT * FROM sql7253778.Keys"; cn.Open(); dt.Load(cmd.ExecuteReader()); if (dt.Rows.Count > 0) s = dt.Rows[0]["key"].ToString(); else { MessageBox.Show("Sorry, No more keys :( Send screenshot of this message To me on discord."); }
#2
}
}
返回s;
}
#2 } } return s; }
加载并返回我来自数据库的一个值。
That loads and returns me one value from Database.
但现在我想从数据库中删除 string s 的价值。
But now i want to remove value of string s from database.
我只能删除在#2区域上执行命令。
i can only give the Remove command on #2 zone.
这就是我的SQL代码:
Thats my SQL code:
SELECT * FROM `Keys`
我不知道如何插入另一个命令,因为它对我来说永远不会有用。
推荐答案
最佳做法是获取字符串值以及标识该行的主键/ id,以便您可以编写;
Best practice is to get the string value along with the primary key/id that identifies the row so you can then write;
DELETE FROM sql7253778.Keys WHERE ID = @Id
DELETE FROM sql7253778.Keys WHERE ID = @Id
我在哪里D是主要字段名称,@ Id是您添加到命令对象的Parameter集合的参数,例如: cmd.Parameters.AddWithValue(" @Id,
N )其中N是在上面的读取操作期间从数据库表中读取字符串所得到的主键值。
Where ID is the primary field name and @Id is a parameter you add to the Parameter collection of the command object, e.g. cmd.Parameters.AddWithValue("@Id, N) where N is the primary key value you would get from reading the string from the database table during the read operation above.
这篇关于从SQL数据库中删除1个值c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!