C#查询access数据库无结果,

tsmmst 2011-07-09 12:06:56
有一张表shorestPaths ,有一个字段PointsPath,是备注类型,里面内容形如“,1,2,3,4,8,14,” ,我写下面的语句去查询返回结果为null,不知道问题出在哪?在Access里面直接查是有结果的,郁闷
count是一个int型数组。
OleDbCommand command = dbconn.CreateCommand();
for (int j = 0; j < count.Length; j++)
{
count[j] = 0;
string id=(j+1).ToString();
command.CommandText = @"SELECT * FROM shorestPaths WHERE (((shorestPaths.[PointsPath]) like '%,"+id+",%'))";
count[j] = command.ExecuteNonQuery();

}
...全文
236 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tsmmst 2011-07-09
  • 打赏
  • 举报
回复
求各位帮忙看看指导啊,纠结,郁闷中
tsmmst 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 tsmmst 的回复:]
具体需求就是,有一张表shorestPaths ,有一个字段PointsPath,是备注类型,里面内容形如“,1,2,3,4,8,14,” ,我希望查出PointsPath字段中包含2的记录的条数,也就是查找",2,"的个数
[/Quote]

tsmmst 2011-07-09
  • 打赏
  • 举报
回复
具体需求就是,有一张表shorestPaths ,有一个字段PointsPath,是备注类型,里面内容形如“,1,2,3,4,8,14,” ,我希望查出PointsPath字段中包含2的记录的条数,也就是查找",2,"的个数
tsmmst 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhy2003119 的回复:]
like '%,"+id+",%'
这句的意思你明白吗?

例如:

like '%,1,%' 的
满足条件 是: 0,1,2
而 ,1,2 着是不满足条件的因为 '%, 表是 ,前面要有值.

所以你j=0是根本就 没有符合条件的结果.

你的查询 条件 第1个和最后一个 数字都是不符合你的查询条件的.

由于不知道你的需求 和具体 内容,不好帮你分析什么是对的
……
[/Quote]

我特意查了的,access里面%匹配0个或多个字符;而且查‘,2,’也没有返回结果啊,莫名其妙啊
谢谢你的关注!
老鼠爱上猫 2011-07-09
  • 打赏
  • 举报
回复
like '%,"+id+",%'
这句的意思你明白吗?

例如:

like '%,1,%' 的
满足条件 是: 0,1,2
而 ,1,2 着是不满足条件的因为 '%, 表是 ,前面要有值.

所以你j=0是根本就 没有符合条件的结果.

你的查询 条件 第1个和最后一个 数字都是不符合你的查询条件的.

由于不知道你的需求 和具体 内容,不好帮你分析什么是对的
tsmmst 2011-07-09
  • 打赏
  • 举报
回复
忘了说,表里面有200万行记录
tsmmst 2011-07-09
  • 打赏
  • 举报
回复
这个返回结果还是0个啊
count[j] = command.ExecuteNonQuery();

直接在Access里面查是返回2025条记录的...

忘了说,表里面有200万行记录

[Quote=引用 4 楼 taomanman 的回复:]
C# code


command.CommandText = "SELECT * FROM shorestPaths WHERE PointsPath like '?,"+id+"?,'";
[/Quote]
暖枫无敌 2011-07-09
  • 打赏
  • 举报
回复
?相当于SQL中的_单字符匹配
tsmmst 2011-07-09
  • 打赏
  • 举报
回复
?表示什么,应该是
command.CommandText = "SELECT * FROM shorestPaths WHERE PointsPath like '?,"+id+",?'";


问题在于同一条SQL语句,这样使用是有结果的呀
da = new OleDbDataAdapter("SELECT * FROM shorestPaths WHERE (((shorestPaths.[PointsPath]) like '%,1,%'))", dbconn);这个查询是有结果的啊
暖枫无敌 2011-07-09
  • 打赏
  • 举报
回复

command.CommandText = "SELECT * FROM shorestPaths WHERE PointsPath like '?,"+id+"?,'";
tsmmst 2011-07-09
  • 打赏
  • 举报
回复
private void button7_Click(object sender, EventArgs e)
{
dbconn = new OleDbConnection(path);
dbconn.Open();
DataSet ds = new DataSet(); //创建DataSet对象
da = new OleDbDataAdapter(@"select * from shorestPaths", dbconn); //引用数据库连接dbconn并依据SQL语句"select * from kaizhi"创建OleDbDataAdapter对象da
//da = new OleDbDataAdapter("SELECT * FROM shorestPaths WHERE (((shorestPaths.[PointsPath]) like '%,1,%'))", dbconn);这个查询是有结果的啊
da.Fill(ds, "shorestPaths");
DataSet ds2 = new DataSet();
da = new OleDbDataAdapter(@"select * from Points", dbconn); da.Fill(ds2, "Points");
OleDbCommandBuilder cb = new OleDbCommandBuilder(da); // 创建OleDbCommandBuilder对象cb用于更新OleDbDataAdapter对象da的Insert、Delete、Update指令
da.UpdateCommand = cb.GetUpdateCommand(); //更新OleDbDataAdapter对象da的指令

int[] count = new int[ds2.Tables["Points"].Rows.Count];


OleDbCommand command = dbconn.CreateCommand();

for (int j = 0; j < count.Length; j++)
{
count[j] = 0;
string id=(j+1).ToString();
command.CommandText = @"SELECT * FROM shorestPaths WHERE (((shorestPaths.[PointsPath]) like '%,"+id+",%'))";
count[j] = command.ExecuteNonQuery();

}
for (int j = 0; j < ds2.Tables["Points"].Rows.Count; j++)
{
ds2.Tables["Points"].Rows[j]["pointBetweenness"] = count[j];
}
da.Update(ds2, "Points");
dbconn.Close();
}
这个是Button事件里面的代码,真心不知道物体出在哪儿呀
tsmmst 2011-07-09
  • 打赏
  • 举报
回复
在Access里面模糊查询时,通配符用“*”,但是用C#写SQL语句,好像要用“%”

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧