简体   繁体   中英

How do I pull data from an SQL database into local double var using c#?

I'm trying to pull data through an ODBC connection into two local double variables but I keep getting a syntax error and I'm not sure what I'm missing.

I'm using this to plot coordinates on a map.

The code:

using (ODBC.connect = new OdbcConnection(ODBC.connectionString))
{
    double Latitude = 0.0;
    double Longitude = 0.0;

    OdbcCommand command = new OdbcCommand("SELECT Latitude, Longitude INTO ?, ? FROM TrackVehicles WHERE Registration = ?", ODBC.connect);

    command.Parameters.AddWithValue("?1", Latitude);
    command.Parameters.AddWithValue("?2", Longitude);
    command.Parameters.AddWithValue("?3", textBox1.Text);

    try
    {
        ODBC.connect.Open();
        command.ExecuteNonQuery();

        gMap.Position = new GMap.NET.PointLatLng(Latitude, Longitude);
    }
    catch (OdbcException exception)
    {
        MessageBox.Show(exception.Message);
    }
}

The SQL error I get is:

ERROR [42000] [Sybase][ODBC Driver][SQL Anywhere]Syntax error near '?'on line 1

I've looked into using Parameters.Add() instead but had no luck as I'm a complete noob.

With reference to: https://docs.microsoft.com/en-us/dotnet/api/system.data.odbc.odbcdatareader?view=netframework-4.8

It looks like you're trying to return two values from the database into your C# code.

using (ODBC.connect = new OdbcConnection(ODBC.connectionString))
{
    double Latitude = 0.0;
    double Longitude = 0.0;

    OdbcCommand command = new OdbcCommand("SELECT Latitude, Longitude FROM TrackVehicles WHERE Registration = ?", ODBC.connect);

    command.Parameters.AddWithValue("?1", textBox1.Text);

    try
    {
        ODBC.connect.Open();
        OdbcDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Latitude = reader[0];
            Longitude = reader[1];
        }
        reader.Close()

        gMap.Position = new GMap.NET.PointLatLng(Latitude, Longitude);
    }
    catch (OdbcException exception)
    {
        MessageBox.Show(exception.Message);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM