关闭后,我可以重用MySQL datareader吗? [英] Can I reuse a MySQL datareader after closing it?

查看:203
本文介绍了关闭后,我可以重用MySQL datareader吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我应该有两个数据加载器还是我可以在关闭它后重复使用这个对象?



 searchCommand =   SELECT * FROM titles WHERE titles.KEY=@EKEY; 
linkCommand.CommandText = searchCommand;
linkCommand.Prepare();
linkCommand.Parameters.AddWithValue( @ EKEY,EKEY);

linkReader = linkCommand.ExecuteReader();
linkCommand.Parameters.Clear();

while (linkReader.Read())
{
/ / 这里的一些逻辑
}
linkReader.Close();
WriteLog( done authors);


searchCommand = SELECT * FROM pros WHERE pros.EKEY=@EKEY ;
linkCommand.CommandText = searchCommand;
linkCommand.Prepare();
linkCommand.Parameters.AddWithValue( @ EKEY,EKEY);

linkReader = linkCommand.ExecuteReader();
linkCommand.Parameters.Clear();

while (linkReader.Read())
{
/ / 这里的一些逻辑
}
linkReader.Close();

解决方案

不,你不需要创建另一个实例,一旦关闭,只需添加另一个命令,再次执行它。您可以重复使用它,但要确保已添加其他命令。



此外,当您使用Resources和Stream时,请使用Using语法来使用它们。使用 using(){} 语法,您将允许.NET框架自己处理资源。关闭,处理和刷新功能将在需要时调用,您根本不必担心它们。



http://msdn.microsoft。 com / zh-CN / library / yh598w02.aspx [ ^ ]

I have the following code, should I have two datareaders or I can reuse this object after having closed it?

searchCommand = "SELECT * FROM titles WHERE titles.KEY=@EKEY";
               linkCommand.CommandText = searchCommand;
               linkCommand.Prepare();
               linkCommand.Parameters.AddWithValue("@EKEY", EKEY);

               linkReader = linkCommand.ExecuteReader();
               linkCommand.Parameters.Clear();

               while (linkReader.Read())
               {
                   //some logic here
               }
               linkReader.Close();
               WriteLog("done authors");


               searchCommand = "SELECT * FROM pros WHERE pros.EKEY=@EKEY";
               linkCommand.CommandText = searchCommand;
               linkCommand.Prepare();
               linkCommand.Parameters.AddWithValue("@EKEY", EKEY);

               linkReader = linkCommand.ExecuteReader();
               linkCommand.Parameters.Clear();

               while (linkReader.Read())
               {
                   //some logic here
               }
               linkReader.Close();

解决方案

No, you don't need to create another instance, once closed, just add another command to it and execute it again. You can reuse it, but make sure another command has been added to it.

Also, when you're working with Resources and Stream, use the Using syntax to work with them. Using the using () { } syntax, you will allow .NET framework to take care of the resources itself. Close, Dispose and Flush functions would be called where required and you won't have to worry about them at all.

http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^]


这篇关于关闭后,我可以重用MySQL datareader吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆