mysql ExecuteReader() Object cant be converted [closed]

xu3bshqb  于 2022-12-22  发布在  Mysql
关注(0)|答案(1)|浏览(134)

Closed. This question is not reproducible or was caused by typos . It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have set up a mySql database on a server, and now i want to reach it in order for me to make a webservice. Firstly i just want to test if i can grab an entity from my query in my method (OneEntity), and put it into my list.

public IEnumerable<Person> Get()
    {
        return new List<Person> {
        new Person{ ID = 0, First = OneEntity(), Last ="Example"}
        };

    } 
    public string OneEntity()
    {
        MySql.Data.MySqlClient.MySqlConnection mySqlConnection;
        MySql.Data.MySqlClient.MySqlCommand cmd;
        String connString = System.Configuration.ConfigurationManager.ConnectionStrings["MySql"].ToString();
        mySqlConnection = new MySql.Data.MySqlClient.MySqlConnection(connString);
        cmd = new MySql.Data.MySqlClient.MySqlCommand();

        cmd.CommandText = "SELECT 'name' FROM 'CustomerDb' WHERE 'id' = 0";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = mySqlConnection;

        mySqlConnection.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        mySqlConnection.Close();
        return reader;
     }

I am not very experienced in c# and are therefore not sure if im doing it correct. However in my cmd.ExecuteReader() (Object i guess it is?!??!) i get that it
cannot implicitly convert type 'MySql.Data.MySqlDataReader' to 'System.Data.SqlClient.SqlDataReader'
What am i doing wrong here?? obviously my return is not correct either, as i specified my method to be 'string'.. but even though i type in a string, the error doesn't dissapear?

5kgi1eie

5kgi1eie1#

you shoud use MySqlDataReader not SqlDataReader

MySqlDataReader Reader = cmd.ExecuteReader();

code should return string not the reader in your case.
To return the first item use this return reader.GetString(0);

相关问题