Reputation: 48686
I have a MySql database and one of the fields is a GEOMETRY
field. I am using ADO.Net to connect to the database and I am writing data into the field like this:
var myGeometry = new MySql.Data.Types.MySqlGeometry(15.3f, 12.9f);
string cmdString = "INSERT INTO `MyTable` (LaunchPoint) VALUES (@val1);";
using (var connection = new MySqlConnection(_connectionString))
{
using (var command = new MySqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = cmdString;
command.Parameters.Add(new MySqlParameters("@val1", MySqlDbType.Geometry) {Value = myGeometry});
connection.Open();
command.ExecuteScalar();
}
}
This is working fine. It writes the data to the database and if I do a SELECT
command from MySQL Workbench, I can see the data is there.
The problem I am running into is reading the data back. The data comes back as a byte[]
instead of a MySqlGeometry
object. Here is my read code:
string sql = "SELECT * FROM MyTable";
using (var connection = new MySqlConnection(_connectionString))
{
using (var command = new MySqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandTet = sql;
connection.Open();
using (var adapter = new MySqlDataAdapter())
{
using (var ds = new DataSet())
{
adapter.SelectCommand = command;
adapter.Fill(ds);
// Error occurs on the next line
var geo = (MySqlGeometry) ds.Tables[0].Rows[0]["LaunchPoint"];
}
}
}
}
An error occurs when I try to load the geo
variable with the data. The error is telling me it can't cast a byte array into a MySqlGeometry
. Upon closer look, it does, indeed, seem like the MySQL .NET connector is converting it into a byte array for some reason. I found a similar question about this here. However, this question has no response and it's like 6 years old. Surely this would have been fixed by now if its a bug.
Does anyone have a workaround for this?
Upvotes: 1
Views: 1043