SQL运用 关于上一篇和下一篇

DaNIeL920 2010-09-20 11:00:13


OleDbConnection conn = null;
OleDbDataReader myReader = null;
List<KS_Article> list = null;
try
{
string sql = "select a.id,a.tid,c.Folder,a.title,a.articlecontent,a.inputer from KS_Article as a,KS_Class as c where a.tid=c.id order by a.id";
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|1.mdb");
OleDbCommand cmd = new OleDbCommand(sql, conn);
if (conn.State != ConnectionState.Open)
conn.Open();
myReader = cmd.ExecuteReader();
list = new List<KS_Article>();
while (myReader.Read())
{
KS_Article temp = new KS_Article();
temp.id = (int)myReader["id"];
//string tid = (string)myReader["tid"];
temp.tid = (string)myReader["tid"];
temp.folder = (string)myReader["Folder"];
temp.title = (string)myReader["title"];
temp.articlecontent = (string)myReader["articlecontent"];
temp.inputer = (string)myReader["inputer"];
temp.uphtml = getUphtmlById(temp.id, temp.tid);
list.Add(temp);
}
return list;
}
catch (System.Data.OleDb.OleDbException e)
{
throw new Exception(e.Message);
}
finally
{
myReader.Close();
conn.Close();
}




temp.uphtml = getUphtmlById(temp.id, temp.tid);

想把上一篇和下一篇加到 LIST里。先处理了上一篇。。
加了这句后效率变的非常低。。有没有办法弄成联合查询之类的语句?
...全文
531 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
DaNIeL920 2010-09-21
  • 打赏
  • 举报
回复
可以实现取值了。。

不过效率不高唉~~还不如用 OleDbDataReader 快
OleDbDataReader 执行数据读取才40秒。

用LINQ要 55-58秒。不带生成。


for (int i = 0; i < list.Count; i++)
{
uplist = list.Take(i + 1).ToList<KS_Article>();
downlist = list.Skip(i + 1).ToList<KS_Article>();
List<KS_Article> uphtml = (from p in uplist where p.tid == list[i].tid where p.id < list[i].id orderby p.id descending select p).Take(1).ToList<KS_Article>();
List<KS_Article> downhtml = (from p in downlist where p.tid == list[i].tid where p.id > list[i].id orderby p.id ascending select p).Take(1).ToList<KS_Article>();
if (uphtml.Count != 0)
list[i].uphtml = uphtml[0].id.ToString() + ".html";
else
list[i].uphtml = "无";
if (downhtml.Count != 0)
list[i].downhtml = downhtml[0].id.ToString() + ".html";
else
list[i].downhtml = "无";
}


是不是我的LINQ写的有问题啊?
BIRD72sky 2010-09-20
  • 打赏
  • 举报
回复
这个上一篇,下一篇是指什么啊?不明白!
wuyq11 2010-09-20
  • 打赏
  • 举报
回复
使用linq skip(1).take(1)一次获取上一篇下一篇
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复
我也想用。。问题这个网站里的数据库。不可能改的哇~~

用来做文字版。。只能读他的ACCESS。。。然后套我自己的模板和生成命名格式。
wyq29 2010-09-20
  • 打赏
  • 举报
回复
你可以考虑用sqlserver了
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复
前面这段用了20秒左右。后面生成文件25秒左右
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复
套模板,生成目录,生成文件的。。

--------------------------

ID:19530 添加成功

共生成 目录 272 个,文章 11665 篇

程序共运行时间: 47秒
wyq29 2010-09-20
  • 打赏
  • 举报
回复
你写的最上面代码是所有文章内容

你一个页面放所有文章内容? 12000篇??
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复
因为上一篇和下一篇 都是属于同一栏目。。所以文件夹是在同一级
文件命名是 ID.html 所以我只要比对ID号就可以了。
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wyq29 的回复:]
temp.uphtml = getUphtmlById(temp.id, temp.tid);

getUphtmlById 你是怎么写的??

另外你上面返回是所有记录 你还要每条记录的上一篇 下一篇 都放进去? 这样做有什么意义?你在页面怎么体现?
[/Quote]


private string getUphtmlById(int id,string tid)
{
string sql = "select top 1 id as uphtml from KS_Article where [id] < " + id + " and tid = '" + tid + "' order by id";
//OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|1.mdb");
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader myReader = null;
try
{
myReader = cmd.ExecuteReader();
if (myReader.Read())
{
if (myReader["uphtml"].ToString() == "")
return "-1";
return myReader["uphtml"].ToString();

}
return "-1";
}
catch (System.Data.OleDb.OleDbException e)
{
throw new Exception(e.Message);
}
finally
{
myReader.Close();
}
}


页面了是替换模板。。读出来的是文章。
在页面里可以点击浏览上一篇文章和下一篇文章。
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复
单句执行当然快了。。

问题在 temp.uphtml = getUphtmlById(temp.id, temp.tid);
这个里面OleDbConnection ,OleDbDataReader 又都打开关闭了一次。。

这个里拖速度。

我现在把OleDbConnection 设成全局的了。。现在运行一次可以减少到20秒。。
OleDbDataReader 还是太占用了
wyq29 2010-09-20
  • 打赏
  • 举报
回复
temp.uphtml = getUphtmlById(temp.id, temp.tid);

getUphtmlById 你是怎么写的??

另外你上面返回是所有记录 你还要每条记录的上一篇 下一篇 都放进去? 这样做有什么意义?你在页面怎么体现?

imshi 2010-09-20
  • 打赏
  • 举报
回复
一个复杂的查询应该比多次简单的查询快点吧,不过你那么大的数据量应该换用sqlserver
wyq29 2010-09-20
  • 打赏
  • 举报
回复
上一篇 下一篇 也就两个 简单查询 只返回一条记录 10万数据 ac数据库运行一下也很快吧?


你写的问题吧。。。
imshi 2010-09-20
  • 打赏
  • 举报
回复
KS_Article可以增加几个属性:如lastid,lasttitle,nextid,nexttitle;
将获取上一个和下一个的Sql语句合并到一条sql中,只查询一次
select top 1 id from table1 where id>@id as nextid
select top 1 title from table1 where id>@id as nexttitle
....
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复
这个我知道的。。我是想合并到一起查询

因为是ACCESS不能用存储过程。
加了那个取上一篇之后 运行非常慢。效率好低

读了12000条测试数据。。用了70多秒。。这也太慢了··= =

不加的时候没具体侧过。感觉一下就读好了。
wuyq11 2010-09-20
  • 打赏
  • 举报
回复
下一篇select top 1 * from table1 where id>@id order by id
上一篇select top 1 * from table1 where id <@id order by id desc
或使用LINQ 排序skip,take(1)
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复
你没看明白问题。。。= =
无涯乌鸦五雅 2010-09-20
  • 打赏
  • 举报
回复
27 下一条条数据 28 select TOP 1 * from Product where ProductID>27 order by ProductID asc

27 上一条数据26 select TOP 1 * from Product where ProductID<27 order by ProductID DESC



前天才做的
DaNIeL920 2010-09-20
  • 打赏
  • 举报
回复

for (int i = 0; i < list.Count; i++)
{
var m = (from p in list where p.tid == list[i].tid where p.id < list[i].id select p.id).Skip(1).Take(1);
list[i].uphtml = "m转换";
}


LINQ语句搜了下。。磕磕巴巴写出来了。。= =
这个m 值怎么取。。不是很清楚唉~~断点设置了可以看到结果是正确的。
就是取不出来。。

加载更多回复(5)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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