Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MySql select with c#

Tags:

c#

select

mysql

Can anyone tell whats wrong with my code? I have tried a million different things and I cant seem to make it work. I need to make a select in my mysql database and use the id from the table with the specified name I take from a combobox.

I took that name from the combobox and put it into a variable named "nomeres", now I need to do a select with it and take the id from that name from the database. Everything I try to do results in a mysql syntax error in line 1, but I've tried alot of things and its always the same. The database is fine, I tried the select directly from it myself, no tables or columns names are incorrect. This is the code im using:

MySql.Data.MySqlClient.MySqlConnection dbConn = new MySql.Data.MySqlClient.MySqlConnection("Persist Security Info=False;server=localhost;database=notas;uid=root;password=" + dbpwd);

MySqlCommand cmd = dbConn.CreateCommand();
cmd.CommandText = "SELECT id from residentes WHERE nome ='" + nomeres;

try
{
    dbConn.Open();                
} catch (Exception erro) {
    MessageBox.Show("Erro" + erro);
    this.Close();
}

MySqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
    idnumber = reader.ToString();
}
like image 216
Martin Koch Avatar asked Dec 22 '13 03:12

Martin Koch


People also ask

Can I use MySQL with C?

The C API provides low-level access to the MySQL client/server protocol and enables C programs to access database contents. The C API code is distributed with MySQL and implemented in the libmysqlclient library. For legal information, see the Legal Notices.

Can you use SELECT in MySQL?

SELECT is used to retrieve rows selected from one or more tables, and can include UNION operations and subqueries. Beginning with MySQL 8.0.31, INTERSECT and EXCEPT operations are also supported.

How do I SELECT data in MySQL?

Introduction to MySQL SELECT statement First, specify one or more columns from which you want to select data after the SELECT keyword. If the select_list has multiple columns, you need to separate them by a comma ( , ). Second, specify the name of the table from which you want to select data after the FROM keyword.

How does SELECT work in MySQL?

MySQL SELECT statement queries the database according to the criteria set by the operator and returns the rows/columns that match those criteria. With the help of this MySQL query data method, database administrators can retrieve, group, summarize and analyze data.


1 Answers

as others have already pointed you towards right direction, i would like to suggest you to use parameterised queries to avoid SQL injection attacks.

Your query is open to SQL injection attacks so please read here

Try This: using parameterised SQL queries

cmd.CommandText = "SELECT id from residentes WHERE nome = @nome";
cmd.Parameters.AddWithValue("@nome",nomeres);
like image 77
Sudhakar Tillapudi Avatar answered Sep 20 '22 09:09

Sudhakar Tillapudi