简体   繁体   English

ado.net中的数据读取器

[英]data reader in ado.net

I have a data reader object, which reads through say 4 rows, and I will be looping through the rows. 我有一个数据读取器对象,可以读取4行,而我将遍历这些行。 While reading third row, I insert a row inside the same table, will my data reader be able to read the newly inserted row. 在读取第三行时,我在同一张表中插入一行,我的数据读取器将能够读取新插入的行。 If not how to achieve this functionality 如果没有,如何实现此功能

Here is the code i tried. 这是我尝试的代码。

AseCommand sessionCmd = null;
            //AseCommand selectCmd = null;
            AseCommand insertCmd = null;

            AseDataReader reader = null;

            string retCode = "Nothing returned from the Server";
            string insertStatement;
            AseConnection conn = null;
                conn = new AseConnection("Data Source='" + host + "';Port='" + port + "';UID='" + user + "';PWD='" + password + "';Database=" + db + ";");
                conn.Open();
                sessionCmd = new AseCommand("select * from dbo.DummyTable", conn);

                try
                {
                    reader = sessionCmd.ExecuteReader();
                    int count = 0;
                    while (reader.Read())
                    {

                        Console.WriteLine(reader.GetString(0));
                        count++;
                        if (count == 3)
                        {
                            //insert into table
                            insertCmd = new AseCommand("insert into DummyTable values (5)", conn);
                            insertCmd.ExecuteReader();
                        }
                    }
                }

                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                }

I found your question interesting, because I thought the context would along the lines if a row being added from another process, would the current reader see it. 我发现您的问题很有趣,因为我认为如果从另一个进程中添加一行内容,当前读者会看到上下文。 If I had written your test and it did not produce the result I expected, I might come to Stackoverflow and ask: is it possible to read the new row and I just did not do it right? 如果我已经编写了您的测试,但未达到预期的结果,我可能会去Stackoverflow询问: 是否可以读取新行,而我做得不好? or maybe even get an answer like: it might work sometimes but not always because of xyz ... However, you got answer three, which was 'don't bother us, we didn't sleep well last night.' 甚至可能得到诸如这样的答案: 有时可能会但由于xyz而并非总是如此 。但是,您得到了答案三,那就是“不要打扰我们,昨晚我们睡不好觉。”

In general terms, a DataReader is a read-only forward-reading stream of tabular data sets. 一般而言,DataReader是表格数据集的只读前读流。 It is streaming the results of a query, not monitoring a table...so the query results have already been executed by the database and are being served when called,but not being updated. 它正在流式传输查询的结果,而不监视表...因此查询结果已由数据库执行,并且在调用时将被提供,但不会被更新。

Incidentally, what you did was insert a new row using an ExecuteReader command, which returned a new reader...it doesn't modify the outer reader you are looping through. 顺便说一句,您所做的是使用ExecuteReader命令插入了新行,该命令返回了一个新的阅读器...它不会修改您正在遍历的外部阅读器。 You could read the results of the insert from the new reader...although its not quite clear why you would want to. 您可以从新阅读器中读取插入的结果...尽管不清楚为什么要这么做。

Although probably beyond the scope of your test, you might be interested in the concept of Multiple Active Result Sets which does allow you to go back and forth between two results sets on the same connection. 尽管可能超出测试范围,但您可能对“ 多个活动结果集”的概念感兴趣,该概念的确使您可以在同一连接的两个结果集之间来回切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM