Selecting whole row in Mysql database using C# and saving it as a list

0

I'm trying to select one specific row from my mysql database.

In SQL I'd just use

select * from endkunden where id = 2;

this works just fine, I get everything I want.

Now I want to do this in my c# code and save the data into a string list.

I tried doing it like this

public List<string> SelectListRow(string target, string table,string idef, int id)
    {
        string query = "SELECT " + target + " FROM " + table + " where "+ idef + " = " +id;
        List<string> list = new List<string>();

        if (this.OpenConnection())
        {
            MySqlCommand cmd = new MySqlCommand(query, connection);
            MySqlDataReader dataReader = cmd.ExecuteReader();

            while (dataReader.Read())
            {

                list.Add(Convert.ToString(dataReader[target]));
            }

            dataReader.Close();
            this.CloseConnection();
            return list;
        }

        return list;
    }

Since this worked for selecting all columns in the table using

select compname from endkunden;

I assumed, that this would work with rows as well, but it doesn't. When using it like this

I use the following query:

select * 
from endkunden 
where id = 2

but now I get an error:

System.IndexOutOfRangeException
HResult=0x80131508
Nachricht = Could not find specified column in results: *
Quelle = MySql.Data

Stapelüberwachung:
at MySql.Data.MySqlClient.ResultSet.GetOrdinal(String name)
at MySql.Data.MySqlClient.MySqlDataReader.GetOrdinal(String name)
at MySql.Data.MySqlClient.MySqlDataReader.get_Item(String name)
at MySQL_Tests.DBAC.SelectListRow(String target, String table, String idef, Int32 id) in C:\Users\Murf\source\repos\MySQL Tests\MySQL Tests\DBAC.cs:line 187
at MySQL_Tests.Program.Main(String[] args) in C:\Users\Murf\source\repos\MySQL Tests\MySQL Tests\Program.cs:line 13

Nachricht means message, quelle means error, stapelüberwachung means .. i don´t know

Any ideas how to fix this?

Greetings, Murf

c#
mysql
asked on Stack Overflow Apr 27, 2020 by Murf • edited Apr 27, 2020 by marc_s

2 Answers

0

Instead using the data reader with the column name use dataReader.GetString(0) which would give the first column.

public List<string> SelectListRow(string target, string table,string idef, int id)
    {
        string query = "SELECT " + target + " FROM " + table + " where "+ idef + " = " +id;
        List<string> list = new List<string>();
        if (this.OpenConnection())
        {
            MySqlCommand cmd = new MySqlCommand(query, connection);
            MySqlDataReader dataReader = cmd.ExecuteReader();

            while (dataReader.Read())
            {

                list.Add(dataReader.GetString(0));
            }
            dataReader.Close();
            this.CloseConnection();
            return list;
        }

        return list;
    }
answered on Stack Overflow Apr 27, 2020 by nbk
0

Since you are passing * it seems that you want multiple columns instead of just the first one.

public List<string> SelectListRow(string target, string table,string idef, int id)
    {
        string query = "SELECT " + target + " FROM " + table + " where "+ idef + " = " +id;
        List<string> list = new List<string>();
        if (this.OpenConnection())
        {
            MySqlCommand cmd = new MySqlCommand(query, connection);
            MySqlDataReader dataReader = cmd.ExecuteReader();

            while (dataReader.Read())
            {
                for (int i = 0; i < dataReader.FieldCount; i++) {
                {
                    list.Add(dataReader.GetString(i));
                }
            }
            dataReader.Close();
            this.CloseConnection();
            return list;
        }

        return list;
    }

That will get you the values that you require. One additional thing I noticed is that if there is an exception that the connection data reader arent cleaned up. You can fix this by using try block with a finally clause or wrapping these in 'using' blocks.

answered on Stack Overflow Apr 27, 2020 by harleybl

User contributions licensed under CC BY-SA 3.0