c# - 在数据库中插入文本框值

标签 c# sql ado.net

我使用以下代码将 TextBox 值保存到数据库中。但是,当我插入值时,它将保存在新行中。如何将它保存到同一行?

private void button1_Click(object sender, EventArgs e)
{
    string pass = textBox1.Text;
    sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=P3;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = sql;
    cmd.CommandText = ("Insert [MyTable] ([MyColumn]) Values (@pass)");
    cmd.Parameters.AddWithValue("@pass", pass);
    sql.Open();
    cmd.ExecuteNonQuery();
    sql.Close();
}

最佳答案

使用更新命令而不是插入。

cmd.CommandText = "update YourTable set FieldName = YourValue where KeyField = YourKeyValue"

关于c# - 在数据库中插入文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10403798/

相关文章:

c# - 通过 ado.net 从 C# 调用 SQL Server 存储过程超时

c# 扩展方法 - 设计模式

c# - 使用 NHibernate 在 WHERE 子句中使用 NOT IN 进行查询

c# - .Net 应用程序中并发问题的最佳实践

c# - ADO.NET Entity Framework 的内部类

c# - ADO .NET、 Entity Framework 和 LINQ to SQL - 我很困惑

c# - ASP.NET 复合控件向父窗体引发事件

sql - 从具有非唯一外键的表中选择 N 行

c# - 我如何将从数据库中检索到的 int 转换为字符串

mysql - 我需要多表查询中的空字段