odbc准备语句引发异常

h7wcgrx3  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(205)

我有一个通过mysql-odbc连接器运行sql语句的程序。当我对具有几何图形列类型的表的select查询使用占位符时,会引发异常。
这是密码

static void Main(string[] args)
    {
        try
        {
            using (OdbcConnection connection = new OdbcConnection(ConnectionString))
            {
                connection.Open();

                OdbcCommand command = new OdbcCommand("DROP TABLE IF EXISTS TestT", connection);
                command.ExecuteNonQuery();

                command.CommandText = "CREATE TABLE TestT(ID INT, NAME VARCHAR(20), DeliveryLocation geometry DEFAULT NULL)";
                command.ExecuteNonQuery();

                command.CommandText = "INSERT INTO TestT(ID, NAME) VALUES(1, 'DT'),(2, 'teamDT')";
                command.ExecuteNonQuery();

                command.CommandText = "SELECT * FROM TestT where id = ?";

                var parameter = new OdbcParameter("Parameter1", OdbcType.Int);
                parameter.Direction = ParameterDirection.Input;
                parameter.Value = 1;
                command.Parameters.Add(parameter);

                var adapter = new OdbcDataAdapter
                {
                    SelectCommand = command
                };
                var dataSet = new DataSet();

                adapter.Fill(dataSet);
                var table = dataSet.Tables[0];

                Console.WriteLine($"Number of rows returned: {table.Rows.Count}");
            }
        }
        catch (OdbcException exception)
        {
            for (int i = 0; i < exception.Errors.Count; i++)
            {
                Console.WriteLine("ERROR #" + i + "\n" + "Message: " +
                              exception.Errors[i].Message + "\n" +
                              "Native: " +
                              exception.Errors[i].NativeError.ToString() + "\n" +
                              "Source: " +
                              exception.Errors[i].Source + "\n" +
                              "SQL: " +
                              exception.Errors[i].SQLState + "\n");
            }
        }
    }

按原样运行会得到以下输出

ERROR #0
Message: [MySQL][ODBC 5.3(a) Driver][mysqld-5.7.23-log]
Native: 517
Source: myodbc5a.dll
SQL: HY000

如果我将deliverylocation类型更改为int,或者从表中删除列,或者将select语句更改为

SELECT * FROM TestT where id = 1

我得到以下回应

Number of rows returned: 1

如何防止错误发生,而不必删除或更改列类型,并在select语句中使用占位符(?)保留列类型?
我使用的是mysql odbc5.3ansi驱动程序和mysql 5.7.23

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题