我有一个通过MySQL ODBC Connector运行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 ODBC 5.3 ANSI驱动程序和MySQL 5.7.23