gpt4 book ai didi

c# - 带有参数值的 OleDb Update sql 不更新 Access 中的记录

转载 作者:行者123 更新时间:2023-12-02 22:31:08 25 4
gpt4 key购买 nike

有人能告诉我下面的代码有什么问题吗?

command.Connection = ConnectionManager.GetConnection();
command.CommandText = "Update Table1 SET Replaceme = ? WHERE Searchme = ?";
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("Replaceme", "Goodman");
command.Parameters.AddWithValue("Searchme", "Anand");

command.Connection.Open();
int recordsaffected = command.ExecuteNonQuery();
MessageBox.Show("Records affected : " + recordsaffected);

MessageBox 显示 0 条记录,它实际上没有更新可用的记录。

表名 (Table1) 和列名 (Replaceme and Searchme) 拼写正确。

最佳答案

首先,OleDbParameter 是位置性的,没有命名。阅读 documentation 中的备注部分.忽略任何不理解这一点的答案。

这是一个最小的工作示例。

首先,创建您的数据库:

enter image description here

enter image description here

二、编写代码:

using System;
using System.Windows.Forms;
using System.Data.OleDb;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connectionString;
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";
connectionString += "Data Source=C:\\Temp\\Database1.mdb";

using (var connection = new OleDbConnection(connectionString))
{
connection.Open();

var command = connection.CreateCommand();
command.CommandText =
"UPDATE Table1 SET Replaceme = ? WHERE Searchme = ?";

var p1 = command.CreateParameter();
p1.Value = "Goodman";
command.Parameters.Add(p1);

var p2 = command.CreateParameter();
p2.Value = "Anand";
command.Parameters.Add(p2);

var result = String.Format("Records affected: {0}",
command.ExecuteNonQuery());
MessageBox.Show(result);
}
}
}
}

结果:

enter image description here

enter image description here

关于c# - 带有参数值的 OleDb Update sql 不更新 Access 中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12226682/

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