C# 具有几何列类型的表的Odbc准备语句引发异常
我有一个通过MySQL ODBC连接器运行SQL语句的程序。在具有几何图形列类型的表上使用占位符进行SELECT查询时,会引发异常 这是密码C# 具有几何列类型的表的Odbc准备语句引发异常,c#,mysql,odbc,prepared-statement,C#,Mysql,Odbc,Prepared Statement,我有一个通过MySQL ODBC连接器运行SQL语句的程序。在具有几何图形列类型的表上使用占位符进行SELECT查询时,会引发异常 这是密码 static void Main(string[] args) { try { using (OdbcConnection connection = new OdbcConnection(ConnectionString)) { conn
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");
}
}
}
如果我将DeliveryLocation类型更改为int或从表中删除列,或将SELECT语句更改为
SELECT * FROM TestT where id = 1
我得到以下回应
Number of rows returned: 1
如何避免发生错误而不必删除或更改列类型,并在SELECT语句中使用占位符(?)
我使用的是MySQL ODBC 5.3 ANSI驱动程序和MySQL 5.7.23您不能使用MySQL的.Net API吗?在这种情况下,我必须使用ODBC Connector,我不确定,但我认为ODBC中不支持这种数据类型。您可能会考虑编写一个存储过程,它将需要的数据返回到您程序中需要的空间数据属性中。
Number of rows returned: 1