gpt4 book ai didi

c# - 短小精悍: "Insufficient parameters supplied to the command"

转载 作者:行者123 更新时间:2023-12-01 23:24:51 26 4
gpt4 key购买 nike

我有一个Test模型类:

public class Test
{
public string One;
public int Two;
}

我有一个测试表:

CREATE TABLE "test" 
(
"one" TEXT NOT NULL,
"two" INTEGER NOT NULL
);

当尝试执行此代码时:

using (IDbConnection con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
{
con.Execute("INSERT INTO test VALUES (@One, @Two)", new Test
{
One = "hello",
Two = 123
});
}

我收到此错误:

code = Unknown (-1), message = System.Data.SQLite.SQLiteException (0x80004005): unknown error
Insufficient parameters supplied to the command

我尝试了一切,但找不到原因。

最佳答案

Dapper .execute() 命令需要命令参数为“Anonymous”、“string”、“List”和“dynamic”,因此不支持传递类型化对象

using (IDbConnection con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
{
con.Execute("INSERT INTO test (one, two) VALUES (@One, @Two)", new
{
One = "hello",
Two = 123
});
}

使用您的测试对象。<​​/strong>

using (IDbConnection con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
{
Test tobj = new Test();
tobj.One = "hello";
tobj.Two = 123;

con.Execute("INSERT INTO test (one, two) VALUES (@One, @Two)", tobj);
}

关于c# - 短小精悍: "Insufficient parameters supplied to the command",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57916394/

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