永远不会将IsPrepared设置为true

永远不会将IsPrepared设置为true

本文介绍了MySqlCommand Prepare()永远不会将IsPrepared设置为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是问题:无论我做什么,我都无法实际准备MySqlCommand.我尝试从 http://dev.mysql.com/doc/refman/5.6/zh-CN/connector-net-programming-prepared.html ,但进行了非常小的修改,但这实际上也不起作用.

Here's the issue: no matter what I seem to do, I can't get a MySqlCommand to actually prepare. I've tried copy/pasting the example code from http://dev.mysql.com/doc/refman/5.6/en/connector-net-programming-prepared.html with very slight modifications, but that does not actually work either.

我搜寻Google尝试寻找解决方案,但最接近的是: MySql语句准备不粘" ,它实际上并未回答问题.

I scoured Google to try and find a solution, but the closest thing that came up was: MySql statement prepare "not sticking" which did not actually answer the question.

这是此测试的我的表格设置:

Here's my table setup for this test:

CREATE  TABLE `test`.`test_prepared_query` (
  `id` INT NOT NULL ,
  `value` INT NOT NULL ,
  PRIMARY KEY (`id`) );

C#中的测试代码

public void TestPrepareQuery()
{
    connString = new MySqlConnectionStringBuilder();
    connString.Server = "localhost";
    connString.Database = "test";
    connString.UserID = "someuserid";
    connString.Password = "somepassword";

    bool isprepared;

    using (MySqlConnection conn = new MySqlConnection(connString.ToString()))
    {
        MySqlCommand cmd = new MySqlCommand(
            "INSERT INTO test_prepared_query VALUES (@id, @value)", conn);
        cmd.Connection.Open();

        cmd.Prepare();
        isprepared = cmd.IsPrepared; // isprepared is false here

        cmd.Parameters.AddWithValue("@id", 0);
        cmd.Parameters.AddWithValue("@value", 0);

        cmd.Prepare();
        isprepared = cmd.IsPrepared; // isprepared is still false

        // this is 1 -- the query succeeds
        int rowsAffected = cmd.ExecuteNonQuery();

        for (int i = 1; i < 10; i++)
        {
            cmd.Parameters["@id"].Value = i;
            cmd.Parameters["@value"].Value = i;

            // this is 1 -- the query succeeds
            rowsAffected = cmd.ExecuteNonQuery();
        }
    }
}

运行代码时,它确实将表中的行成功放置,但是单步执行该程序将检查cmd.IsPrepared的状态始终为false.有谁知道为什么会这样?该源代码与示例代码基本相同,只修改了表名以及实际的连接字符串.

When I run the code, it does successfully put rows in the table, but stepping through the program reviews that the state of cmd.IsPrepared is always false. Does anyone know why this could be happening? This source code is essentially identical to the example code, with only modifications to the table name as well as real connection strings.

我已经尝试过使用?name格式的变量,但这也不起作用.在测试中,我一次也只尝试调用一次cmd.Prepare()方法.

I've tried variables with ?name format, and that does not work either. I've also tried only having one cmd.Prepare() method call at a time in the tests.

推荐答案

我最终提取了MySql Connector/.NET的源代码,发现如果MySqlCommand.Connection.Settings.IgnorePrepare = true(这是默认值). ,那么调用Prepare是一个否定操作.

I eventually pulled up the source code for MySql Connector / .NET and discovered that if MySqlCommand.Connection.Settings.IgnorePrepare = true (which is the default!), then calling Prepare is a no op.

解决此问题的方法是在连接字符串中将IgnorePrepare显式设置为false.使用以下代码片段,使用MySqlConnectionStringBuilder可以很容易地做到这一点:

The way to fix this is to explicitly set IgnorePrepare to false in the connection string. This can be done rather easily with a MySqlConnectionStringBuilder using the following code snippet:

MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();
// .. set up the rest of your connection
connBuilder.IgnorePrepare = false;

MySqlConnection conn = new MySqlConnection(connBuilder.ToString());

这篇关于MySqlCommand Prepare()永远不会将IsPrepared设置为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:47