ADO.NET 动态执行多个查询

发布于 2024-10-03 13:33:14 字数 873 浏览 3 评论 0原文

我正在做一个小应用程序,用户输入一组查询然后执行它们。

问题是我想显示相关信息,例如如果他输入类似以下内容:

SELECT * FROM server;
UPDATE server SET name = 'Kojak';

它得到:

  1. 选定的行
  2. 受 UPDATE 影响的行数

我的打印循环看起来像:

reader = cmd.ExecuteReader();
do
{

    while (reader.Read())
    {
        if (!(reader.RecordsAffected > 0))
        {
            for (int i = 0; i < reader.FieldCount; i++)
                host.WriteLine("Field " + i + ": " + reader[i].ToString());
        }
        else {
            host.WriteLine(reader.RecordsAffected.ToString() + " Affected rows.");
        }
    }                    
} while (reader.NextResult());
host.WriteLine("Block executed successfuly");

问题是我无法设法区分 SELECT 和 UPDATE,因为 reader.Read() 在到达第二个查询时返回 FALSE。 当存在 UPDATE/DELETE/INSERT 查询时,如何解决这个问题并能够获取受影响的行数?

谢谢。

I'm doing a little app where a user enters a block of queries and then executes them.

The thing is that I want to show relevant information, like for example if he inputs something like:

SELECT * FROM server;
UPDATE server SET name = 'Kojak';

It gets:

  1. The rows selected
  2. The number of rows affected by the UPDATE

My printing loop looks like:

reader = cmd.ExecuteReader();
do
{

    while (reader.Read())
    {
        if (!(reader.RecordsAffected > 0))
        {
            for (int i = 0; i < reader.FieldCount; i++)
                host.WriteLine("Field " + i + ": " + reader[i].ToString());
        }
        else {
            host.WriteLine(reader.RecordsAffected.ToString() + " Affected rows.");
        }
    }                    
} while (reader.NextResult());
host.WriteLine("Block executed successfuly");

The thing is that I can't manage to make the difference between SELECTs and UPDATEs, because reader.Read() returns FALSE when it reaches the second query.
How can I solve that and be able to get the number of affected rows when there's a UPDATE/DELETE/INSERT query ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-10-10 13:33:14

您不能使用 cmd.ExecuteReader() 执行非查询。您需要使用ExecuteNonQuery()

我想您需要解析 SQL 语句列表来确定它是什么类型的命令(例如 SELECTUPDATE),并调用适当的方法。

You can't use cmd.ExecuteReader() to execute non queries. You need to use ExecuteNonQuery().

I would imagine you will need to parse the list of SQL statements to determnine what sort of comamnd it is (e.g. SELECT or UPDATE), and call the appropriate method.

好久不见√ 2024-10-10 13:33:14

@David:我是这样做的:

public int Execute(String block, dbStudio.components.DbsTab host)
{
    this.Connect();
    DbCommand cmd = this.dbConnection.CreateCommand();
    DbDataReader reader = null;

    cmd.CommandText = block;
    int ret = -1;

    try
    {
        long ticks = DateTime.Now.Ticks;
        reader = cmd.ExecuteReader();
        do
        {
            while (reader.Read())
            {
                String str = "";
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    //host.WriteLine("Field " + i + ": " + reader[i].ToString());
                    str = str + reader[i].ToString() + "    ";
                }
                host.WriteLine(str);
            }
        } while (reader.NextResult());

        host.WriteLine("Block executed successfuly");
        host.WriteLine(reader.RecordsAffected.ToString() + " Affected rows.");
        host.WriteLine(((DateTime.Now.Ticks - ticks) / 1000).ToString() + "ms");

        ret = reader.RecordsAffected;
    }
    catch (Exception e)
    {
        host.WriteLine(e.Message);
    }
    finally
    {
        this.Disconnect();
    }
    return ret;
}

@David: here's how I did it:

public int Execute(String block, dbStudio.components.DbsTab host)
{
    this.Connect();
    DbCommand cmd = this.dbConnection.CreateCommand();
    DbDataReader reader = null;

    cmd.CommandText = block;
    int ret = -1;

    try
    {
        long ticks = DateTime.Now.Ticks;
        reader = cmd.ExecuteReader();
        do
        {
            while (reader.Read())
            {
                String str = "";
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    //host.WriteLine("Field " + i + ": " + reader[i].ToString());
                    str = str + reader[i].ToString() + "    ";
                }
                host.WriteLine(str);
            }
        } while (reader.NextResult());

        host.WriteLine("Block executed successfuly");
        host.WriteLine(reader.RecordsAffected.ToString() + " Affected rows.");
        host.WriteLine(((DateTime.Now.Ticks - ticks) / 1000).ToString() + "ms");

        ret = reader.RecordsAffected;
    }
    catch (Exception e)
    {
        host.WriteLine(e.Message);
    }
    finally
    {
        this.Disconnect();
    }
    return ret;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文