gpt4 book ai didi

c# - 创建新的 DbCommand 时出错

转载 作者:行者123 更新时间:2023-12-02 22:43:33 24 4
gpt4 key购买 nike

我想创建一个新的 DbCommand,但出现了一些错误:

DbCommand insertcommand = new DbCommand("insert into persons(firstname, lastname, email) values(?, ?, ?)", connection);
insertcommand.Parameters.Add(1, DbType.String, 50, "fistname");
insertcommand.Parameters.Add(2, DbType.String, 50, "lastname");
insertcommand.Parameters.Add(3, DbType.String, 100, "email");
dbAdapter.InsertCommand = insertcommand;

这会产生错误

Cannot create an instance of the abstract class Or interface "System.Data.Common.DbCommand"

No overload for method 'Add' takes 4 arguments

我应该如何创建这些命令并填充参数?

最佳答案

您通常需要要么直接创建具体实例(new SqlCommand 等)或使用IDbConnection.CreateCommand .

尚不清楚哪种最适合您的情况。

至于 Add 调用 - 如果您使用具体的命令类型,您可能有更多可用的重载。否则你可能想要使用 IDbCommand.CreateParameter ,设置属性,然后将其添加到参数集合中。

关于c# - 创建新的 DbCommand 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10400903/

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