gpt4 book ai didi

c# - Access 类型不匹配

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:18 24 4
gpt4 key购买 nike

有两个表:

MyTable1:
id AutoNumber
typecode Text

MyTable2
id AutoNumber
pid Number
freq Number

using System.Data.OleDb;
namespace AccessSelect
{
class Program
{
static void Main(string[] args)
{
var sql = @"select 'x' from mytable1 where typecode=@typeCode and EXISTS (
select 'x' from mytable2 where (freq=0 OR freq=@freq) and mytable1.id=mytable2.pid)";

using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=gpi.mdb;Jet OLEDB:Database Password=???"))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);

cmd.Parameters.Add(new OleDbParameter("@typeCode", "KK3000"));
cmd.Parameters.Add(new OleDbParameter("@freq", 50));

var o = cmd.ExecuteScalar();
}
}
}
}

我不断收到“标准表达式中的数据类型不匹配”异常。

如果我更改 SQL 以包含值:

select 'x' from mytable1 where typecode='KK3000' and EXISTS (
select 'x' from mytable2 where (freq=0 OR freq=50) and mytable1.id=mytable2.pid)

我没有得到错误......

知道哪里出了问题吗?

最佳答案

来自 MSDN :

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

因此将您的查询更改为:

select 'x' from mytable1 where typecode = ? 
and EXISTS (select 'x' from mytable2 where (freq=0 OR freq = ?) and mytable1.id=mytable2.pid)

并且您必须按照它们在查询中出现的相同顺序添加参数。

关于c# - Access 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17715304/

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